mybaits generator自动生成model、xml、dao

来源:互联网 发布:农村淘宝发展前景分析 编辑:程序博客网 时间:2024/05/01 12:25

1.添加包

<!--mybaties-generator  -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mybaties -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>

2.添加配置文件org/mybatis/generator/generatorConfiguration.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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<!-- 自定义的分页插件 -->
<plugin type="org.mybatis.generator.plugins.SelectListPlugin" />
<!-- 是否需要自动生成注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.0.2:3306/da?characterEncoding=utf8" userId="root"
password="root">
</jdbcConnection>
<!-- 
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@192.168.0.2:1521:orcl" userId="root"
password="root">
</jdbcConnection>
-->


<javaTypeResolver>
<property name="forceBigDecimals" value="true" />
</javaTypeResolver>
<!-- model生成位置 -->
<javaModelGenerator targetPackage="com.zefu.ims.model"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀  -->    
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格  --> 
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- xml生成位置 -->
<sqlMapGenerator targetPackage="com.zefu.ims.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- dao生成目录 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.zefu.ims.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 表配置 -->
<table tableName="t_dev_details" domainObjectName="DeviceDetails"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<generatedKey column="details_id"
sqlStatement="select replace(uuid(),'-','') from dual" />
</table>
</context>
</generatorConfiguration> 

3.main方法org/mybatis/generator/MybatisGenerator.java

package org.mybatis.generator;


import java.net.URISyntaxException;


import org.mybatis.generator.api.ShellRunner;


/**
 * <pre>
 * add pagination using mysql limit. 
 * This class is only used in ibator code generator.
 * </pre>
 */
public class MybatisGenerator {
public static void main(String[] args) {
System.out.println("开始生成代码...");
MybatisGenerator t = new MybatisGenerator();
String config = "generatorConfiguration.xml";
try {
config = t.getClass().getResource(config).toURI().getPath();
} catch (URISyntaxException e) {
e.printStackTrace();
}


System.out.println("完整的配置文件路径:" + config);
String[] arg = { "-configfile", config, "-overwrite" };
ShellRunner.main(arg);
System.out.println("完成。");
}
}

4.spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc  
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx    
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> 
<!-- 定义自动扫描Classpath路径,扫描带有@Component/@Service/@Repository的pojo加入applicationContext, 
并根据@Autoired/@Qualifier 与 @Resource进行IOC配置,并对@PostConstruct 和 @PreDestroy和@Requried进行处理。 -->
<context:component-scan base-package="com.zefu.ims" />
<!--
 配置容器后处理器,Spring容器将从属性文件中搜索这些key对应的value
 setter使用@Value(${jdbc.url}) 
 配置在spring.xml 中   从applicationContext.xml 分离出spring.xml和spring-hibernate.xml
 为了区分以后可能会有什么spring-mybatis.xml等等
-->
<!-- 
<context:property-placeholder location="classpath:META-INF/resources/database.properties"/>
-->




<!--jdbc dataSource-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>        
<property name="url" value="jdbc:mysql://192.168.0.2:3306/mo?characterEncoding=utf8"/>         
<property name="username" value="user"/>         
<property name="password" value="root"/>
<!-- 
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>        
<property name="url" value="jdbc:oracle:thin:@192.168.0.2:1521:orcl"/>         
<property name="username" value="user"/>         
<property name="password" value="root"/>
-->               
<!-- 
<property name="driverClassName" value="${datasource.driverClassName}"/>        
<property name="url" value="${datasource.url}"/>         
<property name="username" value="${datasource.username}"/>         
<property name="password" value="${datasource.password}"/> 
-->

<!--      
<property name="initialSize" value="${datasource.initialSize}"/>          
<property name="maxActive" value="${datasource.maxActive}"/>       
<property name="maxIdle" value="${datasource.maxIdle}"/>          
<property name="minIdle" value="${datasource.minIdle}"/>       
-->
</bean>        

<!-- 事务管理器 
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  -->  
    
<!-- ############## MyBatis3配置 begin ############## -->
<!-- MyBatis 执行 SQL 操作工厂支持对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:META-INF/resources/mybatis-config.xml" />
<!-- 定义Mybatis的Session工厂,typeAliasesPackage为entity包全名,多个用逗号或者分号隔开 -->
<property name="typeAliasesPackage" value="com.zefu.ims.model" />
<property name="mapperLocations"
value="classpath:com/zefu/ims/mapper/*Mapper.xml" />

</bean>
<!-- 定义所要扫描的Mapper配置文件包路径,多个包用逗号或者分号隔开 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="basePackage" value="com.zefu.ims.mapper" />
</bean>



</beans>

0 0
原创粉丝点击