SpringBoot集成Mybatis(0配置注解版)
Mybatis初期使用比较麻烦,需要各种配置文件、实体类、dao层映射关联、还有一大推其它配置。当然Mybatis也发现了这种弊端,初期开发了generator可以根据表结构自动生成实体类、配置文件和dao层代码,可以减轻一部分开发量;后期也进行了大量的优化可以使用注解了,自动管理dao层和配置文件等,发展到最顶端就是今天要讲的这种模式,mybatis-spring-boot-starter就是springboot+mybatis完全注解不用任何配置文件,就可以简单配置轻松上手。
mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解解决一切问题,一种是简化后的老传统。
以注解版的方式进行讲解。
users表结构
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`username` varchar(32) DEFAULT NULL COMMENT '用户名',
`password` varchar(32) DEFAULT NULL COMMENT '密码',
`sex` varchar(32) DEFAULT NULL,
`nick_name` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`) USING HASH
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;
1、添加pom文件
<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.lynch.springboot</groupId>
<artifactId>spring-boot-mybatis-annotation</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mybatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、application.properties 添加相关配置
#mybatis.type-aliases-package=com.lynch.entity spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://192.168.1.149:3306/aa?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = attack
springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中,对于开发人员不用管,直接拿来使用即可。
在启动类中添加对mapper包扫描@MapperScan
package com.lynch; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("com.lynch.mapper")
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
或者直接在Mapper类上面添加注解@Mapper,建议使用上面那种,不然每个mapper加个注解也挺麻烦的。
3、开发Mapper
package com.lynch.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider; import com.lynch.entity.UserEntity;
import com.lynch.enums.SexEnum; public interface UserMapper { @Select("SELECT * FROM users")
@Results({ @Result(property = "sex", column = "sex", javaType = SexEnum.class),
@Result(property = "nickName", column = "nick_name") })
List<UserEntity> getAll(); @Select("SELECT * FROM users WHERE id = #{id}")
@Results({ @Result(property = "sex", column = "sex", javaType = SexEnum.class),
@Result(property = "nickName", column = "nick_name") })
UserEntity getOne(Long id); @Insert("insert into users(username,password,sex) values(#{username}, #{password}, #{sex})")
int insert(UserEntity user); @Update("UPDATE users SET userName=#{username},nick_name=#{nickName} WHERE id =#{id}")
void update(UserEntity user); @Delete("DELETE FROM users WHERE id =#{id}")
void delete(Long id); @InsertProvider(type=UserProvider.class, method = "batchInsert")
int batchInsert(@Param("userList")List<UserEntity> userList); @SelectProvider(type = UserProvider.class, method = "queryUser")
@Results({
@Result(property = "sex", column = "sex", javaType = SexEnum.class),
@Result(property = "nickName", column = "nick_name")
})
public List<UserEntity> queryUser(UserEntity user); @UpdateProvider(type = UserProvider.class, method = "updateUser")
public int updateUser(@Param("U")UserEntity user);
}
package com.lynch.mapper; import java.util.List;
import java.util.Map; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL; import com.github.pagehelper.util.StringUtil;
import com.lynch.entity.UserEntity; /**
* 利用@Provider实现动态SQL
*
* @author Lynch
*
*/
public class UserProvider {
public String queryUser(UserEntity user) {
StringBuffer sql = new StringBuffer("select * from users where 1=1 ");
if(StringUtil.isNotEmpty(user.getUsername())) {
sql.append(String.format("and username like '%s'", "%"+user.getUsername()+"%"));
} return sql.toString();
} public String batchInsert(Map map) {
List<UserEntity> userList = (List<UserEntity>)map.get("userList");
StringBuffer sql = new StringBuffer("insert into users (username,password) values "); for(UserEntity user : userList) {
sql.append(String.format("('%s', '%s'),", user.getUsername(), user.getPassword()));
} sql = sql.deleteCharAt(sql.length() -1);
System.out.println(sql.toString());
return sql.toString();
} public String updateUser(@Param("U")UserEntity user) {
SQL sql = new SQL(){{
UPDATE("users"); if (StringUtil.isNotEmpty(user.getNickName())){
SET("nick_name = #{U.nickName}");
} WHERE("id = #{U.id}");
}}; return sql.toString();
} }
注意,使用#符号和$符号的不同:
// This example creates a prepared statement, something like select * from teacher where name = ?;
@Select("Select * from teacher where name = #{name}")
Teacher selectTeachForGivenName(@Param("name") String name); // This example creates n inlined statement, something like select * from teacher where name = 'someName';
@Select("Select * from teacher where name = '${name}'")
Teacher selectTeachForGivenName(@Param("name") String name);
4、单元测试
package com.lynch.mapper; import java.util.ArrayList;
import java.util.List; 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; import com.lynch.entity.UserEntity;
import com.lynch.enums.SexEnum; @RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest { @Autowired
private UserMapper userMapper; @Test
public void insert() throws Exception {
int result = 0;
try {
result = userMapper.insert(new UserEntity("lisi", "123456", SexEnum.MAN));
} catch (Exception e) {
System.out.println(e.getMessage().indexOf("ConstraintViolationException"));
}
System.out.println("result=" + result);
} @Test
public void batchInsert() throws Exception {
List<UserEntity> userList = new ArrayList<UserEntity>();
userList.add(new UserEntity("a","a",SexEnum.MAN));
userList.add(new UserEntity("c","b",SexEnum.MAN));
userList.add(new UserEntity("b","b",SexEnum.WOMAN)); System.out.println("result=" + userMapper.batchInsert(userList));
} @Test
public void getAll() throws Exception {
List<UserEntity> users = userMapper.getAll();
System.out.println(users.toString());
} @Test
public void testUpdate() throws Exception {
UserEntity user = userMapper.getOne(45L);
System.out.println(user.toString());
user.setNickName("neo");
user.setSex(SexEnum.WOMAN);
userMapper.update(user);
} @Test
public void queryUser() throws Exception {
UserEntity user = new UserEntity();
user.setUsername("l");
System.out.println(userMapper.queryUser(user));
} @Test
public void updateUser() throws Exception {
UserEntity user = new UserEntity();
user.setId(45L);
user.setNickName("aaa");
System.out.println(userMapper.updateUser(user));
} }
SpringBoot集成Mybatis(0配置注解版)的更多相关文章
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- 0120 springboot集成Mybatis和代码生成器
在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- SpringBoot集成Mybatis配置动态数据源
很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ...
- SpringBoot集成MyBatis的Bean配置方式
SpringBoot集成MyBatis的Bean配置方式 SpringBoot是一款轻量级开发的框架,简化了很多原先的xml文件配置方式,接下来就介绍一下如何不适用XML来配置Mybatis spri ...
- SpringBoot 集成MyBatis 中的@MapperScan注解
SpringBoot 集成MyBatis 中的@MapperScan注解 2018年08月17日 11:41:02 文火慢炖 阅读数:398更多 个人分类: 环境搭建 在SpringBoot中集成My ...
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
- SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源
SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...
随机推荐
- JavaSE基础知识(3)—流程控制结构
一.顺序结构 1.说明 程序从上往下依次执行,中间没有任何跳转或选择2.特点 变量必须遵循 “前向引用” (局部变量必须先声明.赋值,然后再使用!) 二.分支结构(条件) 1.说明 程序从两条或多条路 ...
- tiny4412 --uboot移植(2) 点灯
开发环境:win10 64位 + VMware12 + Ubuntu14.04 32位 工具链:linaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-g ...
- vba文件对比并高亮显示
每月月底要和人事要离职人员名单,并账号列表里删除已经离职人员的账号,如下代码通过将账号列表与人事发来的离职清单进行对比,高亮找出离职人员的账号,并进行删除. Sub DeleteMain() Dim ...
- day 5,格式化输出,for,while, break,continue,列表
本节内容: 1,格式化输出 2,数据类型 3,for 循环 4,while 循环 5,列表 pycharm的简单使用,设置pycharm自动生成日期和计算机用户名 ctrl+d复制一行 1,格式化输出 ...
- unable to bind listening socket for address '127.0.0.1:9090': Address already in use (98)
unable to bind listening socket for address '127.0.0.1:9090': Address already in use (98) php-fpm 启动 ...
- js判断手机系统(Android或IOS),跳转相应下载地址
<script type="text/javascript"> $(document).ready(function(e) { var u = navigator.us ...
- centos7通过yum安装JDK1.8
安装之前先检查一下系统有没有自带open-jdk 命令: rpm -qa |grep java rpm -qa |grep jdk rpm -qa |grep gcj 如果没有输入信息表示没有安装. ...
- jdbc随笔
通过jdbc连接数据库的基本步骤: 导入jar包驱动类 jdbc语法:jdbc:子协议:厂商内容 对于mysql而言:jdbc:mysql://主机地址:端口号/库名 ...
- 安装zookeeper(单机,伪集群)
1.登陆zookeeper官网下载 https://zookeeper.apache.org/ zookeeper-3.4.8.tar.gz 解压:tar -zxvf zookeeper-3.4.8. ...
- MyBatis-Plus 多库部署方式;spring mvc 多库部署方式
1.实现mybatis-plus的多个数据库的切换方式 源码地址:https://github.com/baomidou/mybatisplus-spring-mvc 2.因为其文档都是相互依赖的,所 ...