Spring Boot MyBatis
MyBatis简介
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache 迁移到了google code,并且改名为MyBatis 。
集成spring boot 的时候必须在mapper接口上面标注@Mapper注解
项目图片
pom.xml
只需要在pom.xml引入需要的数据库配置,就会自动访问此数据库,如果需要配置其他数据库,可以在application.properties进行添加
默认使用org.apache.tomcat.jdbc.pool.DataSource创建连接池
<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.jege.spring.boot</groupId><artifactId>spring-boot-mybatis</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>spring-boot-mybatis</name><url>http://maven.apache.org</url><!-- 公共spring-boot配置,下面依赖jar文件不用在写版本号 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.1.RELEASE</version><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><!-- 持久层 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><!-- h2内存数据库 --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><finalName>spring-boot-mybatis</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin></plugins></build>
</project>
模型对象User
package com.jege.spring.boot.mybatis.entity;/*** 模型对象*/
public class User {private Long id;private String name;private Integer age;public User() {}public User(String name, Integer age) {this.name = name;this.age = age;}
}
持久层UserMapper
package com.jege.spring.boot.mybatis.mapper;import java.util.List;import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import com.jege.spring.boot.mybatis.entity.User;/*** 持久层接口,由spring自动生成其实现*/
@Mapper
public interface UserMapper {@Delete("drop table t_user if exists")void dropTable();@Insert("create table t_user (id bigint generated by default as identity, age integer, name varchar(255), primary key (id))")void createTable();@Insert("insert into t_user(name,age) values(#{name},#{age})")void insert(User user);@Select("select id,name,age from t_user")List<User> findAll();@Select("select id,name,age from t_user where name like #{name}")List<User> findByNameLike(String name);@Delete("delete from t_user")void deleteAll();}
无需application.properties
测试类UserMapperTest
package com.jege.spring.boot.mybatis;import static org.assertj.core.api.Assertions.assertThat;import org.junit.After;
import org.junit.Before;
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.SpringJUnit4ClassRunner;import com.jege.spring.boot.mybatis.entity.User;
import com.jege.spring.boot.mybatis.mapper.UserMapper;@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
public class UserMapperTest {@AutowiredUserMapper userMapper;// 每次执行Test之前先删除表,创建表@Beforepublic void before() throws Exception {userMapper.dropTable();userMapper.createTable();}// 打印出class com.sun.proxy.$Proxy66表示spring注入通过jdk动态代理获取接口的子类@Testpublic void proxy() throws Exception {System.out.println(userMapper.getClass());}@Testpublic void save() throws Exception {for (int i = 0; i < 10; i++) {User user = new User("jege" + i, 25 + i);userMapper.insert(user);}}@Testpublic void all() throws Exception {save();assertThat(userMapper.findAll()).hasSize(10);}@Testpublic void findByName() throws Exception {save();assertThat(userMapper.findByNameLike("jege%")).hasSize(10);}// 每次执行Test之后清空数据@Afterpublic void destroy() throws Exception {userMapper.deleteAll();}}
扫一扫获取更多相关资讯哟!!!
Spring Boot MyBatis相关推荐
- Eclipse + Spring boot +mybatis + mysql
Eclipse + Spring boot +mybatis + mysql 如题.使用Springboot 2.0 版本进行网页的开发.原理和优点很多博文已经讲过了,这里不再赘述.但是很多项目按照他 ...
- spring boot+mybatis整合
LZ今天自己搭建了下Spring boot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多.其实只用Spring boot也可以开发,但是对于多表多条件分页查询,Sp ...
- Spring Boot + Mybatis 实现动态数据源
动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...
- Spring boot Mybatis 整合(注解版)
之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...
- 在Spring Boot + Mybatis 中,使用@Repository失效
在Spring Boot + Mybatis 中,使用@Repository失效 在springboot 中,给mapper的接口上加上@Repository,无法生成相应的bean,从而无法@Aut ...
- Spring Boot + Mybatis 快速整合
引言 最近在工作结束后抽时间学习了一下mybatis的知识,因为之前有学习过,但是经久不用,也未曾踏实地整理,因此有所淡忘. super meeting会议管理系统是我厂最近开发的一套会议预约平台.持 ...
- spring boot + mybatis + layui + shiro后台权限管理系统
后台管理系统 版本更新 后续版本更新内容 链接入口: springboot + shiro之登录人数限制.登录判断重定向.session时间设置:https://blog.51cto.com/wyai ...
- 从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建
从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建 本文简介 为什么使用Spring Boot 搭建怎样一个环境 开发环境 导入快速启动 ...
- Spring boot Mybatis 整合(完整版)
Spring boot Mybatis 整合(完整版) 更多干货 SpringBoot系列目录 正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: ...
最新文章
- 【PC工具】更新免费文库文档下载器,破解文库下载器,免费下载文库文档
- Matlab变量及其命名规则
- 74款app源码,值得你拥有的干货
- 二维burgers方程_二维Burgers方程的RKDG有限元解法
- 用计算机画 信息技术课标要求,[引用]小学信息技术课程标准
- 同步异步和阻塞3-同步阻塞
- 华为发布了其自研的鸿蒙操作系统,官宣!鸿蒙手机操作系统即将发布
- 【COS】函数使用技巧
- 关闭ntp服务的 monitor monlist,解决漏洞CVE-2013-5211
- c++ 优先队列(priority_queue)
- 内后视镜和外后视镜哪个显示真实距离?
- SqlSession的使用范围
- 人工智能在减灾遥感中的应用
- 基于JSP微信小程序汽车票订票售票系统设计与实现
- 存量房贷利率,一种简单估算其自然年利率调整的方法。
- C++实现Python变量
- 【能效管理】关于学校预付费水电系统云平台应用分析介绍
- 计算机主板维修高手,计算机电路基础与维修高手
- NVIDIA支持CUDA的显卡选型简述
- 360浏览器保存网页html,如何设置360浏览器网页保存类型默认为html
热门文章
- beego 例子_beego框架代码分析
- maven jar包冲突
- NodeJs express自定义中间件
- java guava限流,Guava的RateLimiter实现接口限流
- Mybatis源码分析之(四)mapper访问数据库的底层原理(代理方法中具体访问数据库的细节)
- 学习Spring Boot:(十九)Shiro 中使用缓存
- oracle数据库中索引会失效的几种情况
- mysql5.7.14安装版_MySql5.7.14安装教程详解(解压版)_MySQL
- html超链接使用d,HTML图像的调用和超链接
- 递推算法之滚动数组思维方式