Mybatis應用學習(6)——Spring框架整合與逆向工程 原
1. 與Spring框架整合
1. 整合思路:
- 需要spring通過單例方式管理SqlSessionFactory。
- spring和mybatis整合生成代理物件,使用SqlSessionFactory建立SqlSession。(spring和mybatis整合自動完成)
- 持久層的mapper都需要由spring進行管理。
2. 準備環境:
- 主要需要的jar包為:Spring相關jar包、Mybatis相關jar包、mybatis和spring的整合包(mybatis-spring)
- Spring的配置檔案中的相關配置:
<!--配置資料來源--> <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>
2. Mybatis逆向工程
1. mybaits需要程式員自己編寫sql語句,mybatis官方提供逆向工程可以針對單表自動生成mybatis執行所需要的程式碼(mapper.java,mapper.xml、po..)
2. 使用方式:
- 首先需要匯入包,可以通過Maven匯入,然後複製貼上逆向工程的程式碼以及配置檔案
- 配置檔案:
<?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>
- 逆向工程java程式碼:
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(); } } }