Java Web - SM(SpringMVC + Mybatis) 代码自动生成

来源:互联网 发布:大乐透软件破解版 编辑:程序博客网 时间:2024/04/28 05:52

SSH已经过时,SpringMVC比Structs2轻多了,Mybatis比Hibernate轻多了,阿里巴巴的标准架构是 Spring + Mybatis

但是,使用SM还是不方便。对于小的web项目,比如网站后台数据的 API 服务器项目,Spring 和 Mybatis 的xml文件加起来可能比代码都多!

要是那些xml能自动生成就好了!

世界上有很多大牛比你先想到了,而且他们真的做到了。

Mybatis自动生成

文件结构:
这里写图片描述

D:\0_WORK\MYBAITIS-GENERATOR│  generatorConfig.xml│  generatorConfig.xml.bak│  log4j-1.2.15.jar│  mybatis-generator-core-1.3.2.jar│  postgresql-9.4-1201.jdbc4.jar│  run.bat //命令java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite│  slf4j-api-1.7.6.jar│  slf4j-log4j12-1.7.6.jar│└─src    └─out            ProjName.java            ProjNameMapper.java            ProjNameMapper.xml

主配置文件

<?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"><generatorConfiguration>    <classPathEntry    location="用你的 数据库JDBC.jar 代替postgresql-9.4-1201.jdbc4.jar"/>    <context id="DB2Tables"    targetRuntime="MyBatis3">        <commentGenerator>            <property name="suppressDate" value="true"/>            <property name="suppressAllComments" value="true"/>        </commentGenerator>        <jdbcConnection driverClass="org.postgresql.Driver" connectionURL="jdbc:数据库://数据库IP:端口/数据库名" userId="数据库用户" password="密码">        </jdbcConnection>        <javaTypeResolver>            <property name="forceBigDecimals" value="false"/>        </javaTypeResolver>        <javaModelGenerator targetPackage="out" targetProject="src">            <property name="enableSubPackages" value="true"/>            <property name="trimStrings" value="true"/>        </javaModelGenerator>        <sqlMapGenerator targetPackage="out" targetProject="src">            <property name="enableSubPackages" value="true"/>        </sqlMapGenerator>        <javaClientGenerator type="XMLMAPPER" targetPackage="out" targetProject="src">            <property name="enableSubPackages" value="true"/>        </javaClientGenerator>        <!--核心配置,表到实体类的 ORM-->        <table tableName="表名" domainObjectName="要生成的实体名"             enableInsert="false"            enableSelectByPrimaryKey="true"            enableUpdateByPrimaryKey="true"            enableSelectByExample="false"            enableUpdateByExample="false"            enableDeleteByPrimaryKey="false"            enableDeleteByExample="false"            enableCountByExample="false"            selectByExampleQueryId="false"            selectByPrimaryKeyQueryId="false">            <property name="useActualColumnNames" value="true"/>            <property name="constructorBased" value="false"/>        </table>    </context></generatorConfiguration>

详细请看http://www.cnblogs.com/yjmyzz/p/mybatis-generator-tutorial.html

mybatis-generator 有三种用法:命令行、eclipse插件、maven插件。个人觉得maven插件最方便,可以在eclipse/intellij idea等ide上可以通用。

(博主注:使用下来其实还是命令行最方便,因为项目不一定用 maven ,用 ant 的项目多了去了)

关于 mybatis-generator 配置有任何问题可以去看官网文档http://mbg.cndocs.tk/running/running.html

其中,和生成Mapper的方法有关的就是 <table> 标签的属性 enableXXXXX:

enableInsert 指定是否生成Insert语句。
默认值是 true。

enableSelectByPrimaryKey 指定是否生成通过主键查询的语句。 无论这个怎么设置,当表不存在主键的时候,不会生成这个语句。
默认值是 true。

enableSelectByExample 指定是否生成通过Example查询的语句。 这个语句支持运行时生成多种不同条件的动态查询。
默认值是 true。

enableUpdateByPrimaryKey 指定是否生成通过主键更新的语句。 无论这个怎么设置,当表不存在主键的时候,不会生成这个语句。
默认值是 true。

enableDeleteByPrimaryKey 指定是否生成通过主键删除的语句。 无论这个怎么设置,当表不存在主键的时候,不会生成这个语句。
默认值是 true。

enableDeleteByExample 指定是否生成通过Example删除的语句。 这个语句支持运行时生成多种不同的条件动态删除。
默认值是 true。

enableCountByExample 指定是否生成通过Example查询总数的语句。 这个语句将返回满足Example条件的数据总数。
默认值是 true。

enableUpdateByExample 指定是否生成通过Example更新的语句。 这个语句将更新满足Example条件的数据。 如果设置为True,UpdateByExampleSelective语句也会生成。 这个语句只会更新那些参数中值不为null的的列。
默认值是 true。

SpringMVC 代码自动生成

官方插件 - Spring Boot

Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

5分钟构建spring web mvc REST风格HelloWorld

第三方

转自http://www.cnblogs.com/skyme/archive/2011/12/22/2297592.html

1.18 slave4j

slave4j是一个eclipse的插件,也是一个springmvc,spring,hibernate的整合框架

slave4j插件能帮你做什么?

搭建spring,springmvc,hibernate开发环境代码生成:生成curd代码,包括jsp

slave4j框架能帮你做什么?

封装了hibernate常用操作提供了常用的工具类(1.0.0版本提供的比较少,在以后的版本中继续添加)

官方网站:http://code.google.com/p/slave4j/

官方论坛:

版权形式:免费

开源:是

1.19 JunJava

JunJava 一个java程序代码自动生产的工具。

针对JavaEE或者是JAVASE,AutoJava能快速的生成可以由目前最流行的结构框架搭建的项目,如Hibernater,Spring,Struts等自动组合配置。

通过连接数据库,选择任意数据表生成实体模型,支持简单SQL语句查询,直接浏览数据表结构和数据。

支持多项目管理,文件编辑,一键式生成代码。快速构建基础框架代码,并支持MVC 3层构架。

生成的项目可以直接导入Eclipse,并且可以直接运行部署Tomcat服务器中,大大减少程序员的工作量。

官方网站:

官方论坛:

版权形式:免费

开源:是

0 0
原创粉丝点击