FreeMarker demo

SSM+FreeMarker + demo

反向生成实体类

仅作为学习笔记,所以本文可能说的不是那么明了,就像我最初目的并不是为了讲懂读者一样。谅解!

目录结构

FreeMarker  demo

配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
        <!--利用mybatis generator反向生成数据库对应的model实体-->
<generatorConfiguration>
    <!-- mysql-connector文件路径 -->
    <classPathEntry  location="mysql-connector-java-8.0.17.jar"/>

    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- 链接配置 输入数据库名及密码 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" userId="root" password="0000"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/dbName"/>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类的路径,这个路径可以自动生成,但是必须有src这个路径-->
        <!-- 比如 E:\path\to\your\project\src -->
        <javaModelGenerator targetPackage="blog.entity" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成映射的路径,blog.mapXML这个路径可以自动生成,但是必须有src这个路径-->
        <sqlMapGenerator targetPackage="blog.mapXML" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成接口的路径,这个路径可以自动生成,但是必须有src这个路径-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="blog.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 不要改变标签顺序
            The content of element type "context" must match 
             "(property*,plugin*,commentGenerator?,jdbcConnection,javaTypeResolver?,javaModelGenerator,sqlMapGenerator?,javaClientGenerator?,table+)".
            原因:配置文件 generatorConfig.xml 里面的context的子元素必须按照它给出的顺序,如错误提示的match“……”部分。
            ————————————————
            原文链接:https://blog.csdn.net/abcd_d_/article/details/46118209
        -->
        <!-- 表名、实体类名称 -->
        <table tableName="user_main" domainObjectName="UserMain" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
        <table tableName="user_detail" domainObjectName="UserDetail" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
        <table tableName="resource" domainObjectName="Resource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
        <table tableName="user_content" domainObjectName="UserContent" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
        <table tableName="comment" domainObjectName="Comment" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
        <table tableName="login_log" domainObjectName="LoginLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
        <table tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
        <table tableName="role_resource" domainObjectName="RoleResource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
        <table tableName="role_user" domainObjectName="RoleUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
        <table tableName="article_vote" domainObjectName="ArticleVote" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>
执行命令
java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
打开终端

FreeMarker  demo

执行结束
E:\Desktop\blog>java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
MyBatis Generator finished successfully.

E:\Desktop\blog>
生成实体类成功

FreeMarker  demo

demo

ftl模板
注意后面的属性要与实体类里面的属性变量名保持一样
${user}
${user.userId}
${user.userEmail}
${user.userPswd}
${user.userPhone}
${user.userNickName}
${user.userState}
${user.userGraphUrl}
${user.userAvailable}
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-mybatis.xml"})
public class FreeM {
    @Autowired
    private UserMainMapper userMainMapper;

    @Test
    public void testFindUserById() {
        Map<String, Object> model = new HashMap<String, Object>();
        Long id = 1L;
        UserMain userMain = this.userMainMapper.selectByPrimaryKey(id);
        model.put("user", userMain);
        Configuration cof = new Configuration();
        cof.setObjectWrapper(new DefaultObjectWrapper());
        try {
            cof.setTemplateLoader(new FileTemplateLoader(new File("E:/Desktop/blog")));
            //绝对路径:E:/Desktop/blog/src/main/resources/freeMarker/temp.ftl
            Template temp = cof.getTemplate("/src/main/resources/freeMarker/temp.ftl");
            StringWriter writer = new StringWriter();
            temp.process(model, writer);
            System.out.println(writer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        System.err.println(userMain.toString());
    }
}
运行结果
UserMain{Id=1, Email='123456@qq.com', Pswd='0000', Phone='123456', NickName='乔峰', State='1', GraphUrl='null', Available='1'}
1
123456@qq.com
0000
123456
乔峰
1
null
1
上一篇:freemarker


下一篇:使用Spring Framework在Freemarker中表示空值