这篇文章是介绍 Spring Boot整合mybatis的,一个简单的增删改查。
建表
DROP TABLE IF EXISTS tbl_user;
CREATE TABLE tbl_user (
id int(11) NOT NULL AUTO_INCREMENT,
user_name varchar(255) NOT NULL,
password varchar(255) NOT NULL,
age int(11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
在pom文件里添加mybatis依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
在application.properties配置文件中配置数据源、Mybatis的实体和配置文件路径:
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_demo
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.typeAliasesPackage: cn.yideng.mybatis.entity
mybatis.mapperLocations: classpath:mapper/*.xml
建立包结构:entity dao service controller
创建一个实体类:
public class User {
private Integer id;
private String userName;
private String password;
private Integer age;
public User() {}
public User(Integer id, String userName, String password, Integer age) {
this.id = id;
this.userName = userName;
this.password = password;
this.age = age;
}
// 省略 getter/setter, toString()
}
DAO接口:
public interface UserDao {
List<User> findAll(); //查询所有记录
void addUser(User user); // 新增
void updateUser(User user); // 修改
int deleteUser(Integer id); //根据id删除
}
Service接口:
public interface UserService {
List<User> findAll();
void addUser(User user);
void updateUser(User user);
int deleteUser(Integer id);
}
Service接口的实现类:
@Service(value="userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public void addUser(User user) {
userDao.addUser(user);
}
@Override
public void updateUser(User user) {
userDao.updateUser(user);
}
@Override
public int deleteUser(Integer id) {
return userDao.deleteUser(id);
}
}
```
UserMapper.xml映射文件:路径是
esourcesmapperUserMapper.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="cn.yideng.mybatis.dao.UserDao">
<resultMap id="userResultMap" type="cn.yideng.mybatis.entity.User">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<sql id="user_column_list">
id, user_name, password, age
</sql>
<select id="findAll" resultMap="userResultMap">
select
<include refid="user_column_list" />
from tbl_user
</select>
<insert id="addUser" parameterType="cn.yideng.mybatis.entity.User">
insert into tbl_user (user_name, password, age)
values (
#{userName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER})
</insert>
<update id="updateUser" parameterType="cn.yideng.mybatis.entity.User">
update tbl_user set
user_name= #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age= #{age,jdbcType=INTEGER}
where id= #{id,jdbcType=INTEGER}
</update>
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from tbl_user where id= #{id,jdbcType=INTEGER}
</delete>
</mapper>
Controller类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping(value = "/add")
private void addUser(User user){
userService.addUser(user);
}
@GetMapping("/getList")
private List<User> getAllUser(){
return userService.findAll();
}
@PutMapping(value = "update/{id}")
private void updateUser(User user){
userService.updateUser(user);
}
@DeleteMapping(value = "delete/{id}")
private int deleteUser(Integer id){
return userService.deleteUser(id);
}
}
修改启动类,添加@MapperScan注解,使其可以扫描DAO层接口:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableAutoConfiguration
@MapperScan("cn.yideng.mybatis.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
然后用postman 模拟测试,