整合MyBatis
官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
整合测试
1、导入 MyBatis 所需要的依赖,和其他依赖(其实在创建项目的时候就可以选择)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2、配置数据库连接信息
spring:
datasource:
username: root
password: Wl123456
url: jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
3、先测试数据库是否连接成功!
4、创建实体类,导入 Lombok!数据库可以根据字段随便写几个
@AllArgsConstructor
@Data
@NoArgsConstructor
@Repository
public class User {
private int id;
private String name;
private String pwd;
}
5、创建mapper目录以及对应的 Mapper 接口
//@Mapper这个注解表示这是一个mybatis的mapper类
@Mapper
@Repository
public interface UserMapper {
//查找所有的user
List<User> queryUserList();
//根据id查user
User queryUserById(@Param("id") int id);
//增加一个人user
int addUser(User user);
//更新一个user
int updateUser(User user);
//删除一个user
int deleteUser(@Param("id") int id);
}
6、对应的Mapper映射文件
<?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.wu.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from user
</select>
<select id="queryUserById" resultType="User">
select * from user where id = #{id}
</select>
<insert id="addUser" parameterType="User">
insert into user(id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="User">
update user set name =#{name},pwd=#{pwd} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id}
</delete>
</mapper>
7、然后还要在application.yml配置中绑定mapper.xml文件
#整合mybatis
mybatis:
type-aliases-package: com.wu.pojo
mapper-locations: classpath:com/wu/mapper/*.xml
8、maven配置资源过滤问题
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
9、编写User的Controller进行测试
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/queryUserList")
public List<User> queryUserList(){
List<User> list = userMapper.queryUserList();
return list;
}
@GetMapping("/queryUserById")
public User queryUserById(){
User user = userMapper.queryUserById(3);
return user;
}
@GetMapping("/addUser")
public String addUser(){
userMapper.addUser(new User(6, "小明", "12313"));
return "add-ok";
}
@GetMapping("/deleteUser")
public String deleteUser(){
userMapper.deleteUser(6);
return "delete-ok";
}
@GetMapping("/updateUser")
public String updateUser(){
userMapper.updateUser(new User(1, "小磊","12332"));
return "update-ok";
}
}
启动项目访问进行测试!