spring+mybatis+mybatisplus
- 创建mapper接口,继承
BaseMapper<obj>
,obj为你需要操作的数据表 - 创建对应的实体类
- 配置数据源
4.设置配置文件
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context">
5.进行测试
springboot+mybatis+mp
1.创建mapper接口继承BaseMapper<obj>
2.创建实体类
3.配置数据源
4.配置log4j.properties
5.导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>//springboot起步依赖
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>//springboot测试依赖
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>//mysql驱动
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>//连接池
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>//mybatisplus
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>//日志
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
@TableIdtype (type=IdAUTO)设置数据库id增长的方式
1.@TableField(value=“字段名”),当数据库字段和实体的属性不匹配时
2.如果是实体中存在数据表中没有的属性,则用@TableDieled(exist=false)注解
3.如果想要保护某些数据,则@TableField(select=false),查询时不返回该字段
根据条件更新
User user=new User();
user.setName("赵四");
QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
userQueryWrapper.eq("name","赵4");
userMapper.update(user,userQueryWrapper);
#userQueryWrapper为自定义的条件对象
UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
userUpdateWrapper.set("name","xiaoming").set("job","学生").eq("name","朗12");
#这里的属性都是字段名,不是实体属性名
userMapper.update(null,userUpdateWrapper);
List<User> users = userMapper.selectList(null);
System.out.println(users);
根据多条件删除
QueryWrapper<User> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("name","赵四").eq("job","政教处主任");
int delete = userMapper.delete(queryWrapper);
` #推荐使用
User user=new User();
user.setName("扛把子");
user.setJob("学生");
QueryWrapper<User> userQueryWrapper=new QueryWrapper<>(user);
userMapper.delete(userQueryWrapper);`
根据id批量删除
根据id批量查询
mybatisplus的基本配置
1.mybatisplus的全局配置文件,在properties文件中—在springboot中
2.在springmvc中
当mp中的方法不能完成需求时
1.在mapper接口中添加需要的方法
2.在配置文件下,定义mapper.xml文件
3.指定xml文件的路径
别名扫描实体包,让mapper.xml文件的返回类型,只需写实体类名即可
配置文件中,全局id自增的策略
allEq的用法
1.通过Wrapper对象调用
2.方法调用
指定查询的字段
公共字段填充
当多张表有相同属性时,可以创建一个类,为所有的属性共同服务
实现步骤
-
在实体类的对应属性上添加@TableField
-
创建一个类编写元数据对象处理器,在此类中为公共字段赋值,通过实现MetaObjectHander接口来编写方法
-
重写插入填充和更新填充的方法
4.调用setValue
需要添加@Component,将对象交给容器管理
需要注意的是,如果这样操作是拿不到当前的sessionId的,只能通过ThreadLocal的方式
Thread是线程的意思,客户端每次发送的http请求,对应的服务端都会给定一个线程来处理,在涉及到以上案例的方法中,都属于一个线程
ThreadLocal不是一个线程,而是线程的局部变量,当使用ThreadLocal局部变量的线程,他会给每个线程提供一个独立副本,不会影响其他的线程,具有线程隔离的效果,线程外侧不能访问
结合以上,在公共字段自动填充中,想要获取当前的sessiId,需要使用到ThreadLocal
1.创建类
public class BaseContext {
private static ThreadLocal<Integer> threadLocal=new ThreadLocal<>();
public static void setThreadLocal(Integer integer)
{
threadLocal.set(integer);
}
public static Integer getThreadLocal()
{
return threadLocal.get();
}
}
2.在登录的时候,会有过滤器放行静态资源,可以在此获得sessionID,将值传给ThreadLocal对象
BaseContext(类名).setThreadLocal((Integer) employee);
3.在实现MetaObjectHandler公共字段填充的类中,获取想要的sessionId
BaseContext(类名).setThreadLocal((Integer) employee);