Mybatis-plus
为了简化mybatis开发而设计,对mybatis只做增强而不做改变
特性
- 无侵入:在项目中引入不会对现有项目产生影响
- 损耗小:启动即会自动自动注入基本的curd,
- 强大的crud操作:内置mapper,通过Service,仅仅使用少量配置即可完成简单的crud操作
- 支持lambda形式调用:可通过lambda表达式编写各种查询条件
- 支持主键自动生成
- 支持ActiveRecord模式:支持ActiveRecord形式调用,
- 支持自定义全局通用操作
- 内置代码生成器
- 内置分页插件:可基于mybatis物理分页
- 分页插件支持多种数据库
- 提供全局拦截器
jar包引入
spring-boot
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
spring MVC
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.5.2</version>
</dependency>
补充:mybatis-plus和mybatis两者会存在版本冲突问题
项目示例(Spring boot)
-
相关操作
-
相关注解
- @TableName(“str”) -->str为数据库中表名称
- @TableId() —>被修饰对象视为表中的主键
- @TableField() —>被修饰对象为表中非主键属性
-
mapper中可使用的方法
-
int insert(T entity)
-
int deleteById(Serializable id);
-
int deleteById(T entity);
-
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
-
eg: @Test
public void test03() {
Map<String, Object> columnMap = new HashMap<String, Object>();
columnMap.put("email","23@qq.com");
columnMap.put("age",30);
int i = userMapper.deleteByMap(columnMap);
System.out.println(i);
}
-
int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
-
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
-
int updateById(@Param(Constants.ENTITY) T entity);
-
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
-
List<T> selectBatchIds(@Param(Constants.COLL) Collection<? extends Serializable> idList);
-
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
-
default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
List<T> ts = this.selectList(queryWrapper);
if (CollectionUtils.isNotEmpty(ts)) {
if (ts.size() != 1) {
throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records");
}
return ts.get(0);
}
return null;
}
-
default boolean exists(Wrapper<T> queryWrapper) {
Long count = this.selectCount(queryWrapper);
return null != count && count > 0;
}
-
Long selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
-
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
-
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
-
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
-
<P extends IPage<T>> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
-
<P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
有关QueryWrapper的使用
代码生成器
添加依赖
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
代码构造器逻辑编写
AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("Nero");
gc.setOpen(false);
gc.setServiceName("%sService");
gc.setIdType(IdType.AUTO);
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/financial? serverTimezone=GMT%2B8&characterEncoding=utf-8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("******");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
PackageConfig pc = new PackageConfig();
pc.setParent("com.example.demo");
pc.setEntity("pojo");
mpg.setPackageInfo(pc);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setLogicDeleteFieldName("is_deleted");
strategy.setEntityBooleanColumnRemoveIsPrefix(true);
strategy.setRestControllerStyle(true);
mpg.setStrategy(strategy);
mpg.execute();
补充:AutoGenerator中共配置的GlobalConfig,DataSourceConfig,PackageConfig,StrategyConfig属性其他配置客通过查看源代码进行选择配置
配置文件编写
server:
port: 8080
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: ******
url: jdbc:mysql://localhost:3306/financial?serverTimezone=GMT%2B8&characterEncoding=utf-8
mybatis-plus:
mapper-locations: classpath*:com/example/demo/mapper/xml/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
在启动类中添加MapperScann()注解
@MapperScan("com.example.demo.mapper")
|