Spring boot Mybatis 整合(注解版)
之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦。接口定义和映射离散在不同的文件中,阅读起来不是很方便。于是,准备使用mybatis的注解方式实现映射。如果喜欢xml方式的可以看我之前的博文:Spring boot Mybatis 整合(完整版)
个人开源项目
推荐开源项目
更多干货
源码
请前往文章末端查看
开发环境:
- 开发工具:Intellij IDEA 2017.1.3
- JDK : 1.8.0_101
- spring boot 版本 : 1.5.8.RELEASE
- maven : 3.3.9
拓展:
- springboot 整合 Mybatis 事务管理
开始
1.新建一个springboot项目:
添加依赖
2.看一下项目结构
3.完整依赖
<?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>com.winterchen</groupId>
<artifactId>springboot-mybatis-demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-mybatis-demo2</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
4.配置文件
因为习惯性的喜欢使用yml作为配置文件,所以将application.properties替换为application.yml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mytest
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
简单且简洁的完成了基本配置,下面看看我们是如何在这个基础下轻松使用Mybatis访问数据库的
使用Mybatis
- 在Mysql数据库中创建数据表:
CREATE DATABASE mytest;
USE mytest;
CREATE TABLE t_user(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL ,
password VARCHAR(255) NOT NULL ,
phone VARCHAR(255) NOT NULL
) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;
- 创建映射对象User
package com.winterchen.domain;
/**
* User实体映射类
* Created by Administrator on 2017/11/24.
*/
public class User {
private Integer id;
private String name;
private String password;
private String phone;
//省略 get 和 set ...
}
- 创建User映射的操作UserMapper,为了后续单元测试验证,实现插入和查询操作
package com.winterchen.mapper;
import com.winterchen.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* User映射类
* Created by Administrator on 2017/11/24.
*/
@Mapper
public interface UserMapper {
@Select("SELECT * FROM T_USER WHERE PHONE = #{phone}")
User findUserByPhone(@Param("phone") String phone);
@Insert("INSERT INTO T_USER(NAME, PASSWORD, PHONE) VALUES(#{name}, #{password}, #{phone})")
int insert(@Param("name") String name, @Param("password") String password, @Param("phone") String phone);
}
如果想了解更多Mybatis注解的详细:springboot中使用Mybatis注解配置详解
- 创建springboot 主类:
package com.winterchen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootMybatisDemo2Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisDemo2Application.class, args);
}
}
- 创建测试单元:
- 测试逻辑:插入一条name为"weinterchen"的User,然后根据user的phone进行查询,并判断user的name是否为"winterchen"。
package com.winterchen;
import com.winterchen.domain.User;
import com.winterchen.mapper.UserMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisDemo2ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void test(){
userMapper.insert("winterchen", "123456", "12345678910");
User u = userMapper.findUserByPhone("12345678910");
Assert.assertEquals("winterchen", u.getName());
}
}
- 测试结果
说明已经成功了。
**如果出现mapper注入不了的情况,请检查版本,当前博客的搭建方法只适合1.5.*版本的,如果你的版本是2.0以上的版本,请参照我的另一篇博客的mybatis的配置:springboot2.0整合mybatis **
事务管理(重要)
我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合。由于数据操作在顺序执行的过程中,任何一步操作都有可能发生异常,异常会导致后续操作无法完成,此时由于业务逻辑并未正确的完成,之前成功操作数据的并不可靠,需要在这种情况下进行回退。
为了测试的成功,请把测试的内容进行替换,因为之前测试的时候已经将数据生成了,重复的数据会对测试的结果有影响
@Test
@Transactional
public void test(){
userMapper.insert("张三", "123456", "18600000000");
int a = 1/0;
userMapper.insert("李四", "123456", "13500000000");
User u = userMapper.findUserByPhone("12345678910");
Assert.assertEquals("winterchen", u.getName());
}
只需要在需要事务管理的方法上添加 @Transactional 注解即可,然后我们启动测试,会发现异常之后,数据库中没有产生数据。
如果大家想对springboot事务管理有更加详细的了解,欢迎大家查看:springboot事务管理详解
源码:https://github.com/WinterChenS/springboot-mybatis-demo2/
springboot技术交流群:681513531
Spring boot Mybatis 整合(注解版)的更多相关文章
- Spring和MyBatis整合(注解版)
1.导入所需要的依赖 <!--MyBatis和Spring的整合包 由MyBatis提供--> <dependency> <groupId>org.mybatis& ...
- Spring boot Mybatis 整合(完整版)
个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...
- Spring boot Mybatis 整合
PS: 参考博客 PS: spring boot配置mybatis和事务管理 PS: Spring boot Mybatis 整合(完整版) 这篇博客里用到了怎样 生成 mybatis 插件来写程 ...
- Spring boot Mybatis整合构建Rest服务(超细版)
Springboot+ Mybatis+MySql整合构建Rest服务(涵盖增.删.改.查) 1.概要 1.1 为什么要使用Spring boot? 1.1.1 简单方便.配置少.整合了大多数框架 ...
- 详解spring boot mybatis全注解化
本文重点介绍spring boot mybatis 注解化的实例代码 1.pom.xml //引入mybatis <dependency> <groupId>org.mybat ...
- spring boot +mybatis 整合 连接数据库测试(从0到1)
spring boot 整合mybatis 1.打开idea创建一个项目 2.在弹出的窗口中选择spring initializr(初始化项目),点击next 3.接下来填写group 与artifa ...
- spring boot mybatis 整合教程
本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper 分页 ...
- Spring Boot Mybatis整合
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 特 ...
- Spring Boot 实战 —— MyBatis(注解版)使用方法
原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...
随机推荐
- 903. Valid Permutations for DI Sequence
We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...
- INSERT IGNORE 与INSERT INTO的区别,以及replace的用法
INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据. 这样就可以保 ...
- 10分钟教你用Python玩转微信之抓取好友个性签名制作词云
01 前言+展示 各位小伙伴我又来啦.今天带大家玩点好玩的东西,用Python抓取我们的微信好友个性签名,然后制作词云.怎样,有趣吧~好了,下面开始干活.我知道你们还是想先看看效果的. 后台登录: 词 ...
- mysql查询语句常用字段操作函数
一.concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为nu ...
- P2540 斗地主增强版
P2540斗地主增强版 参考大佬题解 思路:顺子暴力搜,剩下的牌我不会贪心所以用记忆化搜索(或者dp): 注意:双王不能当对,二不算顺子 代码 #include <cstdio> #inc ...
- java类的基本结构
对象依赖于类存在. 分析过程先有对象后有类,开发过程中先有类后有对象. new是为新建对象开辟内存空间的运算符:以类为模板,开辟空间实例化一个对象,并返回该对象的一个引用. 成员变量 直接在类中定义 ...
- 题目1010:A + B(字符串拆分)
问题来源 http://ac.jobdu.com/problem.php?pid=1010 问题描述 给我们一行标准的字符串,整行读入之后,把它拆开转换成数字进行计算. 问题分析 首先考虑一个问题:如 ...
- 3.2、Factorization Machine实践
1.在上一篇博客中我们构建度为二的因子分解机模型,这篇博客对这个模型进行实践 下图为准备的数据集: 完整代码为: # -*- coding: UTF-8 -*- # date:2018/6/6 # U ...
- 虚拟机上使用 opecnv 读取USB摄像头无法显示
使用opecv读取USB摄像头时候,无法显示图像. 设置 首先查看虚拟机Ubuntu检测摄像头是否已正常插入: ls /dev/video* 结果为: 设置虚拟机USB属性: USB的兼容性设置为US ...
- 读取Excel表格日期类型数据的时候
用POI读取Excel数据:(版本号:POI3.7) 1.读取Excel 2.Excel数据处理: Excel存储日期.时间均以数值类型进行存储,读取时POI先判断是是否是数值类型,再进行判断转化 1 ...