Mybatis Plus使用,条件拼装、自定义分页、多数据源
MyBatis-Plus(简称 MP)是一个MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。文档地址:https://mybatis.plus/guide/
一、spring-boot整合mybatis-plus (配置文档地址:https://mybatis.plus/guide/config.html,包含spring中的配置)
创建springboot项目,引入mybatis-plus的starter
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version>
</dependency><!--mysql数据库驱动-->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version>
</dependency>
springboot配置文件配置数据源
spring:datasource:url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTCdriverClassName: com.mysql.cj.jdbc.Driverusername: rootpassword: root
sql建表语句
CREATE TABLE `user` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',`name` varchar(30) DEFAULT NULL COMMENT '姓名',`age` int(11) DEFAULT NULL COMMENT '年龄',`u_email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '邮箱',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1364106346254659587 DEFAULT CHARSET=utf8;INSERT INTO `user` (`id`, `name`, `age`, `u_email`) VALUES ('1', 'name2', '13', '333@qq.com');
INSERT INTO `user` (`id`, `name`, `age`, `u_email`) VALUES ('2', 'Jack', '28', 'test2@baomidou.com');
INSERT INTO `user` (`id`, `name`, `age`, `u_email`) VALUES ('3', 'Tom', '18', 'test3@baomidou.com');
INSERT INTO `user` (`id`, `name`, `age`, `u_email`) VALUES ('4', 'Sandy', '31', 'test4@baomidou.com');
INSERT INTO `user` (`id`, `name`, `age`, `u_email`) VALUES ('5', 'Billie', '24', 'test5@baomidou.com');
INSERT INTO `user` (`id`, `name`, `age`, `u_email`) VALUES ('6', 'plus', '19', '1@qq.com');
创建Mapper继承BaseMapper
/*** BaseMapper里mybatis-plus封装了增删改方法*/
public interface UserMapper extends BaseMapper<User> {}
创建配置类,添加扫包注解,扫描mapper包
@Configuration
@MapperScan("com.example.demo.mapper")
public class MyBatisPlusConfig {}
编写测试类,测试查询:
@RunWith(SpringRunner.class)
@SpringBootTest
class DemoApplicationTests {@Resourceprivate UserMapper userMapper;@Testpublic void list() {List<User> list = userMapper.selectList(null);list.forEach(user -> System.out.println(user.toString()));}}
二、条件构造器AbstractWrapper
AbstractWrapper(抽象类)是QueryWrapper(查询)和UpdateWrapper(修改)的父类。QueryWrapper和UpdateWrapper条件拼接的方法一致。
QueryWrapper
@Test
public void query(){QueryWrapper<User> queryWrapper = new QueryWrapper<User>();Map<String,String> map = new HashMap<>();map.put("name","plus");map.put("age","18");map.put("u_email","1@qq.com");//allEq全部相等 传入map,key为数据库字段,val为对应的值 sql : name = 'plus' and age = '18' and u_email = '1@qq.com'//queryWrapper.allEq(map);//eq单独字段相等,可以用eq一直拼接字段条件 sql : name = 'Tom' and age = 18//queryWrapper.eq("name","Tom").eq("age","18");//ne不等于,无限ne sql : name != 'Tom'//queryWrapper.ne("name","Tom");//gt大于 sql : age > 18//queryWrapper.gt("age",18);//大于等于 sql : age >= 18//queryWrapper.ge("age",18);//lt小于 sql : age < 18//queryWrapper.lt("age",18);//le小于等于sql :age <= 18//queryWrapper.le("age",18);//between在18到28之间 18 <= age <= 28//queryWrapper.between("age",18,28);//notBetween (age < 18 or age > 28)//queryWrapper.notBetween("age",18,28);//模糊匹配 sql: name like '%lus%'//queryWrapper.like("name","lus");// name not like '%lus%'//queryWrapper.notLike("name","lus");//左匹配 sql : name like '%lus'//queryWrapper.likeLeft("name","lus");//右匹配 sql name like 'plu%'//queryWrapper.likeRight("name","plu");//sql : name is null//queryWrapper.isNull("name");//sql : name is not null//queryWrapper.isNotNull("name");//in 查询 : id in (1,2,3)//queryWrapper.in("id", Arrays.asList(1,2,3));//not in查询 sql: id not in (1,2,3)//queryWrapper.notIn("id", Arrays.asList(1,2,3));//条件在查询的数据中,id in (select id from user where id < 3)//queryWrapper.inSql("id","select id from user where id < 3");// id not in (select id from user where id < 3)//queryWrapper.notInSql("id","select id from user where id < 3");//group by age//queryWrapper.groupBy("age");// order by age asc//queryWrapper.orderByAsc("age");// order by age desc//queryWrapper.orderByDesc("age");//select 指定查询字段,having sum(age) > 18//queryWrapper.select("name").groupBy("name").having("sum(age) > 18");//func支持传入方法/* queryWrapper.func(w -> {if(false){w.eq("name","plus");}else {w.eq("name","Tom");}});*/// 相当于 name = 'plus' or (name = 'Tom' and age = '18')(默认都为and)//queryWrapper.eq("name","plus").or().eq("name","Tom").eq("age","18");//相当于 age = 18 or (name = 'plus' or name = 'Tom')/* queryWrapper.eq("age",18).or(i -> {i.eq("name","plus").or().eq("name","Tom");});*///相当于 id = 3 and (name = 'Tom' and age = '18') , and方法相当于里面小括号外加and/*queryWrapper.eq("id",3).and(i->{i.eq("name","Tom").eq("age",18);});*//* //正常嵌套 (name = 'plus' and age = '19')queryWrapper.nested(i->{i.eq("name","plus").eq("age",19);});*///用于数据库函数 sql : where LEFT(name,2) = 'To'//queryWrapper.apply("LEFT(name,2) = 'To'");//直接拼接sql后面queryWrapper.last("limit 1");List<User> list = userMapper.selectList(queryWrapper);list.forEach(System.out::println);
}
UpdateWrapper
@Test
public void update(){//修改,不仅会修改实体的内容,也会修改set方法里的内容,字段值设为null,set("u_email",null)User user = new User();user.setName("name");user.setAge(13);UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();updateWrapper.set("u_email","333@qq.com");updateWrapper.setSql("name = 'name2'");updateWrapper.eq("id",1);//userMapper.update(user,updateWrapper);userMapper.update(null,updateWrapper);
}
三、自定义sql
注解方式,直接定义方法,注解方式查询:
public interface UserMapper extends BaseMapper<User> {@Select("select * from user where id = #{id}")User findById(@Param("id")String id);/*** 映射xml文件方法* @param name* @return*/User findUserByName(String name);}
xml文件方式:
定义xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper"><resultMap id="BaseResultMap" type="com.example.demo.core.User"><id property="id" column="id"/><result property="name" column="name"/><result property="age" column="age"/><result property="uEmail" column="u_email"/></resultMap><select id="findUserByName" parameterType="java.lang.String" resultMap="BaseResultMap">select * from user where `name` = #{name}</select></mapper>
mybatis-plus配置:
# 如果xml文件在classpath下mapper文件夹下不配置也可以,如果不是就需要配上xml文件路径
mybatis-plus:mapper-locations: classpath*:mapper/*Mapper.xml
查询测试:
@Test
public void findById(){//注解方式User user = userMapper.findById("1");System.out.println(user.toString());
}@Test
public void findByName(){//xml文件方式User user = userMapper.findUserByName("plus");System.out.println(user.toString());
}
三、分页
添加分页插件,注册成bean
@Configuration
@MapperScan("com.example.demo.mapper")
public class MyBatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));return interceptor;}}
mapper接口和xml定义分页查询方法(普通的查询,mybatis-plus自动添加分页,按分页规则写查询方法)
//UserMapper的分页方法
IPage<User> findUserPage(Page<User> userPage);
<!--UserMapper.xml中的分页方法-->
<select id="findUserPage" resultMap="BaseResultMap">select * from `user`
</select>
分页测试代码
@Test
public void findPage(){int pageNo = 2; //页码int pageSize = 2; //每页数据条数Page<User> page = new Page<>(pageNo,pageSize);IPage<User> iPage = userMapper.findUserPage(page);System.out.println("数据总条数:" + iPage.getTotal());System.out.println("总页数:" + iPage.getPages());iPage.getRecords().forEach(System.out::println);
}
自定义分页类,带入对象信息查询:
/*** 创建Pager类,继承Page类,自定义condition字段接收查询条件对象*/
public class Pager<T> extends Page<T> {/*** 条件参数*/private T condition;public T getCondition() {return condition;}public void setCondition(T condition) {this.condition = condition;}public Pager(int pageNo, int pageSize){super(pageNo,pageSize);}}
UserMapper和UserMapper.xml
/*** 自定义Pager,传入查询条件(实体)* @param pager* @return*/
IPage<User> findPageByCondition(Pager<User> pager);
<select id="findPageByCondition" resultMap="BaseResultMap" parameterType="com.example.demo.page.Pager">select * from `user`<where><if test="condition != null"><if test="condition.name != null and condition.name != ''">and `name` = #{condition.name}</if><if test="condition.age != null">and age = #{condition.age}</if></if></where>
</select>
查询测试:
@Test
public void findPageByCondition(){//查询条件对象User user = new User();//user.setName("plus");user.setAge(19);int pageNo = 1; //页码int pageSize = 2; //每页数据条数Pager<User> pager = new Pager<>(pageNo,pageSize);pager.setCondition(user);IPage<User> iPage = userMapper.findPageByCondition(pager);System.out.println("数据总条数:" + iPage.getTotal());System.out.println("总页数:" + iPage.getPages());iPage.getRecords().forEach(System.out::println);
}
四、多数据源
1、引入dynamic-datasource-spring-boot-starter
dynamic-datasource-spring-boot-starter 是一个基于springboot的快速集成多数据源的启动器。
<dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.3.1</version>
</dependency>
2、配置文件
spring:datasource:dynamic:primary: master #默认数据源或数据组,默认为masterstrict: false #设置严格模式,默认false。启动未匹配到默认数据源会报错不会使用默认数据源datasource:master:url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTCusername: rootpassword: rootdriverClassName: com.mysql.cj.jdbc.Drivermaster_1:url: jdbc:mysql://190.168.65.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTCusername: rootpassword: rootdriverClassName: com.mysql.cj.jdbc.Driver
3、使用 @DS注解切换数据源。方法上的注解优先于类上的注解
//可用在类、接口和方法上
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DS {String value();
}
@Service
@DS("master_1")
public class UserServiceImpl implements UserService {@Resourceprivate UserMapper userMapper;@DS("master") //就近原则,方法上的注解优先于类上的注解@Overridepublic void insertUser(User user) {userMapper.insert(user);}
}
4、添加测试,修改数据源
@Test
public void insert(){User user = new User();user.setName("数据源");user.setAge(20);user.setUEmail("99@163.com");userService.insertUser(user);
}
Mybatis Plus使用,条件拼装、自定义分页、多数据源相关推荐
- yii2 mysql where in_Yii2查询之where条件拼装
熟悉Yii2的查询条件后,用Active Record查询数据非常方便. 以下我们介绍where()方法当中,条件的拼装方式. 1 语法 Yii2用where()方法(当然还有其他方法)来实现条件筛选 ...
- django-分页自带的分页-自定义分页
分页的本质 分页就是把查询集按照需求范围取出 例子 视图 >开始构建分页 >通过路径显示当前第几页 >用利分页器对象,生成当前页码所对应的数据,传前端 >下一页的制作 > ...
- DataList自定义分页
Asp.net里头,像DataList,Repeater,DataGrid,GridView,FormView包括Asp.net3.5中的ListView这几种服务器数据绑定控件,是在开发中经常需要用 ...
- ObjectDataSource自定义分页
ObjectDataSource是唯一支持自定义分页的数据源,要实现分页效果,首先要将ObjectDataSource.EnablePageing属性设为true,通过三个属性实现:StartRowI ...
- mybatis常见分页技术和自定义分页原理实战
文章目录 前言 mybatis简单了解 分页类型 分页方式 1.数组分页 2.数据库分页 3.Rowbounds分页 4.自定义插件分页 自定义分页原理 自定义分页实战 聊下第三方分页插件 pageH ...
- 使用FluentMybatis实现mybatis动态sql拼装和fluent api语法
开始第一个例子: Hello World 新建Java工程,设置maven依赖 新建maven工程,设置项目编译级别为Java8及以上,引入fluent mybatis依赖包. <depende ...
- PLSQ执行同样的sql,使用mybatis进行动态拼装执行的时候非常慢的问题解决
如题,项目中碰到了同样的sql,在plsql中执行很快,几乎秒出,但在程序中使用mybatis框架时,却非常的慢,前提是动态拼装的sql.在使用写死的参数,不会出现很慢的效果.最后发现是使用 #{xx ...
- 该服务器支持最多2100个参数,Mybatis批量查询拼装参数超长的解决办法
在使用Mybatis动态拼装查询参数时,执行过程中如果你的项目报错如下 Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 传入的请求具有过 ...
- Mybatis实现自定义分页插件
mybatis自定义分页插件,步骤如下[文章末尾关注公众号获取完整代码]: (1)环境搭建 创建一个maven工程,然后引入mybatis依赖和mysql依赖即可. <dependency> ...
最新文章
- 自然语言处理常用工具
- boost::spirit模块实现一个雇员结构的解析器的测试程序
- http 使用curl发起https请求
- python如何使用ppip安装xlwt_Python-xlwt库的基本使用
- 如何设置CentOS 7获取动态及静态IP地址
- csredis封装_在.NET Core中使用CSRedis
- iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 记录日志
- 头像+壁纸微信小程序源码
- 基于ADS仿真的465khz检波电路
- 网线分类及如何选择?
- Python开发【模块】:Urllib(二)
- Android 微信h5支付
- 庖丁解牛linux内核 百度云,庖丁解牛Linux内核-1
- pos机属于计算机系统吗,pos机与计算机有什么区别
- 论文阅读《Do Pre-trained Models Benefit Knowledge Graph Completion?A Reliable Evaluation and a Reasonab》
- 独立,做新世纪的好少年,无所畏惧
- java推送微信模板消息
- 微信下载手机版下载方式详解
- 浅析如何衡量程序员的生产效率
- php简历模板技能专长,php简历技能怎么写