【OpenSource】renren-generator的剖析

人人网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这些东西
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空•物语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值