SpringBoot04 SpringBoot 和 MyBatis 整合
1 所需的jar包
mysql驱动包:mysql-connector-java
数据库链接池:druid
mybatis对应jar包:mybatis-spring-boot-starter
分页查询对应jar包:pagehelper
<?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> <groupId>cn.springBoot</groupId>
<artifactId>springBootProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springBootProject</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jpa,类似于连接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency> <!--mybatis相关-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<!--分页查询相关-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.1</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <dependencies>
<!-- spring热部署-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build> </project>
2 在数据库中创建一个表并在后台的实体层创建一个与之对应的实体类
package cn.springBoot.springBootProject.entity.test; /**
* Teacher实体类
*/
public class Teacher {
private Integer id;
private String name;
private String password; public Teacher() {
} public Teacher(Integer id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
Teacher实体类
3 创建一个持久层接口
package cn.springBoot.springBootProject.dao.test; import cn.springBoot.springBootProject.entity.test.Teacher;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository; import java.util.List; /**
* Teacher实体类对应的持久层接口
*/
@Repository
public interface TeacherMapper {
@Select("select * from sb_teacher")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "name", column = "name"),
@Result(property = "password", column = "password")
})
List<Teacher> findAllTeacher();
}
Teacher持久层接口
4 在配置文件中配置数据库链接信息

# 服务配置
server:
context-path: /devProject
port: 88 # 数据库配置
spring:
# datasource: # 配置数据库连接信息(利用默认的数据库链接池)
# driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf-8
# username: dev
# password: 182838 datasource: # 配置数据库链接信息(利用阿里巴巴提供的数据库连接池)
name: test
url: jdbc:mysql://127.0.0.1:3306/springboot
username: dev
password: 182838
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20 jpa: # 配置JPA
hibernate:
ddl-auto: update
show-sql: true # 上传文件配置
http:
multipart: # 配置文件上传大小
max-file-size: 50Mb
max-request-size: 50Mb # 日志配置
logging:
level:
root: INFO
org:
# springframework:
# web: DEBUG
hibernate: ERROR
file: e:\\demo\\demo.log mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
数据库配置信息
5 在启动中添加mapper类扫描路径

6 在dao层的接口中操作数据库,服务层调用dao层的相关方法去实现数据库操作

7 详情请参见这篇博客
SpringBoot04 SpringBoot 和 MyBatis 整合的更多相关文章
- SpringBoot与Mybatis整合方式01(源码分析)
前言:入职新公司,SpringBoot和Mybatis都被封装了一次,光用而不知道原理实在受不了,于是开始恶补源码,由于刚开始比较浅,存属娱乐,大神勿喷. 就如网上的流传的SpringBoot与Myb ...
- 30分钟带你了解Springboot与Mybatis整合最佳实践
前言:Springboot怎么使用想必也无需我多言,Mybitas作为实用性极强的ORM框架也深受广大开发人员喜爱,有关如何整合它们的文章在网络上随处可见.但是今天我会从实战的角度出发,谈谈我对二者结 ...
- SpringBoot+Shiro+mybatis整合实战
SpringBoot+Shiro+mybatis整合 1. 使用Springboot版本2.0.4 与shiro的版本 引入springboot和shiro依赖 <?xml version=&q ...
- Springboot与Mybatis整合
最近自己用springboot和mybatis做了整合,记录一下: 1.先导入用到的jar包 <dependency> <groupId>org.springframework ...
- SpringBoot系列——MyBatis整合
前言 MyBatis官网:http://www.mybatis.org/mybatis-3/zh/index.html 本文记录springboot与mybatis的整合实例:1.以注解方式:2.手写 ...
- SpringBoot与Mybatis整合实例详解
介绍 从Spring Boot项目名称中的Boot可以看出来,SpringBoot的作用在于创建和启动新的基于Spring框架的项目,它的目的是帮助开发人员很容易的创建出独立运行的产品和产品级别的基于 ...
- spring-boot、mybatis整合
一.MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 X ...
- springBoot和MyBatis整合中出现SpringBoot无法启动时处理方式
在springBoot和Myatis 整合中出现springBoot无法启动 并且报以下错误 Description: Field userMapper in cn.lijun.control ...
- springboot+Druid+mybatis整合
一.添加Druid.MySQL连接池.mybatis依赖 <!--整合Druid--> <dependency> <groupId>com.alibaba</ ...
随机推荐
- C# 半角?全角
/// <summary> /// 将资料表中已修改的资料行数据去左右空格.全角转半角 /// </summary> public sealed class FieldFitS ...
- Java多线程系列 JUC线程池06 线程池原理解析(五)
ScheduledThreadPoolExecutor解析 ScheduledThreadPoolExecutor适用于延时执行,或者周期性执行的任务调度,ScheduledThreadPoolExe ...
- 小学生都能看懂的数位dp
前言 数位dp其实很久前就知道了,也做过几道和其他算法混在一起的题目,其实通过手玩是能做的 但毕竟是种算法,还是系统学下比较好(节省手玩时间) 模板题 P2602 [ZJOI2010]数字计数 化简题 ...
- 20165101刘天野 2018-2019-2《网络对抗技术》Exp7 网络欺诈防范
目录 20165101刘天野 2018-2019-2<网络对抗技术>Exp7 网络欺诈防范 1.实验内容 1.1 简单应用SET工具建立冒名网站 1.2 ettercap DNS spoo ...
- PyVmomi Clone_VM with CustomizaitonSpec
调用CustomizaitonSpec来Clone VM ''' Created on 2017-09-03 @author: Vincen ''' from pyVmomi import vim f ...
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...
- CSS重置 reset.css
1. [文件] reset.css ~/*------------------------------------------* site:ifnoif.net* Style author:ifnoi ...
- 模拟C#的事件处理和属性语法糖
1. [代码]SharpEvent.hpp /* * SharpEvent.hpp * * Created on: 2014-5-5 * Author: leoking * Copyr ...
- Spring JdbcTemplate详解(转)
原文地址:http://www.cnblogs.com/caoyc/p/5630622.html 尊重原创,请访问原文地址 JdbcTemplate简介 Spring对数据库的操作在jdbc上面做 ...
- 简单使用XStream
Stream1.什么作用 *可以把JavaBean转换为(序列化为)xml2.XStream的jar *核心jar:xstream-1.4.7.jar *必须依赖包:xpp3_min-1.1.4c(X ...