龙空技术网

根据模板及表元数据生成mapper.xml配置映射

阿亮坚持 72

前言:

此时小伙伴们对“数据库映射表怎么做”可能比较重视,大家都想要知道一些“数据库映射表怎么做”的相关资讯。那么小编同时在网摘上收集了一些有关“数据库映射表怎么做””的相关知识,希望朋友们能喜欢,姐妹们快快来学习一下吧!

(1),模板mapper_.ftl文件

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ";><mapper namespace="com.dto.<#list mapDataTableComment as listItmCom > ${listItmCom.upperColumnName} </#list>Dao">	<!--用户实体结果映射-->	<resultMap id="ResultMap" type="com.dto.<#list mapDataTableComment as listItmCom > ${listItmCom.upperColumnName} </#list>Dto">			<result column="tid" jdbcType="VARCHAR" property="tid" />			<#list mapDataList as listItm >		<result column="${listItm.columnName}" jdbcType="VARCHAR" property="${listItm.columnName}" />	</#list>			</resultMap>	<!--表字段列表 -->	<sql id="Base_Column_List">		<#list mapDataList as listItm >		${listItm.columnName} ,	</#list>		</sql>	<insert id="<#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list>AddSave" parameterType="com.dto.<#list mapDataTableComment as listItmCom > ${listItmCom.upperColumnName} </#list>Dto">		insert into <#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list> values (				<#list mapDataList as listItm >				井号左大括${listItm.columnName}右大括 ,			</#list>		)	</insert>	<update id="<#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list>ModifySave" parameterType="com.dto.<#list mapDataTableComment as listItmCom > ${listItmCom.upperColumnName} </#list>Dto">		update <#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list> T		<set>					<#list mapDataList as listItm >			<if test="${listItm.columnName} != null and ${listItm.columnName} != '' ">				${listItm.columnName} = 井号左大括${listItm.columnName}, jdbcType=VARCHAR右大括,			</if>		</#list>					</set>	    	where 1 = 1 and tid = 井号左大括tid右大括	</update>	<delete id="<#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list>Del" parameterType="com.dto.<#list mapDataTableComment as listItmCom > ${listItmCom.upperColumnName} </#list>Dto">		delete from <#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list> where 1 = 1 and tid = 井号左大括tid右大括	</delete>	<select id="get<#list mapDataTableComment as listItmCom > ${listItmCom.upperColumnName} </#list>" resultMap="ResultMap" parameterType="com.dto.<#list mapDataTableComment as listItmCom > ${listItmCom.upperColumnName} </#list>Dto">		select		<include refid="Base_Column_List" />		from <#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list> T		where 1 = 1		<if test="tid != null and tid != '' ">			AND tid = 井号左大括tid, jdbcType=VARCHAR右大括		</if>	</select>	<select id="query<#list mapDataTableComment as listItmCom > ${listItmCom.upperColumnName} </#list>List" resultMap="ResultMap" parameterType="com.dto.<#list mapDataTableComment as listItmCom > ${listItmCom.upperColumnName} </#list>Dto">		select		<include refid="Base_Column_List" />		from <#list mapDataTableComment as listItmCom > ${listItmCom.columnName} </#list> T		where 1 = 1			<if test="tid != null and tid != '' ">				AND tid = 井号左大括tid, jdbcType=VARCHAR右大括			</if>						<#list mapDataList as listItm >						<if test="${listItm.columnName} != null and ${listItm.columnName} != '' ">				AND ${listItm.columnName} like concat('%',井号左大括${listItm.columnName},jdbcType=VARCHAR右大括,'%')			</if>		</#list>							</select>	<select id="getMaxid" resultType="java.lang.Integer">		select max(tid) maxTid from t00_user	</select></mapper>

(2),生成映射xml文件

public class TestCls {    public static void main(String[] args) throws Exception {        TestCls testCls = new TestCls();        String testT00_po = "testT00_user";        String generFileName = testT00_po + "Mapper.xml";        testCls.outTemplate(testT00_po, "用户数据"                , generFileName, "mapper_.ftl" );        System.out.println("执行成功!");    }    // 输出到模板    private void outTemplate(String entryInfoPoName, String tableComment            , String templateFileName , String templFileName ) throws Exception {        Configuration configuration = new Configuration();        String templateDir = "F:\\xbl\\myResource\\temp_test_";        configuration.setDirectoryForTemplateLoading(new File(templateDir));        Template template = configuration.getTemplate(templFileName);        Map<String, List<EntryInfoPo>> dataMap = new HashMap<String, List<EntryInfoPo>>();        List<EntryInfoPo> listEntryInfo = new ArrayList<EntryInfoPo>();        listEntryInfo = testMetaAddComment();        dataMap.put("mapDataList", listEntryInfo );        // 表注释        dataMap.put("mapDataTableComment", testTableComment(entryInfoPoName, tableComment ));        String generFileDir = "F:\\xbl\\myResource\\temp_test_\\gener_file_\\";        File generFile = new File(generFileDir + templateFileName);        if(generFile.exists()) {            generFile.delete();        }        generFile.createNewFile();        Writer outputWriter = new OutputStreamWriter(new FileOutputStream(generFile ) );//        Writer outputWriter = new OutputStreamWriter(System.out );        template.process(dataMap, outputWriter);        System.out.println("输出完成!");        outputWriter.close();    }    // 表注释    private List<EntryInfoPo> testTableComment(String entryInfoPoName, String tableComment) throws Exception {        List<EntryInfoPo> listTableComment = new ArrayList<EntryInfoPo>();        EntryInfoPo entryInfoPo = new EntryInfoPo();        entryInfoPo.setColumnName(entryInfoPoName);        entryInfoPo.setColumnComment(tableComment);        entryInfoPo.setUpperColumnName(firTextUpper( entryInfoPoName ) );        listTableComment.add(entryInfoPo);        return listTableComment;    }    // 首字母大写    private String firTextUpper(String scontext ) {        int textLength = scontext.length();        String sufText = scontext.substring(1, textLength);        String firText = scontext.substring(0, 1);        firText = firText.toUpperCase();        String finText = firText + sufText;        return finText;    }    private List<EntryInfoPo> testMetaAddComment() throws Exception {        List<EntryInfoPo> listEntryInfoPo = new ArrayList<EntryInfoPo>();        Connection connection = getDataBaseConnection();        Statement statement = connection.createStatement();        ResultSet resultSet = statement.executeQuery("show full columns from t00_user ");        while (resultSet.next()) {            String columnComment = resultSet.getString("Comment");            String columnName = resultSet.getString("Field");            columnName = columnName.toLowerCase();            EntryInfoPo entryInfoPo = new EntryInfoPo();            entryInfoPo.setColumnName(columnName);            entryInfoPo.setColumnComment(columnComment);            listEntryInfoPo.add(entryInfoPo);        }        return listEntryInfoPo;    }    private Connection getDataBaseConnection() throws Exception {        // 连接数据库        Class.forName("com.mysql.jdbc.Driver");        String databaseUrl = "jdbc:mysql://127.0.0.1:3306/grcdb05?useUnicode=true&characterEncoding=utf8";        Connection connection = DriverManager.getConnection(databaseUrl, "root", "root");        return connection;    }}

(3),注意替换生成的配置文件中的空格及临时替代符号

标签: #数据库映射表怎么做