springboot 整合Mybaites
上一节讲到springboot 如何使用druid数据源连接mysql并使用JPA进行CRUD操作,这一节继续关注持久化层,看看springboot 是如何整合Mybatise
00 新建一个maven java 项目,pom.xml 初始导入以下依赖:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot 热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--thymeleaf 模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--阿里数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!--mysql 依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.7</version>
</dependency>
</dependencies>
01 以上是前一节中所使用到的依赖,pom.xml 导入mybaties-springboot依赖:
<!--mybaties-springboot 依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
02 通过mybaties逆向工程生成实体类、dao,并移植到项目中:
person.jack
bean
Dept
DeptMapper.xml
Emp
EmpMapper.xml
dao
DeptMapper
EmpMapper
03 配置resources/application.properties,配置数据源
spring.thymeleaf.cache=false
logging.level.root=info
#### 配置默认数据源,并使用 阿里连接池 ###
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=true
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
######JPA 配置#####
# 声明数据库
spring.jpa.database=mysql
# 是否显示SQL语句
spring.jpa.show-sql = true
# Hibernate 自动DDL 操作
# create 每次加载hibernate时都会删除上一次的生成的表
# create-drop 每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除
# update 最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库)
spring.jpa.hibernate.ddl-auto=update
#配置方言
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
04 在application.properties 配置mybatis *Mapper.xml 文件的位置:
### mybaties 配置 ###
#*Mapper.xml文件位置
mybatis.mapper-locations=classpath:/person/jack/bean/*.xml
05 在启动类中使用@MapperScan 配置Mapper接口的位置:
@SpringBootApplication
/**配置 Mapper 接口的位置*/
@MapperScan("person.jack.dao")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
06 添加测试依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
07 编写测试类:
@RunWith(SpringJUnit4ClassRunner.class) /*添加SpringJUnit支持,引入Spring-Test框架*/
@SpringBootTest(classes = App.class) /*指定Springboot启动类启动*/
public class TestMybatis {
@Autowired
private EmpMapper empMapper;
@Test
public void test(){
Emp emp = empMapper.selectByPrimaryKey(7788);
System.out.println(emp);
}
}
08 重写实体类 Emp toString() 方法:
@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
'}';
}
09 运行测试方法test():
#后台打印
Emp{empno=7788, ename='SCOTT', job='ANALYST', mgr=7566, hiredate=Sun Apr 19 00:00:00 CDT 1987, sal=3000, comm=null, deptno=20}
# 主键为 7788 的员工成功查询,SpringBoot 成功整合Mybatis
10 Controller 测试,在EmpMapper.xml 中添加查询,对应添加到Dao:
#EmpMapper.xml
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from emp
</select>
#person/jack/dao/EmpMapper.java 添加抽象方法
List<Emp> selectAll();
11 编写 person/jack/controller/EmpController
@Controller
@RequestMapping("/emp")
public class EmpController {
@Autowired
private EmpMapper empMapper;
@RequestMapping("/empList")
public String empList(Map map){
map.put("empList", empMapper.selectAll());
return "empList";
}
}
12 编写empList.html 模板,使用th:each 迭代,#dates.format格式化输出日期:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>员工列表</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<td>序号</td>
<td>编号</td>
<td>姓名</td>
<td>工作</td>
<td>工资</td>
<td>奖金</td>
<td>入职日期</td>
</tr>
<tr th:each="emp,status:${empList}">
<td th:text="${status.count}"/>
<td th:text="${emp.empno} "/>
<td th:text="${emp.ename}"/>
<td th:text="${emp.job}"/>
<td th:text="${emp.sal}"/>
<td th:text="${emp.comm}"/>
<td th:text="${#dates.format(emp.hiredate, 'yyyy-MM-dd')}"/>
</tr>
</table>
</body>
</html>
13 浏览器访问:http://localhost:8080/emp/empList,页面打印
序号 | 编号 | 姓名 | 工作 | 工资 | 奖金 | 入职日期 |
---|---|---|---|---|---|---|
1 | 7369 | SMITH | CLERK | 800 | 1980-12-17 | |
2 | 7499 | ALLEN | SALESMAN | 1600 | 300 | 1981-02-20 |
3 | 7521 | WARD | SALESMAN | 1250 | 500 | 1981-02-22 |
4 | 7566 | JONES | MANAGER | 2975 | 1981-04-02 | |
5 | 7654 | MARTIN | SALESMAN | 1250 | 1400 | 1981-09-28 |
6 | 7698 | BLAKE | MANAGER | 2850 | 1981-05-01 | |
7 | 7782 | CLARK | MANAGER | 2450 | 1981-06-09 | |
8 | 7788 | SCOTT | ANALYST | 3000 | 1987-04-19 | |
9 | 7839 | KING | PRESIDENT | 5000 | 1981-11-17 | |
10 | 7844 | TURNER | SALESMAN | 1500 | 0 | 1981-09-08 |
11 | 7876 | ADAMS | CLERK | 1100 | 1987-05-23 | |
12 | 7900 | JAMES | CLERK | 950 | 1981-12-03 | |
13 | 7902 | FORD | ANALYST | 3000 | 1981-12-03 | |
14 | 7934 | MILLER | CLERK | 1300 | 1982-01-23 |
14 测试成功!
总结Springboot 整合mybaties 可分为以下几步:
- 导入依赖 mybatis-spring-boot-starter
- application.properties 配置 application.properties
- 在启动类中添加 @MapperScan 注解扫描Dao
- 注入Dao,使用!