SpringBoot:整合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 所需要的依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
2、配置数据库连接信息(不变)
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mytest?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
type-aliases-package: cn.sxt.mybaits.pojo
mapper-locations: classpath:mybaits/mapper/*.xml
3、测试数据库是否连接成功!
4、创建实体类,导入 Lombok 完整的pom依赖
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!-- JDBC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- WEB-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
pojo包下的实体类(User )
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.Mapping;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
public Integer id;
public String username;
public String password;
}
5、创建mapper目录以及对应的 Mapper 接口(UserMapper )
package cn.sxt.mybaits.mapper;
import cn.sxt.mybaits.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.Mapping;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
List<User> queryUserList();
User findUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
6、对应的Mapper映射文件 (在resource文件夹下面建立一个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="cn.sxt.mybaits.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from user
</select>
<select id="findUserById" resultType="User">
select * from user where id=#{id}
</select>
<insert id="addUser" parameterType="User">
insert into user (id,username,password) values(#{id},#{username},#{password})
</insert>
<update id="updateUser" parameterType="User">
update user set username=#{username},password=#{password} where id=#{id}
</update>
<delete id="deleteUser" >
delete from user where id=#{id}
</delete>
</mapper>
7、maven配置资源过滤问题(在yaml文件中写,上面的yaml代码片已经都写好啦)
mybatis:
type-aliases-package: cn.sxt.mybaits.pojo
mapper-locations: classpath:mybaits/mapper/*.xml
8、编写用户的 UserController 进行测试!
package cn.sxt.mybaits.controller;
import cn.sxt.mybaits.mapper.UserMapper;
import cn.sxt.mybaits.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/queryUserList")
public List<User> queryUserList(){
List<User> users = userMapper.queryUserList();
return users;
}
@GetMapping("/addUser")
public String addUser(){
userMapper.addUser(new User(9,"sxt","12345"));
return "addOk";
}
@GetMapping("/updateUser")
public String updateUser(){
userMapper.updateUser(new User(9,"sxt1","123"));
return "updateOk";
}
@GetMapping ("/deleteUser")
public String deleteUser(){
userMapper.deleteUser(9);
return "deleteOk";
}
@GetMapping("/findUserById")
public User findUserById(){
User user = userMapper.findUserById(4);
return user;
}
}
一整个项目
|