1. 整合思路:java
2. 準備環境:mysql
<!--配置數據源--> <context:property-placeholder location="classpath:conf/db.properties"/> <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="password" value="${jdbc.password}"></property> <property name="username" value="${jdbc.name}"></property> </bean> <!--配置sqlSessionFactory--> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 配置mybatis的數據源 --> <property name="dataSource" ref="datasource"></property> <!-- 加載mybatis的配置文件 --> <property name="configLocation" value="mybatis/SqlMapConfig.xml" /> <!-- mapperLocations表示指定Mapper映射文件所在的類路徑,用*通配符掃描該路徑下的全部xml文件 若是SqlMapConfig.xml中已經指定,則能夠不用寫 <property name="mapperLocations" value="classpath:mapper/*.xml"></property> --> </bean> <!-- 經過MapperScannerConfigurer進行Mapper接口批量掃描,並生成Mapper接口的代理實現類,生成的代理實現類的id是Mapper接口類名的首字母小寫 basePackage指定Mapper接口所在的包 sqlSessionFactoryBeanName指定配置完成的sqlSessionFactoryBean的id --> <bean id="mfc" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.mapper"></property> <property name="sqlSessionFactoryBeanName" value="ssf"></property> </bean>
1. mybaits須要程序員本身編寫sql語句,mybatis官方提供逆向工程能夠針對單表自動生成mybatis執行所須要的代碼(mapper.java,mapper.xml、po..)程序員
2. 使用方式:spring
<?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="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--數據庫鏈接的信息:驅動類、鏈接地址、用戶名、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/customer?useUnicode=true&characterEncoding=utf8" userId="root" password="123456"> </jdbcConnection> <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg" password="yycg"> </jdbcConnection> --> <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer,爲 true時把JDBC DECIMAL 和 NUMERIC 類型解析爲java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO類的位置,主要修改 --> <javaModelGenerator targetPackage="com.customer.pojo" targetProject=".\src"> <!-- enableSubPackages:是否讓schema做爲包的後綴 --> <property name="enableSubPackages" value="false" /> <!-- 從數據庫返回的值被清理先後的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.customer.mapper" targetProject=".\src"> <!-- enableSubPackages:是否讓schema做爲包的後綴 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.customer.mapper" targetProject=".\src"> <!-- enableSubPackages:是否讓schema做爲包的後綴 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定數據庫表--> <table tableName="tb_content"></table> <table tableName="tb_content_category"></table> <table tableName="tb_item"></table> <table tableName="tb_item_cat"></table> <table tableName="tb_item_desc"></table> <table tableName="tb_item_param"></table> <table tableName="tb_item_param_item"></table> <table tableName="tb_order"></table> <table tableName="tb_order_item"></table> <table tableName="tb_order_shipping"></table> <table tableName="tb_user"></table> <!-- 有些表的字段須要指定java類型 <table schema="" tableName=""> <columnOverride column="" javaType="" /> </table> --> </context> </generatorConfiguration>
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; public class GeneratorSqlmap { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 File configFile = new File("D:/java project/MybatisOthers/src/main/java/Generater/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } public static void main(String[] args) throws Exception { File file=new File("."); System.out.println(file.getAbsolutePath()); try { GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }