SQL中的and和OR的优先级
首先and的优先级大于or,通俗理解其实or查询其实会把条件分为左右两边来查。
如select * from user where id = 1 and status = 2 or status = 3,本来想查询user表中id为1的状态为2或者3的数据,其实只会这样执行,and比or优先,结果是查询id为1和状态为2,或者状态为3的数据。
解决办法,如果是同字段,如上例子,可Select * from user where id=1 and status in (2,3),或者select * from user where id = 1 and (status = 2 or status = 3),就可以了
hutool包下的BeanUtil工具使用
链接:https://doc.hutool.cn/pages/index/
BeanUtil:用于Map与JavaBean对象的互相转换以及对象属性的拷贝。
1、新建实体类对象
package com.heima.item.test.hutool;
import lombok.Data;
@Data
public class User {
private String name;
private Integer age;
}
package com.heima.item.test.hutool;
import cn.hutool.core.bean.BeanUtil;
import java.util.Map;
public class TestBeanUtil {
public static void main(String[] args) {
User user = new User();
user.setAge(1);
user.setName("张三");
// 对象转化为map
Map<String, Object> map = BeanUtil.beanToMap(user);
System.out.println(map);
// map转化为对象
User user1 = BeanUtil.toBean(map, User.class);
System.out.println(user1);
// 对象拷贝
User user2 = new User();
BeanUtil.copyProperties(user,user2);
System.out.println(user2);
}
}
beanToMap
将对象映射为map
实现有以下几种方式:
实现一:
//最简单的一种,直接将bean对象转换为map(若为null则对应的map的value为null)
public static Map beanToMap(Object bean);
实现二:
/*
isToUnderlineCase:对于驼峰的属性名称,其对应的key是否用'_'分割开
ignoreNullValue:对于空的属性值,是否忽略
这两个值默认都为false
*/
public static Map<String, Object> beanToMap(Object bean, boolean isToUnderlineCase,
boolean ignoreNullValue)
```![在这里插入图片描述](https://img-blog.csdnimg.cn/e010e0e680be458dbc508d12c7964cb3.png#pic_center)
User user = new User();
user.setAge(1);
user.setUserName("张三");
Map<String, Object> map = BeanUtil.beanToMap(user, true, false);
System.out.println(map);
``
运行结果:
{user_name=张三, age=1}
User user = new User();
// user.setAge(1);
user.setUserName("张三");
Map<String, Object> map = BeanUtil.beanToMap(user, true, true);
System.out.println(map);
运行结果:{user_name=张三}
实现三:
/*
targetMap:表示要添加到的map,也就是可以自己传输一个map。
然后再将封装后的map键值对一一封装到里面返回。
*/
public static Map<String, Object> beanToMap(Object bean, Map<String, Object> targetMap,
boolean isToUnderlineCase, boolean ignoreNullValue);
User user = new User();
user.setAge(1);
user.setUserName("张三");
Map<String, Object> map = new HashMap<>();
map.put("MyKey", 100);
Map<String, Object> map1 = BeanUtil.beanToMap(user, map, false, true);
System.out.println(map1);
运行结果: {MyKey=100, userName=张三, age=1}
实现四:
/*
keyEditor:对key自定义编辑。也就是可以对map中的key进行改变。
是一个接口,要自定义实现类(内部实现类)
*/
public static Map<String, Object> beanToMap(Object bean, Map<String, Object> targetMap,
boolean ignoreNullValue, Editor<String> keyEditor);
User user = new User();
user.setAge(1);
user.setUserName("张三");
HashMap<String,Object> map = new HashMap<>();
Map<String, Object> map1 = BeanUtil.beanToMap(user, map, false, new Editor<String>() {
@Override
public String edit(String s) {
return s.toLowerCase() + s.length();
}
});
System.out.println(map1);
运行结果: {username8=张三, age3=1}
实现五:
/*
copyOptions:通过这个参数可以自定义任何转换规则,如“忽略某字段”、“设置editor等”
*/
public static Map<String, Object> beanToMap(Object bean, Map<String, Object> targetMap,
CopyOptions copyOptions);
User user = new User();
user.setAge(1);
user.setUserName("张三");
// 假设不将user对象中的age属性映射到map,可以这样实现
HashMap<String,Object> map = new HashMap<>();
CopyOptions copyOptions = new CopyOptions();
copyOptions.setIgnoreProperties("age");
Map<String, Object> map1 = BeanUtil.beanToMap(user, map, copyOptions);
System.out.println(map1);
运行结果: {userName=张三}
User user = new User();
user.setAge(1);
user.setUserName("张三");
HashMap<String,Object> map = new HashMap<>();
CopyOptions copyOptions = new CopyOptions();
// 设置editor改变map中的key名称
copyOptions.setFieldNameEditor(s -> s.toUpperCase());
Map<String, Object> map1 = BeanUtil.beanToMap(user, map, copyOptions);
System.out.println(map1);
运行结果: {USERNAME=张三, AGE=1}
toBean
toBean是将map中的某一些字段转换为user中的某个属性。
实现一:
public static <T> T toBean(Object source, Class<T> clazz);
User user = new User();
user.setAge(1);
user.setUserName("张三");
HashMap<String,Object> map = new HashMap<>();
map.put("age",1);
map.put("userName","张三");
User user1 = BeanUtil.toBean(map, User.class);
System.out.println(user1);
运行结果:
User(userName=张三, age=1)
这里map中的key一定要跟User中完全相同。比如:user中的属性名称为“userName”,如果写成“UserName”。那么注入就会为null。
实现二:
public static <T> T toBean(Object source, Class<T> clazz, CopyOptions options)
HashMap<String,Object> map = new HashMap<>();
map.put("age",1);
map.put("userName","张三");
// map中age不转化为user中的age属性
CopyOptions copyOptions = new CopyOptions();
copyOptions.setIgnoreProperties("age");
User user = BeanUtil.toBean(map, User.class, copyOptions);
System.out.println(user);
运行结果: User(userName=张三, age=null)
copyProperties
将一个变量属性拷贝到另一个对象上
实现一:
/*
source:源变量
targer:目标变量
ignoreProperties:需要忽略的字段名称
*/
public static void copyProperties(Object source, Object target, String... ignoreProperties);
新增copy对象
@Data
@ToString
public class TestUser {
private String userName;
private Integer age;
private Integer id;
}
User user = new User();
user.setAge(100);
user.setUserName("张三");
TestUser testUser = new TestUser();
BeanUtil.copyProperties(user,testUser);
System.out.println(testUser);
运行结果: TestUser(userName=张三, age=100, id=null)
spring中的BeanUtils不同。 在这里的BeanUtil拷贝对象,source和target各自可以为不同的类,各自的属性字段都可以随意。 但拷贝的时候只会拷贝属性名称相同的属性(上述中TestUser包含了User不存在的id字段,还是可以拷贝的)
它还有的其他实现:
public static void copyProperties(Object source, Object target, boolean ignoreCase);
public static void copyProperties(Object source, Object target, CopyOptions copyOptions);
User user = new User();
user.setAge(100);
user.setUserName("张三");
// 方式一
TestUser testUser = new TestUser();
BeanUtil.copyProperties(user,testUser,"age");
System.out.println("testUser"+testUser);
// 方式二
TestUser copyUser = new TestUser();
CopyOptions copyOptions = new CopyOptions();
copyOptions.setIgnoreProperties("age");
BeanUtil.copyProperties(user,copyUser,copyOptions);
System.out.println("copyUser" + copyUser);
运行结果:
testUserTestUser(userName=张三, age=null, id=null)
copyUserTestUser(userName=张三, age=null, id=null)