首页 > 代码库 > JeeSite学习笔记~代码生成原理
JeeSite学习笔记~代码生成原理
1.建立数据模型【单表,一对多表,树状结构表】
用ERMaster建立数据模型,并设定对应表,建立关联关系
2.系统获取对应表原理
1.怎样获取数据库的表
genTableForm.jsp:
<label class="control-label">表名:</label>
<div class="controls">
<form:select path="name" class="input-xxlarge">
<form:options items="${tableList}" itemLabel="nameAndComments" itemValue=http://www.mamicode.com/"name" htmlEscape="false"/>
</form:select>
</div>
GenTableController:
// 获取物理表列表
List<GenTable> tableList = genTableService.findTableListFormDb(new GenTable());
model.addAttribute("tableList", tableList);
GenDataBaseDictDao.xml:
//根据name查询数据库对应的表
<select id="findTableList" resultType="GenTable">
<if test="dbName == ‘oracle‘">
SELECT
t.TABLE_NAME AS name,
c.COMMENTS AS comments
FROM user_tables t, user_tab_comments c
WHERE t.table_name = c.table_name
<if test="name != null and name != ‘‘">
AND t.TABLE_NAME = upper(#{name})
</if>
ORDER BY t.TABLE_NAME
</if>
</select>
注:user_tables:当前用户拥有的表
user_tab_comments:获取表的注释
//判断是否已生成表
SELECT
t.COLUMN_NAME AS name,<!--
t.DATA_TYPE,
t.DATA_LENGTH,
t.DATA_PRECISION,
t.DATA_SCALE, -->
(CASE WHEN t.NULLABLE = ‘Y‘ THEN ‘1‘ ELSE ‘0‘ END) AS isNull,
(t.COLUMN_ID * 10) AS sort,
c.COMMENTS AS comments,
decode(t.DATA_TYPE,‘DATE‘,t.DATA_TYPE || ‘(‘ || t.DATA_LENGTH || ‘)‘,
‘VARCHAR2‘, t.DATA_TYPE || ‘(‘ || t.DATA_LENGTH || ‘)‘,
‘VARCHAR‘, t.DATA_TYPE || ‘(‘ || t.DATA_LENGTH || ‘)‘,
‘NVARCHAR2‘, t.DATA_TYPE || ‘(‘ || t.DATA_LENGTH/2 || ‘)‘,
‘CHAR‘, t.DATA_TYPE || ‘(‘ || t.DATA_LENGTH || ‘)‘,
‘NUMBER‘,t.DATA_TYPE || (nvl2(t.DATA_PRECISION,nvl2(decode(t.DATA_SCALE,0,null,t.DATA_SCALE),
‘(‘ || t.DATA_PRECISION || ‘,‘ || t.DATA_SCALE || ‘)‘,
‘(‘ || t.DATA_PRECISION || ‘)‘),‘(18)‘)),t.DATA_TYPE) AS jdbcType
FROM user_tab_columns t, user_col_comments c
WHERE t.TABLE_NAME = c.table_name
AND t.COLUMN_NAME = c.column_name
<if test="name != null and name != ‘‘">
AND t.TABLE_NAME = upper(#{name})
</if>
ORDER BY t.COLUMN_ID
2.业务表配置,代码生成
代码生成工具类:GenUtils
Xml转 Java类:JaxbMapper
调用方法:
代码生成配置对象:getConfig()
XML文件转换为对象:fileToObject(String fileName, Class<?> clazz)
根据分类获取模板列表:
getTemplateList(GenConfig config, String category, boolean isChildTable)
获取数据模型:getDataModel(GenScheme genScheme)
调用模板XML:config.xml
对应映射:GenConfig.java---config.xml
2.代码生成的路径设置
默认生成路径:D:\src\main
Java代码:D:\src\main\java\com\thinkgem\jeesite\modules
Dao.xml:D:\src\main\resources\mappings\modules
Web代码:D:\src\main\webapp\WEB-INF\views\modules
调用代码生成工具类:GenUtils,
路径生成类:Global,
调用方法:generateToFile(),getProjectPath()
String fileName = Global.getProjectPath() + File.separator
+ StringUtils.replaceEach(FreeMarkers.renderString(tpl.getFilePath() + "/", model),
new String[]{"//", "/", "."}, new String[]{File.separator, File.separator, File.separator})
+ FreeMarkers.renderString(tpl.getFileName(), model);
路径配置设置:jeesite.properties:144行
projectPath=D\:\\workspace\\jeesite
3.菜单配置,权限设置
树状图结构
权限:
访问路径:
@Controller
@RequestMapping(value = "http://www.mamicode.com/${adminPath}/workers/hlTest")
查看权限:
@RequiresPermissions("workers:hlTest:view")
编辑权限:
@RequiresPermissions("workers:hlTest:edit")
生成代码原理总结:
JeeSite生成代码原理主要是:通过JAXBContext的Marshal(解组) 和 UnMarshal(编组)实现java与xml的相互转换,其中把要生成代码的文件写成一个XML模板,再通过IO流一行一行读取模板,再进行转换成相应的文件!
具体参看代码(config.xml,GenUtils.java,GenSchemeService.java)
JeeSite学习笔记~代码生成原理