//根据id查询
User user = userMapper.selectById(1L);
//批量查询
List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
//条件查询
HashMap<String, Object> map = new HashMap<>();
map.put("name","Tom");
map.put("age",28);
List<User> users1 = userMapper.selectByMap(map);
分页查询
- 使用limit进行分页
- pageHelper第三方插件
- MybatisPlus也内置了分页插件
如何使用?
配置拦截器即可
@Configuration //配置类
@MapperScan("com.kcl.mapper") //写在这里即可
@EnableTransactionManagement
public class MybatisPlusConfig {
// 旧版 分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
直接使用page对象即可
@Test
void testPage(){
//1.当前页,2.页面大小
IPage<User> userIPage = mUserMapper.selectPage(new Page<>(1, 3), null);
List<User> records = userIPage.getRecords();
System.out.println(records);
}
删除
mUserMapper.deleteById(1L);
mUserMapper.deleteBatchIds(Arrays.asList(1L,2L,3L));
HashMap<String, Object> map = new HashMap<>();
map.put("name","kcl");
mUserMapper.deleteByMap(map);
逻辑删除
物理删除:数据库中直接删除
逻辑删除:不删除,delete=1,管理员可以查看被删除的数据,类似于回收站
略…
性能分析插件
一些慢sql,超过时间就停止进行!
略…
条件构造器wapper
test1 isnotnull ge le
QueryWrapper<User> wapper = new QueryWrapper<>();
//ge表示greater equals
wapper.isNotNull("name").isNotNull("email").ge("age",20);
List<User> users = mUserMapper.selectList(wapper);
System.out.println(users);
test2 查询一个 eq
QueryWrapper<User> wapper = new QueryWrapper<>();
wapper.eq("name","kcl");
User user = mUserMapper.selectOne(wapper);
System.out.println(user);
test3 between
QueryWrapper<User> wapper = new QueryWrapper<>();
wapper.between("age",10,20); //[10,20]
Integer c = mUserMapper.selectCount(wapper); //查询结果数
System.out.println(c);
test4 模糊查询
QueryWrapper<User> wapper = new QueryWrapper<>();
//likeRight 表示%在右边,xx%
wapper.notLike("name","o").likeLeft("name","cl");
List<User> users = mUserMapper.selectList(wapper);
System.out.println(users);
代码自动生成器
//自动生成代码
@Test
void kclCode(){
//1.新建一个代码生成器对象
AutoGenerator autoGenerator = new AutoGenerator();
//2.全局配置
GlobalConfig globalConfig = new GlobalConfig();
//获取程序当前路径
String projectPath = System.getProperty("user.dir");
globalConfig.setOutputDir(projectPath+"/src/main/java");
globalConfig.setAuthor("kcl");
globalConfig.setOpen(false); //生成代码之后不打开文件夹
globalConfig.setFileOverride(false); //是否覆盖
globalConfig.setServiceName("%sService"); //修改service的名称
globalConfig.setIdType(IdType.ID_WORKER);
globalConfig.setDateType(DateType.ONLY_DATE);
autoGenerator.setGlobalConfig(globalConfig);
//3.设置数据源,必须要写,不然不知道去哪里生成代码
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL);
dataSourceConfig.setUrl("jdbc:mysql://rm-bp18jitlw9a952i5x3o.mysql.rds.aliyuncs.com:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("Kclkd2013");
autoGenerator.setDataSource(dataSourceConfig);
//4.包的配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setModuleName("mp");
packageConfig.setParent("com.kcl");
packageConfig.setEntity("pojo");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
packageConfig.setController("controller");
autoGenerator.setPackageInfo(packageConfig);
//5. 测量配置 略
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setInclude("chapter"); //需要映射的数据库表 !!!
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
strategyConfig.setEntityLombokModel(true);
//自动填充配置
TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
TableFill gmtModified = new TableFill("gmt_modified", FieldFill.UPDATE);
strategyConfig.setTableFillList(Arrays.asList(gmtCreate,gmtModified));
strategyConfig.setRestControllerStyle(true); //restful驼峰命名开启
strategyConfig.setControllerMappingHyphenStyle(true); //hello_id_2 ???
autoGenerator.setStrategy(strategyConfig);
autoGenerator.execute(); //执行
}
总结
- 先建立数据库,都加上字段gmt_create, gmt_modified
- 新建springboot项目,pom依赖导入 …mysql,lombok,mybatis_plus, 代码生成器插件, 就这4个
- application.properties 加上数据库配置和mp日志
- 测试类自动生成
- service,mapper都加上@resposity @service
- 新建config类,mapper扫描
- 新建metaObjectHandler, 自动填充gmt属性