Mybatis-plus
MyBatis-Plus(简称 MP),是一个 MyBatis 的增强工具包,只做增强不做改变.
为简化开 发工作、提高生产率而生
简单的说就是可以对一些简单的SQL不需要进行编写。
Spring整合MybatisPlus
查看项目结构
导入依赖
<dependencies>
<!--Mybatis-plus下存在该两个jar文件-->
<!--<!–Mybati需要的jar文件–>-->
<!--<dependency>-->
<!--<groupId>org.mybatis</groupId>-->
<!--<artifactId>mybatis</artifactId>-->
<!--<version>3.4.5</version>-->
<!--</dependency>-->
<!--<!–mybatis和spring集成的依赖–>-->
<!--<dependency>-->
<!--<groupId>org.mybatis</groupId>-->
<!--<artifactId>mybatis-spring</artifactId>-->
<!--<version>1.3.1</version>-->
<!--</dependency>-->
<!--Mybati-plus需要的jar文件-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.2</version>
</dependency>
<!--数据库需要的jar文件-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--引入了SpringMVC依赖中包含了所有的Spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--Spring操作数据库,还需要spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
</dependencies>
通过Maven的项目jar管理,可以看到Mybatis-plus的jar文件存在Spring整合Mybatis需要的jar文件
编写JavaBean
对应数据库中表进行编写
编写Dao层
这一层可以看到我在接口中并没有写入任何方法
@Repository("userdao")
//通过集成BaseMapper<指定对应的javaBean>来进行Mybatis-plus的扩展功能
public interface UserDao extends BaseMapper<User> {
}
对应的SQL映射文件
<?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="cn.dao.UserDao">
</mapper>
编写Spring核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--指定扫描包-->
<!--只有扫描到的类 注解才会生效-->
<context:component-scan base-package="cn"/>
<!--使用ioc设置数据源-->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/usershow?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="ok"/>
</bean>
<!--指定的数据源放入 和Mybatis核心配置文件(虽然里面什么都没有写) -->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="classpath:MybatisConfig.xml"/>
</bean>
<!--扫描指定的 Mybatis位置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.dao"/>
</bean>
</beans>
最大的区别是在SQLSessionFactory的实现类使用了Mybatis-plus做为实现类
主测试类
package cn.SpringMybatisTest;
import cn.dao.UserDao;
import cn.pojo.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class SpringMybatisTest {
public static void main(String[] args) {
// 加载SPring配置文件,Spring配置文件中加载了Mybatis
// 将Mybatis的核心类都放入了Spring容器中
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringMybatis.xml");
// 获取指定的接口实现类
UserDao userdao = context.getBean("userdao", UserDao.class);
// 接口中不存在任何方法 ,但是继承了BaseMapper<JavaBean>
// 该类中存在很多方法可以使用 做到了不用编写SQL也可以进行数据库基本操作
List<User> users = userdao.selectList(null);
System.out.println(users.size());
for (User user : users) {
System.out.println("打印user的用户名:"+user.getCname());
}
}
}
结果伴随着Mybatis-plus的LOG日志
小结
Mybatis-plus只是对Mybatis的增强,并不会影响Mybatis的自己的操作。如果想要以往的,正常编写即可
通过上面看到主要改变就是 注解的更换,SQLsessionFactory的实现类改变
Mybatis-plus的使用
下面就是介绍Mybatis-plus的具体使用了
基本注解
@TableName
在JavaBean中一个类表示数据库中的一张表。
因为Mybatis-plus为我们提供了很多方便,不需要我们去编写sql,所以对JavaBean的要求会很高
约定大于配置大于编码:这就是框架
@TableName在javaBean中指定对应的数据表名字。
如果不写当JavaBean的名字与数据库名字不一致会出现异常
数据库对应展示
当JavaBean的类名对应不上数据表名字
public class UserBean {
private long id;
private String cname;
private String mobile;
private String qq;
private double money;
使用@TbaleName
@Component
@TableName("user")
public class UserBean {
private long id;
private String cname;
private String mobile;
private String qq;
private double money;
@TableField
与@TableName()相同,表示对应JaveBean中的属性对应数据表
观察源码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
public @interface TableField {
/**
* 数据库字段值,
* 不需要配置该值的情况:
* <li> 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 true 时,
* (mp下默认是true,mybatis默认是false), 数据库字段值.replace("_","").toUpperCase() == 实体属性名.toUpperCase() </li>
* <li> 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 false 时,
* 数据库字段值.toUpperCase() == 实体属性名.toUpperCase()</li>
*/
String value() default "";
/**
* 是否为数据库表字段
* 默认 true 存在,false 不存在
*/
boolean exist() default true;
Mybatis对于JavaBean的属性很存在对于下划线的自动转换操作。有的时候也需要避免自动转换通过@TableField进行指定
exist属性
该属性表示该属性是否存在于数据表中,因为在javaBean对应数据库中有的时候那避免放入表示作用的属性
@TableId
因为在数据库中存在 主键 主键存在很多特性 自增,不可重复。等
为了执行新增的时候Mybatis-plus可以进行主键自增。使用该注解。可以进行添加的时候不需要指定主键。
插入操作
int Inset()方法 | 判断传入的实体类非空的属性判断,不是非空的就进行sql编写 |
---|---|
这也就是我前面提到过的使用属性类型的时候最好使用包装类
int insertAllColumn() | 进行新增的时候无论是不是null,都会进行SQL的编写 |
---|---|
操作
@Component
@TableName("user")
public class UserBean {
@TableId(type= IdType.AUTO)
private long id;
private String cname;
private String mobile;
private String qq;
private double money;
主测试类
public class SpringMybatisTest {
public static void main(String[] args) {
// 加载SPring配置文件,Spring配置文件中加载了Mybatis
// 将Mybatis的核心类都放入了Spring容器中
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringMybatis.xml");
// 获取指定的接口实现类
UserDao userdao = context.getBean("userdao", UserDao.class);
// 通过IOC创建对象
UserBean user = context.getBean("user", UserBean.class);
user.setCname("qwe");
int insert = userdao.insert(user);
System.out.println("是否添加成功:"+insert);
}
}
结果展示
更新操作
方法 | 描述 |
---|---|
Integer updateById(@Param(“et”) T entity); | 将更新的对象放入,会根据对象的id进行对应属性重新进行赋值操作**(只会对存在值得属性进行赋值)** |
Integer updateAllColumnById(@Param(“et”) T entity) | 方法,无论是不是非空根据id去更新所有的列 |
使用的场景
- 如果我们想要修改的东西,有一些东西不想进行修改想要保留原来的值就是用 UpdateByid
- 如果想要全部统一修改,使用UpdateAllColumnById
// 通过IOC创建对象
UserBean user = context.getBean("user", UserBean.class);
user.setCname("asd");
user.setId(1);
int insert = userdao.updateById(user);
System.out.println("是否更新成功:"+insert);
删除操作
// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
查询操作
// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
参数说明
类型 | 参数名 | 描述 |
---|---|---|
Serializable | id | 主键ID |
Wrapper | queryWrapper | 实体对象封装操作类(可以为 null) |
Collection<? extends Serializable> | idList | 主键ID列表(不能为 null 以及 empty) |
Map<String, Object> | columnMap | 表字段 map 对象 |
IPage | page | 分页查询条件(可以为 RowBounds.DEFAULT) |
s.WRAPPER) Wrapper queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);
### 参数说明
| 类型 | 参数名 | 描述 |
| ---- | ---- | ---- |
|Serializable |id| 主键ID|
|Wrapper| queryWrapper| 实体对象封装操作类(可以为 null)|
|Collection<? extends Serializable> | idList| 主键ID列表(不能为 null 以及 empty)|
|Map<String, Object> | columnMap | 表字段 map 对象|
|IPage |page | 分页查询条件(可以为 RowBounds.DEFAULT)|