对mybatis做了增强,但是没有做更改,所以以前mybatis如何使用,现在一样,只不过可以选择使用其他的一些扩展功能来加强开发效率,特别是单表的CRUD。
- 提供了内置的分页插件
- 提供了基础的mapper以及service封装
- 提供了乐观锁的实现方式
- 提供了代码构造器的组件
通过注解来对类与表的映射com.baomidou mybatis-plus-boot-starter3.4.2
-
@TableName 在类上面映射表
-
@TableId 在主键属性上面映射主键字段
-
@TableField 普通属性对应字段的映射
@Data @TableName("woniu_user") public class User { @TableId(value="user_id",type = IdType.AUTO) private Integer id; @TableField("user_name") private String name; @TableField("user_age") private Integer age; }
public interface UserMapper extends baseMapper编写service集成通用service{ }
public interface UserService extends IService{ }
@Service public class UserServiceImpl extends ServiceImpl如果要使用分页功能,需要把分页拦截器配置到容器当中implements UserService { }
@Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); PaginationInnerInterceptor pg = new PaginationInnerInterceptor(DbType.MYSQL); //设置分页溢出回到首页的处理 pg.setOverflow(true); interceptor.addInnerInterceptor(pg); return interceptor; }
业务层
@GetMapping("findByPage") public IPagemybatis-plus的特点以及跟mybatis的区别?find(){ QueryWrapper sectionQueryWrapper = new QueryWrapper<>(); sectionQueryWrapper.ge("id", 1); return userMapper.selectPage(new Page<>(1, 2),sectionQueryWrapper); }
MyBatis:一种 *** 作数据库的框架,提供一种Mapper类,支持让你用java代码进行增删改查的数据库 *** 作,省去了每次都要手写sql语句的麻烦。但是!有一个前提,你得先在xml中写好sql语句,也是很麻烦的。
MP的存在就是为了稍稍弥补Mybatis的不足。在我们使用Mybatis时会发现,每当要写一个业务逻辑的时候都要在DAO层写一个方法,再对应一个SQL,即使是简单的条件查询、即使仅仅改变了一个条件都要在DAO层新增一个方法,针对这个问题,MP就提供了一个很好的解决方案,它可以让我们避免许多重复性的工作。
-
导包
com.baomidou mybatis-plus-generator3.4.1 org.apache.velocity velocity-engine-core2.2 -
修改生成器参数
public class GenaratorUtil { public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("liwei"); gc.setOpen(false); // gc.setSwagger2(true); 实体属性 Swagger2 注解 mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql:///task?charsetEncoding=utf- 8&useSSL=false&ServerTimezone=Asia/shanghai"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("123321"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.woniu"); pc.setEntity("entity"); pc.setMapper("mapper"); pc.setService("service"); pc.setServiceImpl("service.impl"); pc.setController("controller"); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker //String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 velocity String templatePath = "/templates/mapper.xml.vm"; // 自定义输出配置 List
focList = new ArrayList<>(); // 自定义配置会被优先输出 focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { //定义输出xml文件的地址以及名字 return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); mpg.setStrategy(strategy); mpg.setTemplateEngine(new VelocityTemplateEngine()); mpg.execute(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)