整合MyBatis
官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
Maven仓库地址:https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter/2.1.1
整合测试
1、导入 MyBatis 所需要的依赖
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version>
</dependency>
2、配置数据库连接信息
spring:datasource:username: rootpassword: 123456#?serverTimezone=UTC解决时区的报错url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.cj.jdbc.Driver
3、测试数据库是否连接成功!
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>
</dependency>
4、创建实体类,导入 Lombok!
Department.java
package com.kuang.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {private Integer id;private String name;private String pwd;
}
5、创建mapper目录以及对应的 Mapper 接口
UserMapper.java
package com.kuang.mapper;//@Mapper : 表示本类是一个 MyBatis 的 Mapperimport com.kuang.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;import java.util.List;
@Mapper
@Repository
public interface UserMapper {List<User> queryUserList();User queryUserBuId(int id);int addUser(User user);int updateUser(User user);int deleteUser(int id);
}
配置
mybatis:type-aliases-package: com.kuang.pojomapper-locations: classpath:mybatis/mapper/*.xml
6、对应的Mapper映射文件
USerMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.kuang.mapper.UserMapper"><select id="queryUserList" resultType="User">select * from mybatis.user</select><select id="queryUserById" resultType="User">select * from mybatis.user where id=#{id}</select><insert id="addUser" parameterType="User">insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd})</insert><update id="updateUser" parameterType="User">update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}</update><delete id="deleteUser" parameterType="int">delete from mybatis.user where id=#{id}</delete>
</mapper>
7、maven配置资源过滤问题
<resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>true</filtering></resource>
</resources>
8、编写部门的 USeController 进行测试!
package com.kuang.controller;import com.kuang.mapper.UserMapper;
import com.kuang.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class UserController {@Autowiredprivate UserMapper userMapper;@GetMapping("/queryUserList")public List<User> queryUserList(){List<User> userList=userMapper.queryUserList();for (User user : userList) {System.out.println(user);}return userList;}@GetMapping("/1")public String getint(){return "asdfdas";}}
启动项目访问进行测试!
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zPL6iLMi-1610113790401)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20210108214929896.png)]