需求分析
通过使用 SpringBoot+MyBatis整合实现一个对数据库中的 users 表的 CRUD
创建工程
04_springboot_mybatis
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<groupId>com.by</groupId>
<artifactId>04_springboot_mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Mybatis 启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- mysql 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- 添加 junit 环境的 jar 包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</project>
application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3305/springboot
username: root
password:
type: com.alibaba.druid.pool.DruidDataSource
logging:
level:
com:
by:
mapper: DEBUG
启动类
@SpringBootApplication
@MapperScan("com.by.mapper") // @MapperScan 用户扫描MyBatis的Mapper接口
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
添加用户
数据表设计
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nam` varchar(255) DEFAULT NULL,
`sex` int(11) DEFAULT NULL,
`pwd` varchar(255) DEFAULT NULL,
`birth` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
pojo
public class User {
private Integer id;
private String nam;
private String pwd;
private Integer sex;
private Date birth;
//getter和setter方法省略
}
mapper
public interface UserMapper {
public void insertUser(User user);
}
<?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.by.mapper.UserMapper">
<insert id="insertUser" parameterType="user">
insert into user(nam,pwd,sex,birth) values(#{nam},#{pwd},#{sex},#{birth})
</insert>
</mapper>
service
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void addUser(User user) {
this.userMapper.insertUser(user);
}
}
junit
/**
* main方法:
* ApplicationContext ac=new
* ClassPathXmlApplicationContext("classpath:applicationContext.xml");
* junit与spring整合:
* @RunWith(SpringJUnit4ClassRunner.class):让junit与spring环境进行整合
* @Contextconfiguartion("classpath:applicationContext.xml")
* junit与SpringBoot整合:
* @RunWith(SpringJUnit4ClassRunner.class):让junit与spring环境进行整合
* @SpringBootTest(classes={App.class}):加载SpringBoot启动类。启动springBoot
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={App.class})
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testAddUser(){
User user = new User();
user.setId(1);
user.setNam("二狗");
user.setPwd("111");
user.setSex(1);
user.setBirth(new Date());
this.userService.addUser(user);
}
}