Spring boot Mybatis 整合(注解版)
之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦。接口定义和映射离散在不同的文件中,阅读起来不是很方便。于是,准备使用mybatis的注解方式实现映射。如果喜欢xml方式的可以看我之前的博文:Spring boot Mybatis 整合(完整版)
个人开源项目
- springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页、个人作品、个人博客)
推荐开源项目
- 开源的springboot接口文档组件swagger2
更多干货
SpringBoot系列目录
源码
请前往文章末端查看
开发环境:
- 开发工具: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/mytestusername: rootpassword: rootdriver-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 {@Autowiredprivate UserMapper userMapper;@Testpublic 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@Transactionalpublic 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
转载于:https://www.cnblogs.com/winterchens/p/10655558.html
Spring boot Mybatis 整合(注解版)相关推荐
- Spring boot Mybatis 整合(完整版)
Spring boot Mybatis 整合(完整版) 更多干货 SpringBoot系列目录 正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: ...
- Spring boot Mybatis 整合
PS: 参考博客 PS: spring boot配置mybatis和事务管理 PS: Spring boot Mybatis 整合(完整版) 这篇博客里用到了怎样 生成 mybatis 插件来写程 ...
- spring boot+mybatis整合
LZ今天自己搭建了下Spring boot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多.其实只用Spring boot也可以开发,但是对于多表多条件分页查询,Sp ...
- spring boot mybatis 整合_MyBatis学习:MyBatis和Spring整合
1. 整合的工程结构 首先我们来看下整合之后的工程结构是什么样的. 2. 配置文件 在于spring整合之前,mybatis都是自己管理数据源的,然后sqlSessionFactory是我们自己去注入 ...
- Spring+Spring Boot+Mybatis框架注解解析
Restful 风格下的Spring Boot的注解开发 电商网站经常用到的restful风格 ,只是一种开发思想,不是开发框架,现在的技术并没有完全实现restful风格. restful风格是一种 ...
- spring boot mybatis 整合_两大热门框架 Spring 与 Mybatis 如何整合呢?
整合的方式 新建 maven 项目 引入依赖包 配置资源文件 案例实操 新建 maven 项目 新建 maven 项目 spring_mybatis 目录结构如下: 主目录包: com.xxx.d ...
- spring cloud spring boot mybatis构建java版 分布式微服务 b2b2c o2o电子商务云商平台
1. 涉及平台 平台管理.商家端(PC端.手机端).买家平台(H5/公众号.小程序.APP端(IOS/Android).微服务平台(业务服务) 2. 核心架构 Spring Cloud.Spring ...
- spring boot mybatis 整合_Spring、MyBatis和SpringMVC的整合
SSM框架整合的知识. 不用maven,为什么呢?主要是帮助更好的理解有哪些包,这样更加透彻.当然了,使用maven会更方便一点. 1 jar包管理 2 整合思路 spring在进行管理时,是很有条理 ...
- 详解spring boot mybatis全注解化
https://www.cnblogs.com/goloving/p/9125948.html https://www.jianshu.com/p/8cffcc105b3a https://blog. ...
最新文章
- Zookeeper 的典型应用场景场景
- AdventureWorksBI.msi 和 AdventureWorksDB.msi 的官方下载地址及安装方法
- 网络加速_BWS2020:加速网络自治,使能敏捷商业
- LeetCode Number of Digit One(计算1的个数)
- 微信公众号文章质量评分算法详解
- python学习:语句
- JavaWeb课程复习资料——中文乱码上下文处理
- rman 备份后恢复整个数据库文件的操作
- 微信小程序中 tabbar的icon图标格式大小
- 十字路口红绿灯plc程序_PLC编程-典型案例红绿灯控制
- vSphere 故障排除之工具篇
- 虚拟化--vsphere排错组件归纳
- 整数、区间与区间端点
- MySQL-第九篇分组和组函数
- 【优化算法】原子搜索优化算法(ASO)【含Matlab源码 1541期】
- java记账软件开发_Java项目之家庭记账软件
- 虚拟机xp系统如何安装--win10专业版
- 手机停机后你们知道怎么打电话?教你鲜为人知的手机锦囊
- 【积跬步以至千里】Markdownpad2报错: Html Rendering Error:An error occurred with the HTML rendering component。
- 【matlab】输入一字符串,字母大写变小写,小写变大写。
热门文章
- Mac入门--如何使用brew安装多个PHP版本
- python操作数据库
- JavaScript GetAbsoultURl
- [转]hadoop新手错误解决方法
- K-Means算法Demo
- Oracle ——概述 Oracle 5 步调优方法论
- windows下命令行修改系统时间;修改系统时间的软件
- 网上找工作秘籍(3)
- c语言有一个正整数加上100,c语言编程实现:一个整数,它加上100后是完全平方数,再加168又是完全平方数,求该数。...
- cmd进入python环境_python2和python3同时存在,如何CMD中进入不同的环境