12-SpringBoot之資料庫(三)——Mybatis
阿新 • • 發佈:2018-11-28
SpringBoot之資料庫(三)——Mybatis
1. 新增pom依賴
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</ artifactId>
<version>1.3.2</version>
</dependency>
2. Mybatis配置
在application.yml檔案中新增Mybatis配置,如下:
#mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.springboot.web.model
專案啟動類新增@MapperScan
@MapperScan("com.springboot.web.dao" )
3. 案例開發
3.1 建立實體類
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String id;
private String name;
private Integer age;
@JSONField(format="yyyy-MM-dd")
private Date birthday;
public User(String id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
}
3.2 DAO
DAO介面:
import com.springboot.web.model.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMybatisDao {
Integer insert(User user);
List<User> getAllUserList();
}
mapper檔案:在resources\mapper檔案下建立UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.springboot.web.dao.UserMybatisDao">
<sql id="BASE_TABLE">
user
</sql>
<sql id="BASE_COLUMN">
id,name,age,birthday
</sql>
<insert id="insert" parameterType="com.springboot.web.model.User">
INSERT INTO
<include refid="BASE_TABLE"/>
<trim prefix="(" suffix=")" suffixOverrides=",">
<include refid="BASE_COLUMN"/>
</trim>
<trim prefix="VALUES(" suffix=")" suffixOverrides=",">
#{id, jdbcType=VARCHAR},#{name, jdbcType=VARCHAR},
#{age, jdbcType=INTEGER}, #{birthday, jdbcType=DATE},
</trim>
</insert>
<select id="getAllUserList" resultType="com.springboot.web.model.User">
SELECT
<include refid="BASE_COLUMN"/>
FROM
<include refid="BASE_TABLE"/>
</select>
</mapper>
3.3 Service
service介面:
import com.springboot.web.model.User;
import java.util.List;
public interface UserMybatisService {
Integer insert(User user);
List<User> getAllUserList();
}
service實現:
import com.springboot.web.dao.UserMybatisDao;
import com.springboot.web.model.User;
import com.springboot.web.service.UserMybatisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserMybatisServiceImpl implements UserMybatisService {
@Autowired
private UserMybatisDao userMybatisDao;
@Override
public Integer insert(User user) {
return userMybatisDao.insert(user);
}
@Override
public List<User> getAllUserList() {
return userMybatisDao.getAllUserList();
}
}
3.4 Controller
import com.springboot.web.model.User;
import com.springboot.web.service.UserMybatisService;
import com.springboot.web.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
@RestController
@RequestMapping("/mybatis")
public class UserMybatisController {
@Autowired
private UserMybatisService userMybatisService;
@GetMapping("/getAllUserList")
public Object getAllUserList() {
return userMybatisService.getAllUserList();
}
@PostMapping("/createUser")
public void createUser(@RequestParam("name") String name, @RequestParam("age") Integer age) {
User user = new User(UUIDUtil.getUUID(), name, age, new Date());
userMybatisService.insert(user);
}
}
3.5 測試
使用postman進行測試
測試createUser:
測試getAllUserList:
4. 原始碼下載
原始碼下載地址:https://download.csdn.net/download/huangjun0210/10791197