springboot系列(八)springboot整合mybatis
本篇介绍一下在springboot中整合mybatis ,使用mysql数据库,集成durid 连接池技术,全部代码是手动生成,没有使用代码生成器来构建代码。
一、创建数据库和表

二、在pom中添加依赖
<!-- 由于maven官方仓库在国外,本地更新很慢,建议使用中国的阿里仓库,在pom中添加如下配置 -->
<repositories><!-- 代码库 -->
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public//</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency> <!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency> <!-- Druid 数据连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
三、添加数据源配置文件 application-dev.yml(dev对应的是开发环境的配置文件)
1、application-dev.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootdb?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: 187664
type: com.alibaba.druid.pool.DruidDataSource
#连接池的配置信息
initialSize: 10
minIdle: 10
maxActive: 100
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
四、添加mybatis的配置文件
mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
</configuration>
五、在springboot核心配置文件中配置数据源和mybatis
application.yml
#数据源配置
spring:
profiles:
active: dev
# Mybatis配置
mybatis:
configLocation: classpath:mybatis.xml #mybatis配置文件路径
mapperLocations: classpath:mapper/**/*.xml #所有mapper映射文件地址
六、分别创建entity、dao、service、controller 、mapper等文件,先来看看文档结构

1、user实体类
package com.chuhouqi.demo.entity;
import java.io.Serializable;
public class User implements Serializable {
private String id;
private String name;
private String sex;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age='" + age + '\'' +
'}';
}
}
2、dao接口

3、UserService接口
package com.chuhouqi.demo.service;
import com.chuhouqi.demo.entity.User;
public interface IUserService {
public User getUser();
}
4、UserService接口的实现类
package com.chuhouqi.demo.service.impl; import com.chuhouqi.demo.dao.IUserDao;
import com.chuhouqi.demo.entity.User;
import com.chuhouqi.demo.service.IUserService; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service("userService")
public class UserServiceImpl implements IUserService { @Autowired
private IUserDao userDao; @Override
public User getUser() {
return userDao.getUser();
}
}
5、UserController类

6、UserMapper映射文件

七、启动项目测试

springboot系列(八)springboot整合mybatis的更多相关文章
- SpringBoot系列(五)Mybatis整合完整详细版
SpringBoot系列(五)Mybatis整合 目录 mybatis简介 项目创建 entity dao service serviceImpl mapper controller 1. Mybat ...
- Springboot 2.0.4 整合Mybatis出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
在使用Springboot 2.0.4 整合Mybatis的时候出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are require ...
- SpringBoot数据访问之整合mybatis注解版
SpringBoot数据访问之整合mybatis注解版 mybatis注解版: 贴心链接:Github 在网页下方,找到快速开始文档 上述链接方便读者查找. 通过快速开始文档,搭建环境: 创建数据库: ...
- Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查
之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...
- (入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)
1.配置tomcat数据源: # 数据源基本配置spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true ...
- SpringBoot学习- 3、整合MyBatis
SpringBoot学习足迹 1.下载安装一个Mysql数据库及管理工具,同类工具很多,随便找一个都可以,我在windows下做测试项目习惯使用的是haosql 它内部集成了MySql-Front管理 ...
- SpringBoot数据访问之整合Mybatis配置文件
环境搭建以及前置知识回顾 SpringBoot中有两种start的形式: 官方:spring-boot-starter-* 第三方:*-spring-boot-starter Mybatis属于第三方 ...
- Spring Boot2 系列教程 (十三) | 整合 MyBatis (XML 版)
前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,之前介绍过了 SpringBoot 整合MyBatis 注解版的使用,上一篇介绍过 MyBatis ...
- SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...
- springboot系列八、springboot整合kafka
背景: 当业务在同一时间出现高并发的时候,这个时候我们不想无限的增加服务器,但是又想提高吞吐量.这时可以考虑使用消息异步处理,进行消峰填谷:同时还可以降低耦合度.常见的消息中间件有kafka,rabb ...
随机推荐
- Swift4.0复习闭包
1.闭包的定义和调用: _ = { (param1: Int, param2: Float, param3: Void) -> return_type in // 闭包执行代码 /* ... * ...
- Python机器学习基础教程-第2章-监督学习之K近邻
前言 本系列教程基本就是摘抄<Python机器学习基础教程>中的例子内容. 为了便于跟踪和学习,本系列教程在Github上提供了jupyter notebook 版本: Github仓库: ...
- 线性链条件随机场(CRF)的原理与实现
基本原理 损失函数 (线性链)CRF通常用于序列标注任务,对于输入序列\(x\)和标签序列\(y\),定义匹配分数: \[ s(x,y) = \sum_{i=0}^l T(y_i, y_{i+1}) ...
- SUPPA 可变剪切分析
SUPPA是一款通过转录本定量来获取可变剪切定量结果的软件.转录本的定量方式有很多,例如count,FPKM, TPM等,作者建议使用TPM,因为先均一化了基因的长度,然后均一化了测序的深度.同时 ...
- vc编译器对 除法的优化
基本知识,7/2 和 6/2 在计算机中的商都为3.C语言的除法不等同于数学意义中的除法. C语言的除法.采用向零取整的方法. -______________0_______________+ 只有在 ...
- Python 模块初始化的时候,发生了什么?
假设有一个 hello.py 的模块,当我们从别的模块调用 hello.py 的时候,会发生什么呢? 方便起见,我们之间在 hello.py 的目录下使用 ipython 导入了. hello.py ...
- java多线程中篇(二) —— 线程的创建和Synchronized锁关键字
学习之前,先了解线程状态图 说明:线程共包括以下5种状态. 1. 新建状态(New) : 线程对象被创建后,就进入了新建状态.例如,Thread thread = new Thread ...
- 11 模块、模块的搜索顺序、__file__内置属性、__name__属性
模块的概念 一个python文件就是一个模块. 模块名同时也是一个标识符,需要符合标识符的命名规则. 在模块中定义的全局变量.函数.类 都是个外界提供的直接使用的工具. 模块就好比工具包,要想使用一个 ...
- PowerBuilder学习笔记之调用事件和函数
2.7.1调用事件和函数 完整语法:[ObjectName]ancestorclass::[type][when]name([argumnetlist]) 说明:ObjectName:指定函数或事件的 ...
- NIO--ByteBuf
Nio 的ByteBuffer 和 Netty 的 ByteBuf 的区别: 1.ByteBuf 将 ByteBuffer的position 分解为:readIndex , writeIndex 因 ...