1、添加jar包
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
2、yml文件配置
#配置tomcat服务器相关信息
server:
port: 8090
servlet:
#项目发布路径 一般都是默认值/
context-path: /
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
#添加mybatis相关配置信息
mybatis:
#只要定义了别名包 之后的映射类型可以自动的完成拼接.
type-aliases-package: com.jt.demo.pojo
mapper-locations: classpath:/mybatis/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
mybatis-plus:
//xml映射文件路径
mapper-locations: classpath*:**/mapper/xml/**/*.xml
//定义别名包 映射类型完成自动拼接
type-aliases-package: com.jd.demo.pojo
configuration:
#驼峰下划线转换
map-underscore-to-camel-case: true
3、pojo,mapper,service配置
//pojo
@Data
@EqualsAndHashCode(callSuper=true)
@Accessors(chain = true)
@TableName("t_secret")
//model封装了CRUD
public class Secret extends Model<Secret>{
private static final long serialVersionUID = 1L;
@TableId(value="id",type=IdType.ASSIGN_UUID)
private String id; // id
}
//mapper层 继承basenapper 记得加泛型 封装了CRUD
//@MapperScan("com.xx.mapper")启动类加上此注解,就不用写下面注解
@Mapper//此注解告知该接口的实现类由框架实现,并交给spring管理
public interface SecretMapper extends BaseMapper<Secret>{
}
//service层 继承IService 封装了CRUD
public interface SecretService extends IService<Secret>{
}
//service层实现类
@Service
@Transactional
public class SecretServiceImpl extends ServiceImpl<SecretMapper, Secret> implements SecretService {
@Autowired
private SecretMapper secretMapper;
}
这样就可以在service使用mapper的crud功能,在controller使用service层的CRUD功能。service的封装在basemapper上更进一步进行封装,功能都差不多,只是多了一个批量使用功能。
注意:在使用ybatis-plus查询的时候对null值和空值的判断
例:1、当表中没有数据的时候
List<Users> selectList = usersMapper.selectList(null);
System.out.println(selectList==null);
System.out.println(selectList.isEmpty());
for(Users users:selectList) {
System.out.println("111111111111111111111");
}
for (int i = 0; i < selectList.size(); i++) {
System.out.println(i);
}
可以看出结果不为null,但是为空,不会进入遍历
2、当表中有数据的时候
List<Users> selectList = usersMapper.selectList(null);
System.out.println(selectList==null);
System.out.println(selectList.isEmpty());
for(Users users:selectList) {
System.out.println(users==null);
System.out.println("111111111111111111111");
System.out.println("------"+users);
}
for (int i = 0; i < selectList.size(); i++) {
System.out.println(i);
}
}
当表中有值的时候,查询表中全部数据的的时候,查询出的结果不会 为null和空(因为表中有值),进入遍历,遍历后的对象也不会为null和空(因为表中主键不为null)。
3、当表中有数据,查询某个字段,但这个字段可能为空或者null
QueryWrapper<Users> queryWrapper=new QueryWrapper<>();
queryWrapper.select("password");
List<Users> selectList = usersMapper.selectList(queryWrapper);
System.out.println(selectList==null);
System.out.println(selectList.isEmpty());
System.out.println("=========================================");
for(Users users:selectList) {
System.out.println(users==null);
System.out.println("111111111111111111111");
System.out.println("------"+users);
}
for (int i = 0; i < selectList.size(); i++) {
System.out.println(i);
}
}
当表中有值的时候,查询表中部分字段的时候,查询出的结果不会 为null和空(因为表中有值),进入遍历,遍历后的对象有可能为null值或者空值(因为查询字段为null的时候,对象就会为null),因为查询的这个字段为空或者null时候,需要对遍历后的对象进行判断。
总之:查询多个返回值为list集合,集合不会为null,但有可能为空,集合中的对象也有可能为null或者空