Mybatis-Plus版本问题及代码生成器问题(2019/3/23)
所使用依赖:
<!--Mybatis Plus版本(Feb 24, 2019)-->
<MybatisPlus.version>3.1.0</MybatisPlus.version>
<!--MybatisPlus代码生成器版本-->
<MPG.version>3.1.0</MPG.version>
<!--MybatisPlus代码生成器模板引擎Apache的Velocity模板版本-->
<ApacheVelocity.version>2.0</ApacheVelocity.version>
<!--Mybatis持久层框架使用MybatisPlus插件包,不需要再导入Mybatis和Mybatis-Spring-->
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${MybatisPlus.version}</version>
</dependency>
<!--AutoGenerator是MyBatis-Plus的代码生成器,类似Mybatis的MBG-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${MPG.version}</version>
</dependency>
<!--MP的代码生成器默认的模板引擎使用的是Apache的Velocity模板,也可以更换为别的模板技术-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${ApacheVelocity.version}</version>
</dependency>
所使用测试类:
package test;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import org.junit.Test;
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;
/**
* @ClassName: MPGenerator
* @Description: MybatisPlus代码构造器使用
* @Date 2019/3/23 16:13
* @Version: 1.0
**/
public class MPGenerator {
//通过传入表名,生成对应表信息
@Test
public void generateCode() {
//管理员表
//generateByTables("admin");
//用户表
//generateByTables("users");
//学生表
//generateByTables("student");
//教师表
//generateByTables("teacher");
}
private void generateByTables(String tableNames) {
//1.全局配置
GlobalConfig config = new GlobalConfig();
//是否支持AR模式,使用类直接调用CRUD
config.setActiveRecord(true)
//设置作者
.setAuthor("Carlson")
//设置生成代码位置(绝对地址)
.setOutputDir("D:\\Java\\src\\main\\java")
//多次生成文件覆盖
.setFileOverride(true)
//主键策略,标明主键
.setIdType(IdType.AUTO)
//生成mapper.xml映射文件
.setBaseResultMap(true)
//生成ResultMap
.setBaseColumnList(true);
// 自定义文件命名,注意 %s 会自动填充表实体属性!,不使用“I”开头命名
config.setMapperName("%sMapper")
.setXmlName("%sMapper")
.setServiceName("%sService")
.setServiceImplName("%sServiceImpl")
.setControllerName("%sController");
//2.数据源配置
//连接地址
String dbUrl = "数据库连接地址";
DataSourceConfig dataSourceConfig = new DataSourceConfig();
//设置数据库类型
dataSourceConfig.setDbType(DbType.MYSQL)
//设置数据连接信息
.setUrl(dbUrl)
.setUsername("root")
.setPassword("123456")
.setDriverName("com.mysql.cj.jdbc.Driver");
//3.策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig
//全局大写命名
.setCapitalMode(true)
//配置使用Lombok
.setEntityLombokModel(true)
//表明生成策略,驼峰命名
.setNaming(NamingStrategy.underline_to_camel)
//需要生成的表,多个表名传数组
.setInclude(tableNames);
//此版本没有该属性
//setDbColumnUnderline(true)//全局下划线命名
//4.包配置
PackageConfig packageConfig = new PackageConfig();
//设置父包
packageConfig.setParent("edu.hut.hz")
.setMapper("dao")
.setXml("dao.mapper")
.setService("services")
.setServiceImpl("servicesImpl")
.setController("controller")
.setEntity("pojo");
//5.整合配置
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(config)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(packageConfig);
//6.执行
autoGenerator.execute();
}
}
问题1:
表的字段名不能使用Java关键字。
问题2:
@RequestMapping("/admin")写在方法上。(默认写在类上为父路径)
其他:
①
protected Serializable pkVal() {
return this.userId;
}
即使用AR(ActiveRecord)继承 Model<>所要重写方法。
②
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
}
public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
@Autowired
protected M baseMapper;}
所使用的AdminServiceImpl继承了ServiceImpl,里面已经注入baseMapper,无需再注入,即可直接使用已有方法。
③
Mapper通用方法和Service通用方法:
Mapper CRUD 接口: |
|
---|---|
insert(同右) | save(插入一条记录) |
deleteById | removeById(根据ID删除) |
deleteByMap | removeByMap(根据 columnMap 条件,删除记录) |
delete | remove(根据 entity 条件,删除记录) |
deleteBatchIds | removeByIds(删除(根据ID 批量删除)) |
updateById | updateById(根据 ID 选择修改) |
update | update(根据 whereEntity 条件,更新记录) |
selectById | getById(根据 ID 查询) |
selectBatchIds | listByIds(查询(根据ID 批量查询)) |
selectByMap | getMap(查询(根据 columnMap 条件)) |
selectOne(根据 entity 条件,查询一条记录) | getOne(根据 Wrapper,查询一条记录) |
selectCount | count(根据 Wrapper 条件,查询总记录数) |
selectList(根据 entity 条件,查询全部记录) | list(查询列表) |
selectMaps(根据 Wrapper 条件,查询全部记录) | listByMap(查询(根据 columnMap 条件)) |
selectObjs(根据 Wrapper 条件,查询全部记录) | getObj(根据 Wrapper,查询一条记录) Object getObj(Wrapper<T> queryWrapper); |
selectPage(根据 entity 条件,查询全部记录(并翻页)) | page(翻页查询) |
selectMapsPage(根据 Wrapper 条件,查询全部记录(并翻页)) | pageMaps(翻页查询) |
saveBatch( 插入(批量)) boolean saveBatch(T entity); | |
boolean saveBatch(Collection<T> entityList, int batchSize); | |
saveOrUpdateBatch(批量修改插入)(与上类同) @param entityList 实体对象集合 | |
saveOrUpdateBatch | |
updateBatchById(根据ID 批量更新) | |
saveOrUpdate(TableId 注解存在更新记录,否插入一条记录) | |
listMaps(查询列表) | |
listObjs(根据 Wrapper 条件,查询全部记录) |