怎样找到工厂直招网站,安徽大学电子信息工程学院官方网,求职seo,做网站为什么赚钱1、ID
1id策略有6种#xff1a; 想要id自增就在id上面添加
TableId(type IdType.AUTO)mybaits-plus的默认的主键策略是#xff1a;
TableId(type IdType.ID_WORKER)这样生成的是19位的数字id。
有的人喜欢使用UUID#xff1a;
TableId(type IdType.UUID)2、cre…1、ID
1id策略有6种 想要id自增就在id上面添加
TableId(type IdType.AUTO)mybaits-plus的默认的主键策略是
TableId(type IdType.ID_WORKER)这样生成的是19位的数字id。
有的人喜欢使用UUID
TableId(type IdType.UUID)2、createTime(创建时间)updateTime(修改时间)
1首先我们的数据库字段必须要有createTime(创建时间)updateTime(修改时间)这两个字段 2在实体类添加注解
TableField(fill FieldFill.INSERT)
private Date createTime;TableField(fill FieldFill.INSERT_UPDATE)
private Date updateTime;3在项目目录下新增一个handler包在包下创建MyMetaObjectHandler.java
Component
public class MyMetaObjectHandler implements MetaObjectHandler {//使用mp实现添加操作这个方法执行Overridepublic void insertFill(MetaObject metaObject) {this.setFieldValByName(createTime, new Date(), metaObject);this.setFieldValByName(updateTime, new Date(), metaObject);}//使用mp实现修改操作这个方法执行Overridepublic void updateFill(MetaObject metaObject) {this.setFieldValByName(updateTime, new Date(), metaObject);}
}
3、乐观锁——version
1 数据库表中添加:“version”并设置默认值为1 2实体类中添加字段然后加上version注解
Version
private Integer Version;3在项目目录下新建一个包config。然后在包下面新建Mpconfig.java文件。
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;EnableTransactionManagement
Configuration
MapperScan(com.bang.mp.mapper)//这里是扫描mapper
public class MpConfig {Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor mybatisPlusInterceptor new MybatisPlusInterceptor();//乐观锁插件mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());//分页插件mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return mybatisPlusInterceptor;}
}
4进行测试
//首先插入一条数据version字段默认赋值为1
Test
void testInsert(){User user new User();user.setAge(18);user.setName(阿昌);user.setEmail(995931576qq.com);int result userMapper.insert(user);
}
//在对这条数据进行修改version会变成 2
Test
void testOptimisticLocker(){//查询User user userMapper.selectById(1364080977348956166L);//修改数据user.setName(Helen Yao);user.setEmail(helenqq.com);//执行更新userMapper.updateById(user);
}
4、分页插件
1在Mpconfig.java 中添加为了方便我在步骤3乐观锁中已经添加了
//分页插件mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));2测试
//分页查询
Test
void testPage(){//1、创建page对象//传入参数当前页 和 每页显示记录数PageUser userPage new Page(1,3);//调用mp分页查询方法//调用mp分页查询过程中底层会封装把所有分页数据分装到page对象中userMapper.selectPage(userPage,null);//通过page对象获取数据userPage.getRecords().forEach(System.out::println);//遍历查询的分页数据System.out.println(userPage.getCurrent());//获取当前页System.out.println(userPage.getSize());//每页显示记录数System.out.println(userPage.getTotal());//总记录数System.out.println(userPage.getPages());//总页数System.out.println(userPage.hasNext());//判断是否有下一页System.out.println(userPage.hasPrevious());//判断是否有上一页
}
5、逻辑删除
1 数据库表中添加:“deleted”并设置默认值为0在这里mybatis-plus默认0是未删除1是已删除。 2实体类中添加字段然后加上TableLogic注解
TableLogic
private Integer deleted;3测试
/**
* 测试 逻辑删除
*/
Test
public void testLogicDelete() {
int result userMapper.deleteById(1L);
System.out.println(result);
}
6、条件查询
QueryWrapper queryWrapper new QueryWrapper();
//ge ,gt ,le, lt
queryWrapper.ge(age,30);
//eq,ne//between 年龄在20岁到30岁
queryWrapper.between(age,20,30);//like 模糊查询
queryWrapper.like(name,岳);//orderBy
queryWrapper.orderByAsc(id);
queryWrapper.orderByDesc(id);
//last 就是在sql 的后面添加
queryWrapper.last(limit 1);
//查询指定的列
queryWrapper.select(id,name);
ListUser list userMapper.selectList(queryWrapper);