第一步:加入mybatis-plus依赖
第二步:配置数据源
spring:
thymeleaf:
cache: false
encoding: utf-8
prefix: classpath:/templates/
suffix: .html
enabled: true
datasource:
url: jdbc:mysql://192.168.1.152:3306/timo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
第三步:创建两张有关联关系的表(我的如下 )
sys_user:用户表
sys_role:权限表
第四步:将两张表映射到实体类bean(其实sys_role建不建都无所谓,可以不建sys_role的实体类)
@Data
@TableName("sys_user") //当实体类名(前提是开启驼峰)与数据库表名一致时可不写
public class SysUser extends Model {
@TableId(type = IdType.AUTO)
private Long id;
private String username;
private String password;
private String salt;
private Integer deptId;
private String picture;
private String sex;
private String email;
private String phone;
@Version
private Integer version;
@TableField(fill = FieldFill.INSERT)
private Date createDate;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateDate;
private Integer status;
********************不想建这个实体类就不建 没有意义
public class SysRole {
private long id;
private String roleName;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
}
第五步:定义mapper(注意看这个@Select里面的SQL)
第六步:定义一个通用实体类Record
import com.alibaba.druid.proxy.jdbc.ClobProxyImpl;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.Reader;
import java.util.*;
/**
* @author Wxiaokun
* @version 1.0
* @date 2020/9/25 0025 下午 4:15
*/
public class Record extends HashMap implements Map {
private static final long serialVersionUID = 1L;
Map map = null;
HttpServletRequest request;
public Record(HttpServletRequest request){
this.request = request;
Map properties = request.getParameterMap();
Map returnMap = new HashMap();
Iterator entries = properties.entrySet().iterator();
Map.Entry entry;
String name = "";
String value = "";
while (entries.hasNext()) {
entry = (Map.Entry) entries.next();
name = (String) entry.getKey();
Object valueObj = entry.getValue();
if(null == valueObj){
value = "";
}else if(valueObj instanceof String[]){
String[] values = (String[])valueObj;
for(int i=0;i<values.length;i++){
value = values[i] + ",";
}
value = value.substring(0, value.length()-1);
}else{
value = valueObj.toString();
}
returnMap.put(name, value);
}
map = returnMap;
}
public Record() {
map = new HashMap();
}
@Override
public Object get(Object key) {
Object obj = null;
if(map.get(key) instanceof Object[]) {
Object[] arr = (Object[])map.get(key);
obj = request == null ? arr:(request.getParameter((String)key) == null ? arr:arr[0]);
} else {
obj = map.get(key);
}
return obj;
}
public String getString(Object key) {
return (String)get(key);
}
@SuppressWarnings("unchecked")
@Override
public Object put(Object key, Object value) {
if(value instanceof ClobProxyImpl){ // 这里必须要到如durid数据源的依赖 读取数据库 Clob类型数据
try {
ClobProxyImpl cpi = (ClobProxyImpl)value;
Reader is = cpi.getCharacterStream(); //获取流
BufferedReader br = new BufferedReader(is);
String str = br.readLine();
StringBuffer sb = new StringBuffer();
while(str != null){ //循环读取数据拼接到字符串
sb.append(str);
sb.append("\n");
str = br.readLine();
}
value = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
return map.put(key, value);
}
@Override
public Object remove(Object key) {
return map.remove(key);
}
@Override
public void clear() {
map.clear();
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value){
return map.containsValue(value);
}
public Set entrySet() {
return map.entrySet();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
public Set keySet() {
return map.keySet();
}
@SuppressWarnings("unchecked")
@Override
public void putAll(Map t) {
map.putAll(t);
}
@Override
public int size() {
return map.size();
}
public Collection values() {
return map.values();
}
}
第七步:接下来就是测试了
测试结果如下:
至此 到这里 我觉得这个东西就有点像Jfinal底层封装的Record类了,在多表联合查询的情况下,我们可以方便的使用mybatis-plus来进行了。大家有什么问题的话就在评论区讨论哈