1.请参看github代码demo-orm-mybatis
文章目录- 一.简介
- 1.1 引入如下依赖项
- 1.2 yml配置如下(Hikari连接池配置详解!!!):
- 1.3 实体类User(lombok注解讲解!!!)
- 1.4 UserMapper接口(使用注解法或者使用xml编写sql!!!!!)
- 1.5 xml文件
- 1.6 编写测试类(方式一:使用junit4编写测试类!!!!)
- 1.7 编写测试类(方式二:使用junit5编写测试类!!!!)
- 1.8 遇到的问题
- 1.8.1 Spring boot 2.2.x spring-boot-starter-test包含JUnit5(包括用于向后兼容JUnit4的老式引擎),因此您可以运行JUnit4和JUnit5测试。如果已将测试迁移到JUnit 5,则应排除JUnit 4支持
此 demo 演示了 Spring Boot 如何与原生的 mybatis 整合,使用了 mybatis 官方提供的脚手架 mybatis-spring-boot-starter 可以很容易的和 Spring Boot 整合。
1.1 引入如下依赖项1.2 yml配置如下(Hikari连接池配置详解!!!):org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 mysql mysql-connector-java runtime org.projectlombok lombok true cn.hutool hutool-all 5.4.5 com.google.guava guava 29.0-jre
spring:
datasource:
url: jdbc:mysql://xxxxxxxx:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: xxxxxx
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
#Hikari连接池配置 ------> 详细配置请访问:https://github.com/brettwooldridge/HikariCP
hikari:
#此属性控制HikariCP尝试在池中维护的最小空闲连接数。如果空闲连接低于此值,并且池中的总连接数小于maximumPoolSize,HikariCP将尽最大努力快速高效地添加其他连接。但是,为了获得最佳性能和响应峰值需求,我们建议不要设置此值,而是允许HikariCP充当固定大小的连接池。默认值:与maximumPoolSize相同
minimum-idle: 5
#这是在从池向您提供连接之前执行的查询,以验证与数据库的连接是否仍处于活动状态
connection-test-query: SELECt 1 FROM DUAL
#此属性控制池允许达到的最大大小,包括空闲连接和正在使用的连接。基本上,该值将确定到数据库后端的实际连接的最大数量。
maximum-pool-size: 20
#此属性控制从池返回的连接的默认自动提交行为。
auto-commit: true
#此属性控制允许连接在池中处于空闲状态的最长时间。此设置仅适用于定义为小于maximumPoolSize的MinimumMidle。一旦池达到最小连接数,空闲连接将不会失效。
idle-timeout: 30000
#此属性表示连接池的用户定义名称,主要出现在日志和JMX管理控制台中,用于标识池和池配置。
pool-name: SpringBootDemoHikariCP
#此属性控制池中连接的最大生存期。正在使用的连接永远不会失效,只有当它关闭时才会被删除
max-lifetime: 60000
#此属性控制客户端(即您)等待池连接的最大毫秒数。如果在连接不可用的情况下超过此时间,将抛出SQLException。
connection-timeout: 30000
#logging.level设置某个包下日志输出级别
logging:
level:
com.mashirro: debug
mybatis:
configuration:
# 下划线转驼峰
map-underscore-to-camel-case: true
mapper-locations: classpath*:mapper
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User implements Serializable {
private static final long serialVersionUID = -1840831686851699943L;
private Long id;
private String name;
private String password;
private String salt;
private String email;
private String phoneNumber;
private Integer status;
private Date createTime;
private Date lastLoginTime;
private Date lastUpdateTime;
}
1.4 UserMapper接口(使用注解法或者使用xml编写sql!!!)
public interface UserMapper {
@Select("SELECT * FROM orm_user")
List selectAllUser();
int deleteById(@Param("id") Long id);
}
1.5 xml文件
1.6 编写测试类(方式一:使用junit4编写测试类!!!)DELETE FROM `orm_user` WHERe `id` = #{id}
(1)
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoOrmMybatisApplicationTests {
@Test
public void contextLoads() {
}
}
(2)
import cn.hutool.core.collection.CollUtil;
import com.mashirro.demoormmybatis.DemoOrmMybatisApplicationTests;
import com.mashirro.demoormmybatis.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@Slf4j
public class UserMapperTest extends DemoOrmMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void selectAllUser() {
List userList = userMapper.selectAllUser();
Assert.assertTrue(CollUtil.isNotEmpty(userList));
log.debug("【userList】= {}", userList);
}
@Test
public void deleteById() {
int i = userMapper.deleteById(1L);
Assert.assertEquals(1, i);
}
}
1.7 编写测试类(方式二:使用junit5编写测试类!!!)
import com.mashirro.demoormmybatis.entity.User;
import com.mashirro.demoormmybatis.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@Slf4j
@SpringBootTest
class DemoOrmMybatisApplicationTests {
@Test
void contextLoads() {
}
@Autowired
private UserMapper userMapper;
@Test
public void selectAllUser() {
List userList = userMapper.selectAllUser();
log.debug("【userList】= {}", userList);
}
@Test
public void deleteById() {
int i = userMapper.deleteById(1L);
}
}
1.8 遇到的问题
1.8.1 Spring boot 2.2.x spring-boot-starter-test包含JUnit5(包括用于向后兼容JUnit4的老式引擎),因此您可以运行JUnit4和JUnit5测试。如果已将测试迁移到JUnit 5,则应排除JUnit 4支持
org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine



