时间:2022-12-09 09:56:16 | 栏目:JAVA代码 | 点击:次
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
支持的数据库有:
<!--mybatis-plus-boot-starter 包含了mybatis的所有依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency> <!--推荐大家使用阿里巴巴的druid的连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <!--引入mysql数据库相关依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope>
spring: application: name: manage-client datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/mysql?userSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 username: root password: ys@2017 main: allow-circular-references: true #mybatis-plus配置 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: classpath:com/huangyabei/mapper/xml/*.xml
@SpringBootApplication(scanBasePackages = {"com.win.sky"}) @MapperScan(basePackages="com.win.sky.dao") @EnableDiscoveryClient public class SaasAdminApplication { public static void main(String[] args) { SpringApplication.run(SaasAdminApplication.class, args); } }
import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.po.TableFill; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import java.util.Arrays; import java.util.List; public class MyBatisPlusGenerate { public static void main(String[] args) { //创建generator对象:通过操作这个对象来生成代码 AutoGenerator autoGenerator = new AutoGenerator(); //数据源:建立数据源与数据库连接 DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL);//选择数据库的类型 //数据源 dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver"); //账号 dataSourceConfig.setUsername("root"); //密码 dataSourceConfig.setPassword("ys@2017"); //连接地址 dataSourceConfig.setUrl("jdbc:mysql://192.168.10.77:3306/yszh_gw?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai"); autoGenerator.setDataSource(dataSourceConfig); //全局配置 GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setOutputDir("D:/generatingCode/test/main/java");//放到子工程里,修改了路径 //这里个人建议经历不要直接放在公司项目里面,避免覆盖已经写好的代码, globalConfig.setAuthor("yszh-gw"); globalConfig.setOpen(false); globalConfig.setServiceName("%sService"); autoGenerator.setGlobalConfig(globalConfig); //包信息 PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent("com.win.sky.news"); packageConfig.setEntity("entity"); packageConfig.setMapper("mapper"); packageConfig.setService("service"); packageConfig.setServiceImpl("service.impl"); packageConfig.setController("controller"); autoGenerator.setPackageInfo(packageConfig); //策略配置 StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig.setInclude("t_test","t_order");//有新的表往里面加就是了 strategyConfig.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略驼峰命名 strategyConfig.setTablePrefix(packageConfig.getModuleName() + "t_");//生成实体时去掉表前缀 strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略 strategyConfig.setEntityLombokModel(true); TableFill tableFill = new TableFill("create_time", FieldFill.INSERT); TableFill tableFill1 = new TableFill("update_time", FieldFill.INSERT_UPDATE); List<TableFill> tableFills = Arrays.asList(tableFill,tableFill1); strategyConfig.setTableFillList(tableFills); autoGenerator.setStrategy(strategyConfig); //运行 autoGenerator.execute(); } }
mybatis-plus在使用过程中类似JPA框架
自定义接口继承了mybatis-plus框架提供的这个IService接口之后,基本的CRUD方法都可以直接使用
Mybatis-plus提供了分页查询方法
Page<TenantVo> page = new Page<>(pageNum,pageSize);
MyBatis-plus提供的条件拼接对象
QueryWrapper<TenantVo> qw = new QueryWrapper<>();
自定义sql查询
在实际开发过程中,可能涉及到多表复杂查询,简单的sql已经不适合使用,这样的情况下,我们可以通过自定义sql配合QueryWrapper条件拼接对象使用
SELECT md.id, md.name, md.menus_id, md.coding, md.data_id, md.remark, md.create_by, md.create_time, md.update_by, md.update_time, d.name dataName, d.rule FROM t_menus_data md LEFT JOIN t_data_permission d on d.id = md.data_id --下面的代码就是用例接收服务层传过来的拼接的条件参数 <where> ${ew.sqlSegment} </where>
mapper层
IPage<MenusDataEntity> pageVo(@Param("page") IPage<MenusDataEntity> page, @Param(Constants.WRAPPER) QueryWrapper<MenusDataEntity> qw);
service层
IPage<MenusDataEntity> page = new Page<>(pageNum,pageSize); QueryWrapper<MenusDataEntity> qw = new QueryWrapper<>(); // 查询添加环节筛选需求 qw.eq("md.menus_id",menusId); if (!StrUtil.isEmpty(name)){ qw.like("md.name",name); } IPage<MenusDataEntity> menusDataEntityIPage = menusDataMapper.pageVo(page,qw);