mybatis-generator和TKmybatis的结合使用
mybatis-generator可以自动生成mapper和entity文件,mybatis-generator有三种用法:命令行、eclipse插件、maven插件。这里使用的是maven插件方式,可以在eclipse/intellij idea等ide上可以通用。
TKmybatis提供一种通用mapper,它不需要针对每个mapper写sql语句,只需继承通用mapper调用相应的接口来完成单表的增删改查。
配置步骤:
1.pom文件中增加代码:
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${mybatis.generator.version}</version> <configuration> <configurationFile>src/main/resources/conf/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mybatis.generator.version}</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${tk.mybatis.version}</version> </dependency> </dependencies> </plugin> <properties> <!-- MyBatis Generator --> <mybatis.generator.version>1.3.2</mybatis.generator.version> <targetJavaProject>${basedir}/src/main/java</targetJavaProject> <targetMapperPackage>com.wdcloud.fayu.mapper</targetMapperPackage> <targetEntityPackage>com.wdcloud.fayu.entity</targetEntityPackage> <!-- tk.mybatis --> <tk.mybatis.version>3.3.6</tk.mybatis.version> <persistence.version>1.0</persistence.version>
<mapper.plugin>tk.mybatis.mapper.generator.MapperPlugin</mapper.plugin> <mapper.Mapper>tk.mybatis.mapper.common.Mapper</mapper.Mapper> </properties> <!--通用Mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${tk.mybatis.version}</version> </dependency> <!-- eitity注解 --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>${persistence.version}</version> </dependency>
2.创建配置文件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> <properties resource="conf/config.properties"/> <!-- 数据库驱动包位置 --> <classPathEntry location="D:\程序\mysql-5.6.26-winx64\mysql-connector-java-5.1.17.jar" /> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <!-- TKmybatis配置 --> <property name="javaFileEncoding" value="UTF-8"/> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <plugin type="${mapper.plugin}"> <property name="mappers" value="${mapper.Mapper}"/> </plugin> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 数据库链接URL、用户名、密码 --> <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}" /> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="${targetEntityPackage}" targetProject="${targetJavaProject}" /> <!-- 生成的映射文件包名和位置 --> <sqlMapGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" /> <!-- 生成service的包名和位置 --> <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" type="XMLMAPPER" /> <!-- 要生成的那些表 --> <table tableName="wd_resource" domainObjectName="WdResource" > <generatedKey column="id" sqlStatement="Mysql" identity="true"/> </table> <table tableName="wd_course" domainObjectName="WdCourse" > <generatedKey column="id" sqlStatement="Mysql" identity="true"/> </table> <table tableName="wd_solr" domainObjectName="WdSolr" > <generatedKey column="id" sqlStatement="Mysql" identity="true"/> </table> </context> </generatorConfiguration>
${targetEntityPackage}等在pom文件的properties中做了配置。
3.修改applicationContext.xml的MapperScannerConfigurer,代码如下:
<!-- 自动扫描注册Mybatis mapper --> <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wdcloud.fayu.mapper" /> </bean> --> <!-- TKmybatis自动扫描注册Mybatis mapper --> <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wdcloud.fayu.mapper"/> </bean>
最后使用maven命令即可使用:
mvn mybatis-generator:generate
配置文件generatorConfig.xml详解:http://blog.csdn.net/isea533/article/details/42102297