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</ ...
随机推荐
- IAR 条件断点
条件断点是IDE的一个重要功能,在IAR调试时候,经常跟踪一个数据,但是对较大的buffer,用普通的断点或live watch都不好跟踪. 比如某个buffer里一个数,我们知道他在第几个,但是却从 ...
- CSS3图片悬停放大动画
在线演示 本地下载
- apache中配置php支持模块模式、cgi模式和fastcgi模式的实验
首先安装apache.mysql和php,依次顺序安装. 1.apache.mysql的安装比较简单,略过 2. php的安装,我安装的是php5.3.6内置了php-fpm,所以不需要再单独下补丁了 ...
- Hadoop 2.x简介
Hadoop 2.0产生背景 Hadoop1.0中HDFS和MapReduce在高可用.扩展性等方面存在问题 HDFS存在的问题 NameNode单点故障,难以应用于在线场景 NameNode压力过大 ...
- Linux学习之CentOS(一)--CentOS6.6下Mysql数据库的安装与配置
在这里我是通过yum来进行mysql数据库的安装的,通过这种方式进行安装,可以将跟mysql相关的一些服务.jar包都给我们安装好,所以省去了很多不必要的麻烦!!! [root@larry ~]# c ...
- Sqoop-1.4.4工具import和export使用详解
转自:http://blog.csdn.net/wodatoucai/article/details/46343291 Sqoop可以在HDFS/Hive和关系型数据库之间进行数据的导入导出,其中主要 ...
- org.eclipse.core.resources.bak文件导致MyEclipse每次关闭时无法保存文件
MyEclipse关闭时提示如下信息 Problems occurred while trying to save the state of the workbench. Internal Error ...
- spring.jar的下载地址
http://repo.spring.io/release/org/springframework/spring/
- python习题-替换敏感词
#3.有一个文件,里面有一些敏感词汇,如下,如果输入这些词,就用**代替,#然后输出,例如输入今天没吃饭,碰到一个傻逼,原来那个sb是小明.输出今天没吃饭,碰到一个**,原来那个**是小明.#需求分析 ...
- 关于android 图片加载优化
android应用对图片处理算是比较频繁的了,尤其是在程序加载大量图片和高分辨率图片时,最容易产生oom异常,下面是个人平时一些省内存加载方法 方法一: public Bitmap decodeFil ...