Warrper没有实现序列化接口, 也不推荐通过网络传输.
myBatis 能用的这里都能用.
配置
<!-- 用mybatis-plus之后 -->
<!-- 别忘了自定义mapper.xml文件的位置, 放在resource 里. maven不映射.java以外的文件 -->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:sqlMapperConfig.xml"/>
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
<property name="typeAliasesPackage" value="com.fz.pojo"/>
</bean>
3.10以上版本暂时在时间类型映射那有问题
导包
compile group: 'com.baomidou', name: 'mybatis-plus', version: '3.2.0'
compile group: 'com.baomidou', name: 'mybatis-plus-generator', version: '3.2.0'
compile group: 'org.apache.velocity', name: 'velocity-engine-core', version: '2.1'
compile group: 'org.freemarker', name: 'freemarker', version: '2.3.29' //自动生成mapper的模板引擎
坑
如果查询带_的字段有的查出来是null,那就关闭驼峰映射,
<configuration>
<!-- 开启驼峰映射 ,为自定义的SQL语句服务-->
<!--设置启用数据库字段下划线映射到java对象的驼峰式命名属性,默认为false-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
原理
自动生成代码(需要依赖org.springframework.spring-core)
package com.fz.util;
import com.baomidou.mybatisplus.annotation.DbType;
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.rules.NamingStrategy;
public class MyBatisPlusGenerator {
public static void main(String[] args) {
//1、全局配置
GlobalConfig config = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
config.setActiveRecord(true)//开启AR模式
.setAuthor("liu ji jiang")//设置作者
.setOutputDir(projectPath + "/src/main/java/") //生成路径(一般都是生成在此项目的src/main/java下面)
.setFileOverride(true)//第二次生成会把第一次生成的覆盖掉
.setActiveRecord(true) // 不需要ActiveRecord特性的请改为false
// .setIdType(IdType.AUTO)//主键策略
.setControllerName("%sController")
.setServiceImplName("%sServiceImpl")
.setServiceName("%sService")//生成的service接口名字首字母是否为I,这样设置就没有I
.setMapperName("%sMapper")
.setXmlName("%sMapper")
.setBaseResultMap(true)//生成resultMap
.setBaseColumnList(true)//在xml中生成基础列
.setEnableCache(false); // XML 二级缓存
//2、数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig
.setDbType(DbType.MYSQL)//数据库类型
.setUrl("jdbc:mysql://localhost:3306/shop?useUnicode=false&useSSL=false&characterEncoding=utf8&useLegacyDatetimeCode=false&serverTimezone=UTC")
.setDriverName("com.mysql.jdbc.Driver")
.setUsername("root")
.setPassword("root");
// dsc.setSchemaName("public");
//3、策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig
.setCapitalMode(false)//开启全局大写命名 ORACLE 注意
// .setNaming(NamingStrategy.no_change)//表名字段名使用下划线
.setNaming(NamingStrategy.underline_to_camel)//下划线到驼峰的命名方式
.setTablePrefix()//表名前缀
.setEntityLombokModel(false)//使用lombok
// select table_name from information_schema.tables where table_schema='shop'
.setInclude("tb_content,tb_content_category,tb_item,tb_item_cat,tb_item_desc,tb_item_param,tb_item_param_item,tb_user".split(","));//逆向工程使用的表
// strategy.setExclude(new String[]{"test"}); // 排除生成的表
// 自定义实体父类
// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
// 自定义实体,公共字段
// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
// 自定义 mapper 父类
// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
// 自定义 service 父类
// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
// 自定义 service 实现类父类
// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
// 自定义 controller 父类
// strategy.setSuperControllerClass("com.baomidou.demo.TestController");
// 【实体】是否生成字段常量(默认 false)
// public static final String ID = "test_id";
// strategy.setEntityColumnConstant(true);
// 【实体】是否为构建者模型(默认 false)
// public User setName(String name) {this.name = name; return this;}
// strategy.setEntityBuilderModel(true);
//4、包名策略配置
PackageConfig packageConfig = new PackageConfig();
packageConfig
.setParent("com.fz")//设置包名的parent
.setMapper("dao")
.setService("service")
.setServiceImpl("service.impl")
.setController("controller")
.setEntity("pojo")
.setXml("dao.xml");//设置xml文件的目录
//5、整合配置
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator
.setGlobalConfig(config)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(packageConfig);
//6、执行
autoGenerator.execute();
// 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
// InjectionConfig cfg = new InjectionConfig() {
// @Override
// public void initMap() {
// Map<String, Object> map = new HashMap<String, Object>();
// map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
// this.setMap(map);
// }
// };
//
// // 自定义 xxList.jsp 生成
// List<FileOutConfig> focList = new ArrayList<>();
// focList.add(new FileOutConfig("/template/list.jsp.vm") {
// @Override
// public String outputFile(TableInfo tableInfo) {
// // 自定义输入文件名称
// return "D://my_" + tableInfo.getEntityName() + ".jsp";
// }
// });
// cfg.setFileOutConfigList(focList);
// mpg.setCfg(cfg);
//
// // 调整 xml 生成目录演示
// focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
// @Override
// public String outputFile(TableInfo tableInfo) {
// return "/develop/code/xml/" + tableInfo.getEntityName() + ".xml";
// }
// });
// cfg.setFileOutConfigList(focList);
// mpg.setCfg(cfg);
//
// // 关闭默认 xml 生成,调整生成 至 根目录
// TemplateConfig tc = new TemplateConfig();
// tc.setXml(null);
// mpg.setTemplate(tc);
// 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下面内容修改,
// 放置自己项目的 src/main/resources/templates 目录下, 默认名称一下可以不配置,也可以自定义模板名称
// TemplateConfig tc = new TemplateConfig();
// tc.setController("...");
// tc.setEntity("...");
// tc.setMapper("...");
// tc.setXml("...");
// tc.setService("...");
// tc.setServiceImpl("...");
// 如上任何一个模块如果设置 空 OR Null 将不生成该模块。
// mpg.setTemplate(tc);
// 打印注入设置【可无】
// System.err.println(mpg.getCfg().getMap().get("abc"));
}
}
评论区