接上一篇 SpringBoot学习01–创建项目,下面开始整合MyBatis
我们先看下项目的目录结构
一、步骤
1. 添加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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 将application.properties换成 application.yml
这里没有强制要求更换,只不过个人感觉 .yml
格式比 .properties
格式更方便查看
以下是application.yml
文件内容
# 配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
username: root
password: admin123
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# mybatis配置
mybatis:
mapper-locations: classpath:mappers/*.xml
type-aliases-package: com.example.demo.model
3. 新建数据库和账户表
CREATE DATABASE `demo` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `demo`;
DROP TABLE IF EXISTS `tb_account`;
CREATE TABLE `tb_account` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`password` varchar(45) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`last_edit_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
4. Dao(Mapper文件)
AccountMapper.java
package com.example.demo.dao;
import com.example.demo.model.Account;
import org.springframework.stereotype.Repository;
@Repository
public interface AccountMapper {
int insert(Account record);
}
5. AccountMapper.xml文件
AccountMapper.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.example.demo.dao.AccountMapper">
<insert id="insert" parameterType="com.example.demo.model.Account">
insert into tb_account (id, name, password,
create_time, last_edit_time)
values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{lastEditTime,jdbcType=TIMESTAMP})
</insert>
</mapper>
6. Service文件
Service接口 AccountService.java
package com.example.demo.service;
import com.example.demo.model.Account;
public interface AccountService {
int insert(Account record);
}
Service实现 AccountServiceImpl.java
package com.example.demo.service.Impl;
import com.example.demo.dao.AccountMapper;
import com.example.demo.model.Account;
import com.example.demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AccountServiceImpl implements AccountService{
@Autowired
private AccountMapper accountMapper;
@Override
public int insert(Account record) {
return accountMapper.insert(record);
}
}
7. 测试类
DemoApplicationTests.java
package com.example.demo;
import com.example.demo.dao.AccountMapper;
import com.example.demo.model.Account;
import com.example.demo.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
@MapperScan(annotationClass = Repository.class,basePackages = "com.example.demo.dao")
public class DemoApplicationTests {
@Autowired
private AccountService accountService;
@Test
public void insert() {
Account account = new Account();
account.setName("zorro");
account.setCreateTime(new Date());
account.setLastEditTime(new Date());
int result = accountService.insert(account);
System.out.println(result);
}
}
执行测试类的insert方法,可以看到如下运行结果
再看下数据库,数据成功插入
最后我们再看下目录结构
至此,简单的整合MyBatis完成。