55.mybatis-plus

55.mybatis-plus,第1张

55.mybatis-plus mybatis-plus

对mybatis做了增强,但是没有做更改,所以以前mybatis如何使用,现在一样,只不过可以选择使用其他的一些扩展功能来加强开发效率,特别是单表的CRUD。

  1. 提供了内置的分页插件
  2. 提供了基础的mapper以及service封装
  3. 提供了乐观锁的实现方式
  4. 提供了代码构造器的组件
导包

    com.baomidou
    mybatis-plus-boot-starter
    3.4.2

通过注解来对类与表的映射
  1. @TableName 在类上面映射表

  2. @TableId 在主键属性上面映射主键字段

  3. @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;
    
    }
    
    
编写mapper继承提供的通用mapper
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 IPage find(){
        QueryWrapper sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.ge("id", 1);
        return userMapper.selectPage(new Page<>(1, 2),sectionQueryWrapper);
}
mybatis-plus的特点以及跟mybatis的区别?

​ MyBatis:一种 *** 作数据库的框架,提供一种Mapper类,支持让你用java代码进行增删改查的数据库 *** 作,省去了每次都要手写sql语句的麻烦。但是!有一个前提,你得先在xml中写好sql语句,也是很麻烦的。
​ MP的存在就是为了稍稍弥补Mybatis的不足。在我们使用Mybatis时会发现,每当要写一个业务逻辑的时候都要在DAO层写一个方法,再对应一个SQL,即使是简单的条件查询、即使仅仅改变了一个条件都要在DAO层新增一个方法,针对这个问题,MP就提供了一个很好的解决方案,它可以让我们避免许多重复性的工作。

plus的代码生成器
  1. 导包

    
        com.baomidou
        mybatis-plus-generator
        3.4.1
    
    
        org.apache.velocity
        velocity-engine-core
        2.2
    
    
  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();
    	}
    }
    

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5684323.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存