首先要搭建的是Spring+MyBatis的整合框架,畢竟Spring是整個Web框架的核心部位,而數據庫操作是一切測試的基礎嘛。
━java
┣ controller(控制層)
┣ mapper(因為沒有Dao,用Mapper層替代持久層)
┣ pojo(基礎模型層)
┣ service(業務層)
┗ util(通用工具)
━resource
┣config
┣mybatis(MyBatis配置,其實這裡的配置文件啥內容也沒有)
┣spring(Spring的配置)
┗mapper(用於存放Mybatis生成的mapper接口對應的xml配置文件)
目錄:Resource/Config/mybatis,文件名:SqlMapConfig.xml
新建一個配置文件,內容如下:

目錄:resource/config,文件名:db.properties
其中“db_house_rent”指的是你的數據庫名稱,直接替換即可,比如你的數據庫叫"abc", 那你就改成:jdbc:mysql://127.0.0.1:3306/abc?characterEncoding=UTF-8。
1 jdbc.driver = com.mysql.jdbc.Driver 2 jdbc.url = jdbc:mysql://127.0.0.1:3306/db_house_rent?characterEncoding=UTF-8 3 jdbc.username = root 4 jdbc.password = 123456
真正的MyBatis配置文件
目錄:resource/config/spring,文件名:applicationContext-dao.xml
當然你也可以為了方便記憶,把文件名的dao改成mybatis,不影響我們後續的配置,但本文章的配置文件的文件名,是有統一格式要求的,後續會說為什麼,但必須按照applicationContext-xxx.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--獲取數據庫配置文件-->
<context:property-placeholder location="classpath:config/db.properties"/>
<!--設置數據源c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="50"/>
<property name="minPoolSize" value="2"/>
<property name="maxIdleTime" value="60"/>
</bean>
<!--sqlsessionFactory bean-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/mybatis/SqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
<!-- 顯式指定Mapper文件位置,如果這裡你不是按照我的目錄結構來建設,那你要自己調整mapper的路徑,這裡說的mapper不是指java類,而是指對應的xml文件,如果不懂,先去學一下Mybatis的基本知識 -->
<property name="mapperLocations">
<list>
<value>classpath*:/mapper/*.xml</value>
</list>
</property>
</bean>
<!--自動掃描mapper接口,並注入sqlsession,這裡順帶一提,因為這裡自動掃描注冊了,所以我們在mapper接口中,不需要使用注解@Repository去標注那些接口,同時也不要在配置文件中加入自動掃描context:component-scan的標簽去掃描mapper包,否則會有矛盾-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.magic.rent.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSession"/>
</bean>
</beans>
目錄:resource,文件名:generatorConfig.xml
這個配置文件,是用於MyBatis的逆向工程的,這個雖然不屬於本框架的內容,但是也算是Mybatis的一個非常好用的插件,所以也一並加到教程中來。
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE generatorConfiguration
3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
5
6 <generatorConfiguration>
7 <classPathEntry
8 location="/Users/wuxinzhe/.m2/repository/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar"/>
9 <context id="testTables" targetRuntime="MyBatis3">
10 <commentGenerator>
11 <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
12 <property name="suppressAllComments" value="true"/>
13 </commentGenerator>
14 <!--數據庫連接的信息:驅動類、連接地址、用戶名、密碼 -->
15 <!--<jdbcConnection driverClass="${jdbc.driver}"-->
16 <!--connectionURL="${jdbc.url}"-->
17 <!--userId="${jdbc.username}"-->
18 <!--password="${jdbc.password}">-->
19 <!--</jdbcConnection>-->
20 <jdbcConnection driverClass="com.mysql.jdbc.Driver"
21 connectionURL="jdbc:mysql://127.0.0.1:3306/db_house_rent?characterEncoding=UTF-8"
22 userId="root"
23 password="199176">
24 </jdbcConnection>
25
26 <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer,為 true時把JDBC DECIMAL 和
27 NUMERIC 類型解析為java.math.BigDecimal -->
28 <javaTypeResolver>
29 <property name="forceBigDecimals" value="false"/>
30 </javaTypeResolver>
31
32 <!-- targetProject:生成PO類的位置 -->
33 <javaModelGenerator targetPackage="mybatis.pojo"
34 targetProject="src/test/java">
35 <!-- enableSubPackages:是否讓schema作為包的後綴 -->
36 <property name="enableSubPackages" value="false"/>
37 <!-- 從數據庫返回的值被清理前後的空格 -->
38 <property name="trimStrings" value="true"/>
39 </javaModelGenerator>
40 <!-- targetProject:mapper映射文件生成的位置 -->
41 <sqlMapGenerator targetPackage="mybatis.mapper"
42 targetProject="src/test/java">
43 <!-- enableSubPackages:是否讓schema作為包的後綴 -->
44 <property name="enableSubPackages" value="false"/>
45 </sqlMapGenerator>
46 <!-- targetPackage:mapper接口生成的位置 -->
47 <javaClientGenerator type="XMLMAPPER"
48 targetPackage="mybatis.mapper"
49 targetProject="src/test/java">
50 <!-- enableSubPackages:是否讓schema作為包的後綴 -->
51 <property name="enableSubPackages" value="false"/>
52 </javaClientGenerator>
53 <!-- 指定數據庫表,這些數據庫表文件,是SpringSecurity的必備表,後續的文章會貼出SQL文件 -->
54 <table tableName="SYS_USERS"
55 enableCountByExample="false"
56 enableUpdateByExample="false"
57 enableDeleteByExample="false"
58 enableSelectByExample="false"
59 selectByExampleQueryId="false"/>
60 <table tableName="SYS_ROLES"
61 enableCountByExample="false"
62 enableUpdateByExample="false"
63 enableDeleteByExample="false"
64 enableSelectByExample="false"
65 selectByExampleQueryId="false"/>
66 <table tableName="SYS_AUTHORITIES"
67 enableCountByExample="false"
68 enableUpdateByExample="false"
69 enableDeleteByExample="false"
70 enableSelectByExample="false"
71 selectByExampleQueryId="false"/>
72 <table tableName="SYS_MODULES"
73 enableCountByExample="false"
74 enableUpdateByExample="false"
75 enableDeleteByExample="false"
76 enableSelectByExample="false"
77 selectByExampleQueryId="false"/>
78 <table tableName="SYS_RESOURCES"
79 enableCountByExample="false"
80 enableUpdateByExample="false"
81 enableDeleteByExample="false"
82 enableSelectByExample="false"
83 selectByExampleQueryId="false"/>
84 <table tableName="PERSISTENT_LOGINS"
85 enableCountByExample="false"
86 enableUpdateByExample="false"
87 enableDeleteByExample="false"
88 enableSelectByExample="false"
89 selectByExampleQueryId="false"/>
90 </context>
91 </generatorConfiguration>
到此我要做一個解釋,就是mapper逆向工程生成的文件,不是直接替換到java工程中,而是在本工程下,我建立的一個test目錄下,這個專門用於寫測試的,之所以要這麼干,是因為如果當你修改數據庫的時候,生成的文件會直接覆蓋已有的文件,如果有修改這個自動生成的文件,那就會被覆蓋掉,所以以防萬一,還是這樣比較方便,每次生成之後,對比一下再把新增的或修改的手動去替換。
━java
┣ mybatis(用於存放生成的逆向工程文件)
┣ com.magic.rent.service(這個是用JUnit自動生成的測試類,回頭會說。)
┣ mapper(存放生成的mapper.java和mapper.xml文件)
┣ poco(存放生成的映射對象文件)
┗ test(很隨意的測試都在這裡,比如字符串截取之類的...)
[如圖,測試類的包,要在IDEA的配置中,設置為“綠色的測試資源”]
[IDEA對項目工程的包有一些分門別類的設置,不懂的朋友自行百度了]
[說實話,我是一個顏值黨,Eclipse實在看不下去,所以喜歡IDEA沒辦法]
既然配置完了,就是要怎麼用了。打開IDEA右側欄的Maven Project,找到插件組(Plugins)中的mybatis-generator.根據標號的操作步驟,找到了以後,直接“雙擊”即可。如果期間出了什麼錯,控制台會直接打印出錯誤原因,英語不好的朋友可以百度翻譯。
