文章目录
十二、整合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

12.1整合测试
- 1、导入 MyBatis 所需要的依赖
<!--mybatis-spring-boot整合-->
<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
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
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:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#整合mybatis(在mybatis中是在配置文件中写的,在SSM中是在spring的配置文件中写的)
mybatis:
type-aliases-package: com.wlw.pojo #别名
mapper-locations: classpath:mybatis/mapper/*.xml #注册mapper文件
-
3、测试数据库是否连接成功!
-
4、创建实体类,导入 Lombok依赖!
User.java
package com.wlw.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
private Boolean gender;
private Date regist_time;
}
-
5、创建mapper目录以及对应的 Mapper 接口
UserMapper.java
package com.wlw.mapper;
import com.wlw.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
//也可以使用@MapperScan(com.wlw.mapper) 在 Springboot04DataApplication类上使用
@Mapper //表示本类是一个 MyBatis 的Mapper类 (类似之前的UserDao接口)
@Repository // 注解生成bean,dao层专用
public interface UserMapper {
List<User> queryUserList();
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
-
6、对应的Mapper映射文件
UserMapper.xml.xml (src/main/resources/mybatis/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.wlw.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from t_user;
</select>
<select id="queryUserById" resultType="User">
select * from t_user where id = #{id}
</select>
<insert id="addUser" parameterType="User">
insert into t_user values (#{id},#{username},#{password},#{gender},#{regist_time});
</insert>
<update id="updateUser" parameterType="User">
update t_user set username=#{username}, password=#{password},gender=#{gender},regist_time=#{regist_time}
</update>
<delete id="deleteUser" parameterType="int">
delete from t_user where id = #{id}
</delete>
</mapper>
- 7、maven配置资源过滤问题
<build>
<!-- 更改maven编译规则,解决xxxMapper.xml存放在resources以外路径中的读取问题
如果xxxMapper.xml 还是存放在/resources/mybatis/mapper/目录下就可以不用配-->
<resources>
<resource>
<!-- 资源目录 -->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include><!-- 新添加 */代表1级目录 **/代表多级目录-->
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
- 8、编写部门的 UserController 进行测试!
package com.wlw.controller;
import com.wlw.mapper.UserMapper;
import com.wlw.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 {
@Autowired
private UserMapper userMapper;
@GetMapping("/userList")
public List<User> queryUserList(){
List<User> users = userMapper.queryUserList();
for (User user : users) {
System.out.println(user);
}
return users;
}
@GetMapping("/addUser")
public String addUser(){
int i = userMapper.addUser(new User(6,"mengyuan","123",true,null));
System.out.println("add--"+i);
return "ok";
}
@GetMapping("/updateUser")
public String updateUser(){
int i = userMapper.updateUser(new User(6,"mengyuan","852963",true,null));
System.out.println("update--"+i);
return "ok";
}
@GetMapping("/deleteUser")
public String deleteUser(){
int i = userMapper.deleteUser(6);
System.out.println("delete--"+i);
return "ok";
}
}
- 启动项目访问进行测试!
4262

被折叠的 条评论
为什么被折叠?



