人人网guide的link
该开源项目主要是利用MyBatis基于模板自动生成MyBatis-Plus的Entity、Mapper(即Dao)、Service、Controller、Mapper XML等。PS:用MyBatis-Plus-Generator会不会更快点
Dao
1.工程启动:包含了@MapperScan(“io.renren.dao”) 扫描该路径下的Dao加入IOC
2.有四个数据库的Dao,选哪个在配置类DbConfig中给出了答案
@Configuration
public class DbConfig {
@Value("${renren.database: mysql}") //application.yml中指出了该值
private String database;
@Autowired
private MySQLGeneratorDao mySQLGeneratorDao;
@Autowired
private OracleGeneratorDao oracleGeneratorDao;
@Autowired
private SQLServerGeneratorDao sqlServerGeneratorDao;
@Autowired
private PostgreSQLGeneratorDao postgreSQLGeneratorDao;
@Bean
@Primary
public GeneratorDao getGeneratorDao(){
if("mysql".equalsIgnoreCase(database)){
return mySQLGeneratorDao;
}else if("oracle".equalsIgnoreCase(database)){
return oracleGeneratorDao;
}else if("sqlserver".equalsIgnoreCase(database)){
return sqlServerGeneratorDao;
}else if("postgresql".equalsIgnoreCase(database)){
return postgreSQLGeneratorDao;
}else {
throw new RRException("不支持当前数据库:" + database);
}
}
}
3.进入MySQLGeneratorDao,看看写的啥
@Mapper
public interface MySQLGeneratorDao implements GeneratorDao {
}
诶它实现了一个GeneratorDao
public interface GeneratorDao {
List<Map<String, Object>> queryList(Map<String, Object> map);
Map<String, String> queryTable(String tableName);
List<Map<String, String>> queryColumns(String tableName);
}
不行啊,这些Dao方法都是没有定义,那在.xml中配置下吧
application.yml中指定了.xml文件位置
mybatis:
mapperLocations: classpath:mapper/**/*.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.renren.dao.MySQLGeneratorDao">
<select id="queryList" resultType="map">
select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables
where table_schema = (select database())
<if test="tableName != null and tableName.trim() != ''">
and table_name like concat('%', #{tableName}, '%')
</if>
order by create_time desc
</select>
<select id="queryTable" resultType="map">
select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables
where table_schema = (select database()) and table_name = #{tableName}
</select>
<select id="queryColumns" resultType="map">
select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns
where table_name = #{tableName} and table_schema = (select database()) order by ordinal_position
</select>
</mapper>
这一样一看原来GeneratorDao中生命的方法主要是为了查询表、字段等信息,用于自定义属性注入模板
GenUtils.java
/home/xu/PersonProjects/IdeaProjects/guimail/renren-generator/src/main/java/io/renren/utils/GenUtils.java
该脚本主要作用:
1.导入代码模板
1.1 自定义代码模板
咱看一下Dao.java.vm:
package ${package}.${moduleName}.dao;
import ${package}.${moduleName}.entity.${className}Entity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* ${comments}
*
* @author ${author}
* @email ${email}
* @date ${datetime}
*/
@Mapper
public interface ${className}Dao extends BaseMapper<${className}Entity> { //典型的mybatisplus
}
2.自定义属性注入
//封装模板数据
Map<String, Object> map = new HashMap<>();
map.put("tableName", tableEntity.getTableName());
map.put("comments", tableEntity.getComments());
map.put("pk", tableEntity.getPk());
map.put("className", tableEntity.getClassName());
map.put("classname", tableEntity.getClassname());
map.put("pathName", tableEntity.getClassname().toLowerCase());
map.put("columns", tableEntity.getColumns());
map.put("hasBigDecimal", hasBigDecimal);
map.put("mainPath", mainPath);
map.put("package", config.getString("package" ));
map.put("moduleName", config.getString("moduleName" ));
map.put("author", config.getString("author" ));
map.put("email", config.getString("email" ));
map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
VelocityContext context = new VelocityContext(map);
在模板中通过:${属性名} 注入对应属性值 如:${classname}
整体流程
工程启动-> 浏览器发起请求 -> SysGeneratorController对应处理 -> /code下 获取响应数据generatorCode -> … -> GenUtils.java
@RequestMapping("/code")
public void code(String tables, HttpServletResponse response) throws IOException{
byte[] data = sysGeneratorService.generatorCode(tables.split(","));
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
}
使用注意
/home/xu/PersonProjects/IdeaProjects/lecture/renren-generator/src/main/resources/application.yml
数据源配置中的数据库记得更改
url: jdbc:mysql://localhost:3306/lecture-admin?useUnicode=true&characterEncoding=UTF-8&useSSL=false
/home/xu/PersonProjects/IdeaProjects/lecture/renren-generator/src/main/resources/generator.properties
# 主目录 感觉这个主目录对应common模块的包名
mainPath=com.yfxu
#包名
package=com.yfxu.lecture
#模块名
moduleName=admin
#表前缀(生成的类名会去掉该表前缀)
tablePrefix=sys_
而且生成的代码依赖utils这些东西