SSM整合配置详见博客:http://blog.csdn.net/gebitan505/article/details/44455235/
mybatis generator运用:
利用mybatis-generator自动生成代码
mybatis-generator有三种用法:命令行、eclipse插件、maven插件。个人觉得maven插件最方便,可以在eclipse/intellij idea等ide上可以通用。
下面是从官网上的截图:(不过官网www.mybatis.org 最近一段时间,好象已经挂了)
<build>
<finalName>ars</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
其中generatorConfig.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="/Users/lyf/Documents/eclipseWorkspaceNew/MavenRepository/Mavenjar/postgresql/postgresql/8.2-504.jdbc4/postgresql-8.2-504.jdbc4.jar"/>
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="${jdbc.driverClassName}"
connectionURL="${jdbc.url}" userId="${jdbc.username}"
password="${jdbc.password}"/>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="lyf.test.entity"
targetProject="/Users/lyf/Documents/eclipseWorkspaceNew/ars/src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="lyf.test.entity.xml"
targetProject="/Users/lyf/Documents/eclipseWorkspaceNew/ars/src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator targetPackage="lyf.test.mapper"
targetProject="/Users/lyf/Documents/eclipseWorkspaceNew/ars/src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"/>-->
<table tableName="t11_initfoldertree" domainObjectName="initfoldertree"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<columnRenamingRule searchString="^D_"
replaceString=""/>-->
</table>
</context>
</generatorConfiguration>
如果postgresql数据库中有多个schema 需添加schema="schemaname"
几个要点:
a) 因为生成过程中需要连接db,所以第3行指定了驱动jar包的位置
b) 15-17行为连接字符串
c) 19-33行指定生成“entity实体类、mybatis映射xml文件、mapper接口”的具体位置
d) 40-46行为具体要生成的表,如果有多个表,复制这一段,改下表名即可
三、使用方式
在命令终端 cd到项目所在地址,运行如下命令:
mvn mybatis-generator:generate