官网
引入依赖
<!--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>
yml配置
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")@EnableDiscoveryClientpublicclassSaasAdminApplication{publicstaticvoidmain(String[] args){SpringApplication.run(SaasAdminApplication.class, args);}}
自动生产代码接口util
importcom.baomidou.mybatisplus.annotation.DbType;importcom.baomidou.mybatisplus.annotation.FieldFill;importcom.baomidou.mybatisplus.generator.AutoGenerator;importcom.baomidou.mybatisplus.generator.config.DataSourceConfig;importcom.baomidou.mybatisplus.generator.config.GlobalConfig;importcom.baomidou.mybatisplus.generator.config.PackageConfig;importcom.baomidou.mybatisplus.generator.config.StrategyConfig;importcom.baomidou.mybatisplus.generator.config.po.TableFill;importcom.baomidou.mybatisplus.generator.config.rules.NamingStrategy;importjava.util.Arrays;importjava.util.List;publicclassMyBatisPlusGenerate{publicstaticvoidmain(String[] args){//创建generator对象:通过操作这个对象来生成代码AutoGenerator autoGenerator =newAutoGenerator();//数据源:建立数据源与数据库连接DataSourceConfig dataSourceConfig =newDataSourceConfig();
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 =newGlobalConfig();
globalConfig.setOutputDir("D:/generatingCode/test/main/java");//放到子工程里,修改了路径 //这里个人建议经历不要直接放在公司项目里面,避免覆盖已经写好的代码,
globalConfig.setAuthor("yszh-gw");
globalConfig.setOpen(false);
globalConfig.setServiceName("%sService");
autoGenerator.setGlobalConfig(globalConfig);//包信息PackageConfig packageConfig =newPackageConfig();
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 =newStrategyConfig();
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 =newTableFill("create_time",FieldFill.INSERT);TableFill tableFill1 =newTableFill("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 =newPage<>(pageNum,pageSize);
MyBatis-plus提供的条件拼接对象
QueryWrapper<TenantVo> qw =newQueryWrapper<>();
自定义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 =newPage<>(pageNum,pageSize);QueryWrapper<MenusDataEntity> qw =newQueryWrapper<>();// 查询添加环节筛选需求
qw.eq("md.menus_id",menusId);if(!StrUtil.isEmpty(name)){
qw.like("md.name",name);}IPage<MenusDataEntity> menusDataEntityIPage = menusDataMapper.pageVo(page,qw);
版权归原作者 夏七丿 所有, 如有侵权,请联系我们删除。