<dependency><groupId>com.baomidougroupId><artifactId>mybatis-plus-boot-starterartifactId><version>3.2.0version>dependency>
mysql mysql-connector-java runtime com.alibaba druid 1.1.19
@SpringBootApplication@MapperScan("com.baizhi.dao")public class MybatisApplication {public static void main(String[] args) {SpringApplication.run(MybatisApplication.class, args);}}
spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/mybatis-plus?characterEncoding=UTF-8spring.datasource.username=rootspring.datasource.password=rootlogging.level.root=infologging.level.com.baizhi.dao=debug
DROP TABLE IF EXISTS `user`;CREATE TABLE `user` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,`age` int(11) DEFAULT NULL,`bir` timestamp NULL DEFAULT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;SET FOREIGN_KEY_CHECKS = 1;
@Data //lombok的注解用来生成get set 等相关方法public class User {private String id;private String name;private Integer age;private Date bir;}
import com.baomidou.mybatisplus.core.mapper.BaseMapper; //提供了各种CRUD方法public interface UserDAO extends BaseMapper<User> {}
@Autowiredprivate UserDAO userDAO;@Testvoid contextLoads() {List<User> users = userDAO.selectList(null);System.out.println(users);}
属性
|
类型
|
必须指定
|
默认值
|
描述
|
value
|
String
|
否
|
""
|
表名
|
schema
|
String
|
否
|
""
|
schema
|
keepGlobalPrefix
|
boolean
|
否
|
false
|
是否保持使用全局的 tablePrefix 的值(当全局 tablePrefix 生效时)
|
resultMap
|
String
|
否
|
""
|
xml 中 resultMap 的 id(用于满足特定类型的实体类对象绑定)
|
autoResultMap
|
boolean
|
否
|
false
|
是否自动构建 resultMap 并使用(如果设置 resultMap 则不会进行 resultMap 的自动构建与注入)
|
excludeProperty
|
String[]
|
否
|
{}
|
需要排除的属性名 @since 3.3.1
|
属性
|
类型
|
必须指定
|
默认值
|
描述
|
value
|
String
|
否
|
""
|
数据库字段名
|
exist
|
boolean
|
否
|
true
|
是否为数据库表字段
|
condition
|
String
|
否
|
""
|
字段 where 实体查询比较条件,有值设置则按设置的值为准,没有则为默认全局的 %s=#{%s}
|
update
|
String
|
否
|
""
|
字段 update set 部分注入,例如:当在version字段上注解update="%s+1" 表示更新时会 set version=version+1 (该属性优先级高于 el 属性)
|
insertStrategy
|
Enum
|
否
|
FieldStrategy.DEFAULT
|
举例:NOT_NULL
insert into table_a( |
updateStrategy
|
Enum
|
否
|
FieldStrategy.DEFAULT
|
举例:IGNORED
update table_a set column=#{columnProperty}
|
whereStrategy
|
Enum
|
否
|
FieldStrategy.DEFAULT
|
举例:NOT_EMPTY
where |
fill
|
Enum
|
否
|
FieldFill.DEFAULT
|
字段自动填充策略
|
select
|
boolean
|
否
|
true
|
是否进行 select 查询
|
keepGlobalFormat
|
boolean
|
否
|
false
|
是否保持使用全局的 format 进行处理
|
jdbcType
|
JdbcType
|
否
|
JdbcType.UNDEFINED
|
JDBC 类型 (该默认值不代表会按照该值生效)
|
typeHandler
|
Class extends TypeHandler>
|
否
|
UnknownTypeHandler.class
|
类型处理器 (该默认值不代表会按照该值生效)
|
numericScale
|
String
|
否
|
""
|
指定小数点后保留的位数
|
@Testpublic void testFindAll(){List<User> users = userDAO.selectList(null);users.forEach(user-> System.out.println("user = " + user));}
@Testpublic void testFindOne(){User user = userDAO.selectById("1");System.out.println("user = " + user);}
//条件查询@Testpublic void testFind(){QueryWrapper<User> queryWrapper = new QueryWrapper<>();//queryWrapper.eq("age",23);//设置等值查询//queryWrapper.lt("age",23);//设置小于查询//queryWrapper.ge("age",23);//小于等于查询 gt 大于 ge 大于等于List<User> users = userDAO.selectList(queryWrapper);users.forEach(user-> System.out.println(user));}
@Testpublic void testFindAll(){QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.likeRight("username","小");List<User> users = userDAO.selectList(queryWrapper);users.forEach(user-> System.out.println("user = " + user));}
@Testpublic void testSave(){User entity = new User();entity.setAge(23).setName("小明明").setBir(new Date());userDAO.insert(entity);}
@Testpublic void testUpdateById(){User user = userDAO.selectById("1");user.setAge(24);userDAO.updateById(user);}
@Testpublic void testUpdate(){User user = userDAO.selectById("1");user.setName("小陈陈");QueryWrapper<User> updateWrapper = new QueryWrapper<>();updateWrapper.eq(true,"age",23);userDAO.update(user, updateWrapper);}
@Testpublic void testDeleteById(){userDAO.deleteById("3");}
@Testpublic void testDelete(){QueryWrapper<User> wrapper = new QueryWrapper<>();wrapper.lambda().eq(true,User::getAge,23);userDAO.delete(wrapper);}
@EnableTransactionManagement@Configuration@MapperScan("com.baizhi.dao")public class MybatisPlusConfig {@Beanpublic PaginationInterceptor paginationInterceptor() {PaginationInterceptor paginationInterceptor = new PaginationInterceptor();return paginationInterceptor;}}
@Testpublic void testFindAll(){IPage<User> page = new Page<>(1,2);page = userDAO.selectPage(page, null);page.getRecords().forEach(user -> System.out.println("user = " + user));}
@Testpublic void testFindAll(){QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.eq("age",23);IPage<User> page = new Page<>(1,2);page = userDAO.selectPage(page, queryWrapper);page.getRecords().forEach(user-> System.out.println("user = " + user));}
//逻辑删除@TableLogicprivate int deleted;
package com.kuang.config;import com.baomidou.mybatisplus.core.injector.ISqlInjector;import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;import org.springframework.context.annotation.*;@Configurationpublic class MyDeletedConfig {@Beanpublic ISqlInjector sqlInjector(){return new LogicSqlInjector();}}
mybatis-plus:global-config:db-config:logic-delete-value: 1 # 逻辑已删除值(默认为 1)logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
//根据id删除@Testvoid deleteById(){int i = userMapper.deleteById(4L);System.out.println(i);}
<dependency><groupId>com.baomidougroupId><artifactId>dynamic-datasource-spring-boot-starterartifactId><version>3.0.0version>dependency>
spring.datasource.primary=master #指定默认数据源spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.jdbc.Driverspring.datasource.dynamic.datasource.master.url=jdbc:mysql://localhost:3306/mybatis-plus?characterEncoding=UTF-8spring.datasource.dynamic.datasource.master.username=rootspring.datasource.dynamic.datasource.master.password=rootspring.datasource.dynamic.datasource.slave_1.driver-class-name=com.mysql.jdbc.Driverspring.datasource.dynamic.datasource.slave_1.url=jdbc:mysql://localhost:3306/mybatis-plus-1?characterEncoding=UTF-8spring.datasource.dynamic.datasource.slave_1.username=rootspring.datasource.dynamic.datasource.slave_1.password=root
public interface UserService{List<User> findAll();void save(User user);}
@Service@Transactionalpublic class UserServiceImpl implements UserService {@Autowiredprivate UserDAO userDAO;@Overridepublic List<User> findAll() {return userDAO.selectList(null);}@Overridepublic void save(User user) {userDAO.insert(user);}}
package com.baizhi;import com.baizhi.entity.User;import com.baizhi.service.UserService;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import java.util.Date;@SpringBootTestpublic class TestUserService {@Autowiredprivate UserService userService;@Testpublic void testFindAll(){userService.findAll().forEach(user-> System.out.println("user = " + user));}@Testpublic void testSave(){User user = new User();user.setName("aaa").setAge(23).setBir(new Date());userService.save(user);}}