1.MyBatis簡(jiǎn)介與配置MyBatis+Spring+MySql
1.1MyBatis簡(jiǎn)介
MyBatis 是一個(gè)可以自定義SQL、存儲(chǔ)過(guò)程和高級(jí)映射的持久層框架。MyBatis 摒除了大部分的JDBC代碼、手工設(shè)置參數(shù)和結(jié)果集重獲。MyBatis 只使用簡(jiǎn)單的XML 和注解來(lái)配置和映射基本數(shù)據(jù)類型、Map 接口和POJO 到數(shù)據(jù)庫(kù)記錄。相對(duì)Hibernate和Apache OJB等“一站式”ORM解決方案而言,Mybatis 是一種“半自動(dòng)化”的ORM實(shí)現(xiàn)。
需要使用的Jar包:mybatis-3.0.2.jar(mybatis核心包)。mybatis-spring-1.0.0.jar(與Spring結(jié)合包)。
下載地址:
http://ibatis.apache.org/tools/ibator
http://code.google.com/p/mybatis/
1.2MyBatis+Spring+MySql簡(jiǎn)單配置
1.2.1搭建Spring環(huán)境
(1)建立maven的web項(xiàng)目;
(2)加入Spring框架、配置文件;
(3)在pom.xml中加入所需要的jar包(spring框架的、mybatis、mybatis-spring、junit等);
(4)更改web.xml和spring的配置文件;
(5)添加一個(gè)jsp頁(yè)面和對(duì)應(yīng)的Controller;
(6)測(cè)試。
可參照:http://limingnihao.iteye.com/blog/830409。使用Eclipse的Maven構(gòu)建SpringMVC項(xiàng)目
1.2.2建立MySql數(shù)據(jù)庫(kù)
建立一個(gè)學(xué)生選課管理數(shù)據(jù)庫(kù)。
表:學(xué)生表、班級(jí)表、教師表、課程表、學(xué)生選課表。
邏輯關(guān)系:每個(gè)學(xué)生有一個(gè)班級(jí);每個(gè)班級(jí)對(duì)應(yīng)一個(gè)班主任教師;每個(gè)教師只能當(dāng)一個(gè)班的班主任;
使用下面的sql進(jìn)行建數(shù)據(jù)庫(kù),先建立學(xué)生表,插入數(shù)據(jù)(2條以上)。
更多sql請(qǐng)下載項(xiàng)目源文件,在resource/sql中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/* 建立數(shù)據(jù)庫(kù) */ CREATE DATABASE STUDENT_MANAGER; USE STUDENT_MANAGER; /***** 建立student表 *****/ CREATE TABLE STUDENT_TBL ( STUDENT_ID VARCHAR (255) PRIMARY KEY , STUDENT_NAME VARCHAR (10) NOT NULL , STUDENT_SEX VARCHAR (10), STUDENT_BIRTHDAY DATE , CLASS_ID VARCHAR (255) ); /*插入學(xué)生數(shù)據(jù)*/ INSERT INTO STUDENT_TBL (STUDENT_ID, STUDENT_NAME, STUDENT_SEX, STUDENT_BIRTHDAY, CLASS_ID) VALUES (123456, '某某某' , '女' , '1980-08-01' , 121546 ) |
創(chuàng)建連接MySql使用的配置文件mysql.properties。
1
2
|
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql: //localhost:3306/student_manager?user=root&password=limingnihao&useUnicode=true&characterEncoding=UTF-8 |
1.2.3搭建MyBatis環(huán)境
順序隨便,現(xiàn)在的順序是因?yàn)榭梢员M量的少的修改寫好的文件。
1.2.3.1創(chuàng)建實(shí)體類: StudentEntity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
public class StudentEntity implements Serializable { private static final long serialVersionUID = 3096154202413606831L; private ClassEntity classEntity; private Date studentBirthday; private String studentID; private String studentName; private String studentSex; public ClassEntity getClassEntity() { return classEntity; } public Date getStudentBirthday() { return studentBirthday; } public String getStudentID() { return studentID; } public String getStudentName() { return studentName; } public String getStudentSex() { return studentSex; } public void setClassEntity(ClassEntity classEntity) { this .classEntity = classEntity; } public void setStudentBirthday(Date studentBirthday) { this .studentBirthday = studentBirthday; } public void setStudentID(String studentID) { this .studentID = studentID; } public void setStudentName(String studentName) { this .studentName = studentName; } public void setStudentSex(String studentSex) { this .studentSex = studentSex; } } |
1.2.3.2創(chuàng)建數(shù)據(jù)訪問(wèn)接口
Student類對(duì)應(yīng)的dao接口:StudentMapper。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public interface StudentMapper { public StudentEntity getStudent(String studentID); public StudentEntity getStudentAndClass(String studentID); public List<StudentEntity> getStudentAll(); public void insertStudent(StudentEntity entity); public void deleteStudent(StudentEntity entity); public void updateStudent(StudentEntity entity); } |
1.2.3.3創(chuàng)建SQL映射語(yǔ)句文件
Student類的sql語(yǔ)句文件StudentMapper.xml
resultMap標(biāo)簽:表字段與屬性的映射。
Select標(biāo)簽:查詢sql。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> < mapper namespace = "com.manager.data.StudentMapper" > < resultMap type = "StudentEntity" id = "studentResultMap" > < id property = "studentID" column = "STUDENT_ID" /> < result property = "studentName" column = "STUDENT_NAME" /> < result property = "studentSex" column = "STUDENT_SEX" /> < result property = "studentBirthday" column = "STUDENT_BIRTHDAY" /> </ resultMap > <!-- 查詢學(xué)生,根據(jù)id --> < select id = "getStudent" parameterType = "String" resultType = "StudentEntity" resultMap = "studentResultMap" > <![CDATA[ SELECT * from STUDENT_TBL ST WHERE ST.STUDENT_ID = #{studentID} ]]> </ select > <!-- 查詢學(xué)生列表 --> < select id = "getStudentAll" resultType = "com.manager.data.model.StudentEntity" resultMap = "studentResultMap" > <![CDATA[ SELECT * from STUDENT_TBL ]]> </ select > </ mapper > |
1.2.3.4創(chuàng)建MyBatis的mapper配置文件
在src/main/resource中創(chuàng)建MyBatis配置文件:mybatis-config.xml。
typeAliases標(biāo)簽:給類起一個(gè)別名。com.manager.data.model.StudentEntity類,可以使用StudentEntity代替。
Mappers標(biāo)簽:加載MyBatis中實(shí)體類的SQL映射語(yǔ)句文件。
1
2
3
4
5
6
7
8
9
10
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> < configuration > < typeAliases > < typeAlias alias = "StudentEntity" type = "com.manager.data.model.StudentEntity" /> </ typeAliases > < mappers > < mapper resource = "com/manager/data/maps/StudentMapper.xml" /> </ mappers > </ configuration > |
1.2.3.5修改Spring 的配置文件
主要是添加SqlSession的制作工廠類的bean:SqlSessionFactoryBean,(在mybatis.spring包中)。需要指定配置文件位置和dataSource。
和數(shù)據(jù)訪問(wèn)接口對(duì)應(yīng)的實(shí)現(xiàn)bean。通過(guò)MapperFactoryBean創(chuàng)建出來(lái)。需要執(zhí)行接口類全稱和SqlSession工廠bean的引用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<!-- 導(dǎo)入屬性配置文件 --> < context:property-placeholder location = "classpath:mysql.properties" /> < bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource" > < property name = "driverClassName" value = "${jdbc.driverClassName}" /> < property name = "url" value = "${jdbc.url}" /> </ bean > < bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "dataSource" /> </ bean > < bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "configLocation" value = "classpath:mybatis-config.xml" /> < property name = "dataSource" ref = "dataSource" /> </ bean > <!— mapper bean --> < bean id = "studentMapper" class = "org.mybatis.spring.MapperFactoryBean" > < property name = "mapperInterface" value = "com.manager.data.StudentMapper" /> < property name = "sqlSessionFactory" ref = "sqlSessionFactory" /> </ bean > |
也可以不定義mapper的bean,使用注解:
將StudentMapper加入注解
1
2
3
4
|
@Repository @Transactional public interface StudentMapper { } |
對(duì)應(yīng)的需要在dispatcher-servlet.xml中加入掃描:
1
2
3
4
5
|
<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "annotationClass" value= "org.springframework.stereotype.Repository" /> <property name= "basePackage" value= "com.liming.manager" /> <property name= "sqlSessionFactory" ref= "sqlSessionFactory" /> </bean> |
1.2.4測(cè)試StudentMapper
使用SpringMVC測(cè)試,創(chuàng)建一個(gè)TestController,配置tomcat,訪問(wèn)index.do頁(yè)面進(jìn)行測(cè)試:
1
2
3
4
5
6
7
8
9
10
11
12
|
@Controller public class TestController { @Autowired private StudentMapper studentMapper; @RequestMapping (value = "index.do" ) public void indexPage() { StudentEntity entity = studentMapper.getStudent( "10000013" ); System.out.println( "name:" + entity.getStudentName()); } } |
使用Junit測(cè)試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@RunWith (value = SpringJUnit4ClassRunner. class ) @ContextConfiguration (value = "test-servlet.xml" ) public class StudentMapperTest { @Autowired private ClassMapper classMapper; @Autowired private StudentMapper studentMapper; @Transactional public void getStudentTest(){ StudentEntity entity = studentMapper.getStudent( "10000013" ); System.out.println( "" + entity.getStudentID() + entity.getStudentName()); List<StudentEntity> studentList = studentMapper.getStudentAll(); for ( StudentEntity entityTemp : studentList){ System.out.println(entityTemp.getStudentName()); } } } |
2.MyBatis的主配置文件
在定義sqlSessionFactory時(shí)需要指定MyBatis主配置文件:
1
2
3
4
|
< bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "configLocation" value = "classpath:mybatis-config.xml" /> < property name = "dataSource" ref = "dataSource" /> </ bean > |
MyBatis配置文件中大標(biāo)簽configuration下子標(biāo)簽包括:
1
2
3
4
5
6
7
8
9
10
11
12
|
configuration |--- properties |--- settings |--- typeAliases |--- typeHandlers |--- objectFactory |--- plugins |--- environments |--- |--- environment |--- |--- |--- transactionManager |--- |--- |__ dataSource |__ mappers |
2.1 properties屬性
properties和java的.properties的配置文件有關(guān)。配置properties的resource指定.properties的路徑,然后再在properties標(biāo)簽下配置property的name和value,則可以替換.properties文件中相應(yīng)屬性值。
1
2
3
4
5
6
7
|
<!-- 屬性替換 --> < properties resource = "mysql.properties" > < property name = "jdbc.driverClassName" value = "com.mysql.jdbc.Driver" /> < property name = "jdbc.url" value = "jdbc:mysql://localhost:3306/student_manager" /> < property name = "username" value = "root" /> < property name = "password" value = "limingnihao" /> </ properties > |
2.2 settings設(shè)置
這是MyBatis 修改操作運(yùn)行過(guò)程細(xì)節(jié)的重要的步驟。下方這個(gè)表格描述了這些設(shè)置項(xiàng)、含義和默認(rèn)值。
設(shè)置項(xiàng) |
描述 |
允許值 |
默認(rèn)值 |
cacheEnabled |
對(duì)在此配置文件下的所有cache 進(jìn)行全局性開(kāi)/關(guān)設(shè)置。 |
true | false |
true |
lazyLoadingEnabled |
全局性設(shè)置懶加載。如果設(shè)為‘false',則所有相關(guān)聯(lián)的都會(huì)被初始化加載。 |
true | false |
true |
aggressiveLazyLoading |
當(dāng)設(shè)置為‘true'的時(shí)候,懶加載的對(duì)象可能被任何懶屬性全部加載。否則,每個(gè)屬性都按需加載。 |
true | false |
true |
multipleResultSetsEnabled |
允許和不允許單條語(yǔ)句返回多個(gè)數(shù)據(jù)集(取決于驅(qū)動(dòng)需求) |
true | false |
true |
useColumnLabel |
使用列標(biāo)簽代替列名稱。不同的驅(qū)動(dòng)器有不同的作法。參考一下驅(qū)動(dòng)器文檔,或者用這兩個(gè)不同的選項(xiàng)進(jìn)行測(cè)試一下。 |
true | false |
true |
useGeneratedKeys |
允許JDBC 生成主鍵。需要驅(qū)動(dòng)器支持。如果設(shè)為了true,這個(gè)設(shè)置將強(qiáng)制使用被生成的主鍵,有一些驅(qū)動(dòng)器不兼容不過(guò)仍然可以執(zhí)行。 |
true | false |
false |
autoMappingBehavior |
指定MyBatis 是否并且如何來(lái)自動(dòng)映射數(shù)據(jù)表字段與對(duì)象的屬性。PARTIAL將只自動(dòng)映射簡(jiǎn)單的,沒(méi)有嵌套的結(jié)果。FULL 將自動(dòng)映射所有復(fù)雜的結(jié)果。 |
NONE, PARTIAL, FULL |
PARTIAL |
defaultExecutorType |
配置和設(shè)定執(zhí)行器,SIMPLE 執(zhí)行器執(zhí)行其它語(yǔ)句。REUSE 執(zhí)行器可能重復(fù)使用prepared statements 語(yǔ)句,BATCH執(zhí)行器可以重復(fù)執(zhí)行語(yǔ)句和批量更新。 |
SIMPLE REUSE BATCH |
SIMPLE |
defaultStatementTimeout |
設(shè)置一個(gè)時(shí)限,以決定讓驅(qū)動(dòng)器等待數(shù)據(jù)庫(kù)回應(yīng)的多長(zhǎng)時(shí)間為超時(shí) |
正整數(shù) |
Not Set (null) |
例如:
1
2
3
4
5
6
7
8
9
|
< settings > < setting name = "cacheEnabled" value = "true" /> < setting name = "lazyLoadingEnabled" value = "true" /> < setting name = "multipleResultSetsEnabled" value = "true" /> < setting name = "useColumnLabel" value = "true" /> < setting name = "useGeneratedKeys" value = "false" /> < setting name = "enhancementEnabled" value = "false" /> < setting name = "defaultExecutorType" value = "SIMPLE" /> </ settings > |
2.3 typeAliases類型別名
類型別名是Java 類型的簡(jiǎn)稱。
它僅僅只是關(guān)聯(lián)到XML 配置,簡(jiǎn)寫冗長(zhǎng)的JAVA 類名。例如:
1
2
3
4
5
|
< typeAliases > < typeAlias alias = "UserEntity" type = "com.manager.data.model.UserEntity" /> < typeAlias alias = "StudentEntity" type = "com.manager.data.model.StudentEntity" /> < typeAlias alias = "ClassEntity" type = "com.manager.data.model.ClassEntity" /> </ typeAliases > |
使用這個(gè)配置,“StudentEntity”就能在任何地方代替“com.manager.data.model.StudentEntity”被使用。
對(duì)于普通的Java類型,有許多內(nèi)建的類型別名。它們都是大小寫不敏感的,由于重載的名字,要注意原生類型的特殊處理。
別名 |
映射的類型 |
_byte |
byte |
_long |
long |
_short |
short |
_int |
int |
_integer |
int |
_double |
double |
_float |
float |
_boolean |
boolean |
string |
String |
byte |
Byte |
long |
Long |
short |
Short |
int |
Integer |
integer |
Integer |
double |
Double |
float |
Float |
boolean |
Boolean |
date |
Date |
decimal |
BigDecimal |
bigdecimal |
BigDecimal |
object |
Object |
map |
Map |
hashmap |
HashMap |
list |
List |
arraylist |
ArrayList |
collection |
Collection |
iterator |
Iterator |
2.4 typeHandlers類型句柄
無(wú)論是MyBatis在預(yù)處理語(yǔ)句中設(shè)置一個(gè)參數(shù),還是從結(jié)果集中取出一個(gè)值時(shí),類型處理器被用來(lái)將獲取的值以合適的方式轉(zhuǎn)換成Java類型。下面這個(gè)表格描述了默認(rèn)的類型處理器。
類型處理器 |
Java類型 |
JDBC類型 |
BooleanTypeHandler |
Boolean,boolean |
任何兼容的布爾值 |
ByteTypeHandler |
Byte,byte |
任何兼容的數(shù)字或字節(jié)類型 |
ShortTypeHandler |
Short,short |
任何兼容的數(shù)字或短整型 |
IntegerTypeHandler |
Integer,int |
任何兼容的數(shù)字和整型 |
LongTypeHandler |
Long,long |
任何兼容的數(shù)字或長(zhǎng)整型 |
FloatTypeHandler |
Float,float |
任何兼容的數(shù)字或單精度浮點(diǎn)型 |
DoubleTypeHandler |
Double,double |
任何兼容的數(shù)字或雙精度浮點(diǎn)型 |
BigDecimalTypeHandler |
BigDecimal |
任何兼容的數(shù)字或十進(jìn)制小數(shù)類型 |
StringTypeHandler |
String |
CHAR和VARCHAR類型 |
ClobTypeHandler |
String |
CLOB和LONGVARCHAR類型 |
NStringTypeHandler |
String |
NVARCHAR和NCHAR類型 |
NClobTypeHandler |
String |
NCLOB類型 |
ByteArrayTypeHandler |
byte[] |
任何兼容的字節(jié)流類型 |
BlobTypeHandler |
byte[] |
BLOB和LONGVARBINARY類型 |
DateTypeHandler |
Date(java.util) |
TIMESTAMP類型 |
DateOnlyTypeHandler |
Date(java.util) |
DATE類型 |
TimeOnlyTypeHandler |
Date(java.util) |
TIME類型 |
SqlTimestampTypeHandler |
Timestamp(java.sql) |
TIMESTAMP類型 |
SqlDateTypeHandler |
Date(java.sql) |
DATE類型 |
SqlTimeTypeHandler |
Time(java.sql) |
TIME類型 |
ObjectTypeHandler |
Any |
其他或未指定類型 |
EnumTypeHandler |
Enumeration類型 |
VARCHAR-任何兼容的字符串類型,作為代碼存儲(chǔ)(而不是索引)。 |
你可以重寫類型處理器或創(chuàng)建你自己的類型處理器來(lái)處理不支持的或非標(biāo)準(zhǔn)的類型。要這樣做的話,簡(jiǎn)單實(shí)現(xiàn)TypeHandler接口(org.mybatis.type),然后映射新的類型處理器類到Java類型,還有可選的一個(gè)JDBC類型。然后再typeHandlers中添加這個(gè)類型處理器。
新定義的類型處理器將會(huì)覆蓋已經(jīng)存在的處理Java的String類型屬性和VARCHAR參數(shù)及結(jié)果的類型處理器。要注意MyBatis不會(huì)審視數(shù)據(jù)庫(kù)元信息來(lái)決定使用哪種類型,所以你必須在參數(shù)和結(jié)果映射中指定那是VARCHAR類型的字段,來(lái)綁定到正確的類型處理器上。這是因?yàn)镸yBatis直到語(yǔ)句被執(zhí)行都不知道數(shù)據(jù)類型的這個(gè)現(xiàn)實(shí)導(dǎo)致的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class LimingStringTypeHandler implements TypeHandler { @Override public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException { System.out.println( "setParameter - parameter: " + ((String) parameter) + ", jdbcType: " + jdbcType.TYPE_CODE); ps.setString(i, ((String) parameter)); } @Override public Object getResult(ResultSet rs, String columnName) throws SQLException { System.out.println( "getResult - columnName: " + columnName); return rs.getString(columnName); } @Override public Object getResult(CallableStatement cs, int columnIndex) throws SQLException { System.out.println( "getResult - columnIndex: " + columnIndex); return cs.getString(columnIndex); } } |
在配置文件的typeHandlers中添加typeHandler標(biāo)簽。
1
2
3
|
< typeHandlers > < typeHandler javaType = "String" jdbcType = "VARCHAR" handler = "liming.student.manager.type.LimingStringTypeHandler" /> </ typeHandlers > |
2.5 ObjectFactory對(duì)象工廠
每次MyBatis 為結(jié)果對(duì)象創(chuàng)建一個(gè)新實(shí)例,都會(huì)用到ObjectFactory。默認(rèn)的ObjectFactory 與使用目標(biāo)類的構(gòu)造函數(shù)創(chuàng)建一個(gè)實(shí)例毫無(wú)區(qū)別,如果有已經(jīng)映射的參數(shù),那也可能使用帶參數(shù)的構(gòu)造函數(shù)。
如果你重寫ObjectFactory 的默認(rèn)操作,你可以通過(guò)繼承org.apache.ibatis.reflection.factory.DefaultObjectFactory創(chuàng)建一下你自己的。
ObjectFactory接口很簡(jiǎn)單。它包含兩個(gè)創(chuàng)建用的方法,一個(gè)是處理默認(rèn)構(gòu)造方法的,另外一個(gè)是處理帶參數(shù)構(gòu)造方法的。最終,setProperties方法可以被用來(lái)配置ObjectFactory。在初始化你的ObjectFactory實(shí)例后,objectFactory元素體中定義的屬性會(huì)被傳遞給setProperties方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class LimingObjectFactory extends DefaultObjectFactory { private static final long serialVersionUID = -399284318168302833L; @Override public Object create(Class type) { return super .create(type); } @Override public Object create(Class type, List<Class> constructorArgTypes, List<Object> constructorArgs) { System.out.println( "create - type: " + type.toString()); return super .create(type, constructorArgTypes, constructorArgs); } @Override public void setProperties(Properties properties) { System.out.println( "setProperties - properties: " + properties.toString() + ", someProperty: " + properties.getProperty( "someProperty" )); super .setProperties(properties); } } |
配置文件中添加objectFactory標(biāo)簽
1
2
3
|
< objectFactory type = "liming.student.manager.configuration.LimingObjectFactory" > < property name = "someProperty" value = "100" /> </ objectFactory > |
2.6 plugins插件
MyBatis允許你在某一點(diǎn)攔截已映射語(yǔ)句執(zhí)行的調(diào)用。默認(rèn)情況下,MyBatis允許使用插件來(lái)攔截方法調(diào)用:
- Executor(update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
- ParameterHandler(getParameterObject, setParameters)
- ResultSetHandler(handleResultSets, handleOutputParameters)
- StatementHandler(prepare, parameterize, batch, update, query)
這些類中方法的詳情可以通過(guò)查看每個(gè)方法的簽名來(lái)發(fā)現(xiàn),而且它們的源代碼在MyBatis的發(fā)行包中有。你應(yīng)該理解你覆蓋方法的行為,假設(shè)你所做的要比監(jiān)視調(diào)用要多。如果你嘗試修改或覆蓋一個(gè)給定的方法,你可能會(huì)打破MyBatis的核心。這是低層次的類和方法,要謹(jǐn)慎使用插件。
使用插件是它們提供的非常簡(jiǎn)單的力量。簡(jiǎn)單實(shí)現(xiàn)攔截器接口,要確定你想攔截的指定簽名。
2.7 environments環(huán)境
MyBatis 可以配置多個(gè)環(huán)境。這可以幫助你SQL 映射對(duì)應(yīng)多種數(shù)據(jù)庫(kù)等。
2.8 mappers映射器
這里是告訴MyBatis 去哪尋找映射SQL 的語(yǔ)句??梢允褂妙惵窂街械馁Y源引用,或者使用字符,輸入確切的URL 引用。
例如:
1
2
3
4
5
|
< mappers > < mapper resource = "com/manager/data/maps/UserMapper.xml" /> < mapper resource = "com/manager/data/maps/StudentMapper.xml" /> < mapper resource = "com/manager/data/maps/ClassMapper.xml" /> </ mappers > |