Java+Spring+MySql環(huán)境中安裝和配置MyBatis的教程
1.MyBatis簡介與配置MyBatis+Spring+MySql
1.1MyBatis簡介
MyBatis 是一個可以自定義SQL、存儲過程和高級映射的持久層框架。MyBatis 摒除了大部分的JDBC代碼、手工設置參數(shù)和結果集重獲。MyBatis 只使用簡單的XML 和注解來配置和映射基本數(shù)據(jù)類型、Map 接口和POJO 到數(shù)據(jù)庫記錄。相對Hibernate和Apache OJB等“一站式”O(jiān)RM解決方案而言,Mybatis 是一種“半自動化”的ORM實現(xiàn)。
需要使用的Jar包:mybatis-3.0.2.jar(mybatis核心包)。mybatis-spring-1.0.0.jar(與Spring結合包)。
下載地址:
http://ibatis.apache.org/tools/ibator
http://code.google.com/p/mybatis/
1.2MyBatis+Spring+MySql簡單配置
1.2.1搭建Spring環(huán)境
(1)建立maven的web項目;
(2)加入Spring框架、配置文件;
(3)在pom.xml中加入所需要的jar包(spring框架的、mybatis、mybatis-spring、junit等);
(4)更改web.xml和spring的配置文件;
(5)添加一個jsp頁面和對應的Controller;
(6)測試。
可參照:http://limingnihao.iteye.com/blog/830409。使用Eclipse的Maven構建SpringMVC項目
1.2.2建立MySql數(shù)據(jù)庫
建立一個學生選課管理數(shù)據(jù)庫。
表:學生表、班級表、教師表、課程表、學生選課表。
邏輯關系:每個學生有一個班級;每個班級對應一個班主任教師;每個教師只能當一個班的班主任;
使用下面的sql進行建數(shù)據(jù)庫,先建立學生表,插入數(shù)據(jù)(2條以上)。
更多sql請下載項目源文件,在resource/sql中。
/* 建立數(shù)據(jù)庫 */
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)
);
/*插入學生數(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。
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)在的順序是因為可以盡量的少的修改寫好的文件。
1.2.3.1創(chuàng)建實體類: StudentEntity
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ù)訪問接口
Student類對應的dao接口:StudentMapper。
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映射語句文件
Student類的sql語句文件StudentMapper.xml
resultMap標簽:表字段與屬性的映射。
Select標簽:查詢sql。
<?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>
<!-- 查詢學生,根據(jù)id -->
<select id="getStudent" parameterType="String" resultType="StudentEntity" resultMap="studentResultMap">
<![CDATA[
SELECT * from STUDENT_TBL ST
WHERE ST.STUDENT_ID = #{studentID}
]]>
</select>
<!-- 查詢學生列表 -->
<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標簽:給類起一個別名。com.manager.data.model.StudentEntity類,可以使用StudentEntity代替。
Mappers標簽:加載MyBatis中實體類的SQL映射語句文件。
<?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ù)訪問接口對應的實現(xiàn)bean。通過MapperFactoryBean創(chuàng)建出來。需要執(zhí)行接口類全稱和SqlSession工廠bean的引用。
<!-- 導入屬性配置文件 -->
<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加入注解
@Repository
@Transactional
public interface StudentMapper {
}
對應的需要在dispatcher-servlet.xml中加入掃描:
<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測試StudentMapper
使用SpringMVC測試,創(chuàng)建一個TestController,配置tomcat,訪問index.do頁面進行測試:
@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測試:
@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時需要指定MyBatis主配置文件:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="dataSource" ref="dataSource" /> </bean>
MyBatis配置文件中大標簽configuration下子標簽包括:
configuration |--- properties |--- settings |--- typeAliases |--- typeHandlers |--- objectFactory |--- plugins |--- environments |--- |--- environment |--- |--- |--- transactionManager |--- |--- |__ dataSource |__ mappers
2.1 properties屬性
properties和java的.properties的配置文件有關。配置properties的resource指定.properties的路徑,然后再在properties標簽下配置property的name和value,則可以替換.properties文件中相應屬性值。
<!-- 屬性替換 --> <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設置
這是MyBatis 修改操作運行過程細節(jié)的重要的步驟。下方這個表格描述了這些設置項、含義和默認值。
|
設置項 |
描述 |
允許值 |
默認值 |
|
cacheEnabled |
對在此配置文件下的所有cache 進行全局性開/關設置。 |
true | false |
true |
|
lazyLoadingEnabled |
全局性設置懶加載。如果設為‘false',則所有相關聯(lián)的都會被初始化加載。 |
true | false |
true |
|
aggressiveLazyLoading |
當設置為‘true'的時候,懶加載的對象可能被任何懶屬性全部加載。否則,每個屬性都按需加載。 |
true | false |
true |
|
multipleResultSetsEnabled |
允許和不允許單條語句返回多個數(shù)據(jù)集(取決于驅動需求) |
true | false |
true |
|
useColumnLabel |
使用列標簽代替列名稱。不同的驅動器有不同的作法。參考一下驅動器文檔,或者用這兩個不同的選項進行測試一下。 |
true | false |
true |
|
useGeneratedKeys |
允許JDBC 生成主鍵。需要驅動器支持。如果設為了true,這個設置將強制使用被生成的主鍵,有一些驅動器不兼容不過仍然可以執(zhí)行。 |
true | false |
false |
|
autoMappingBehavior |
指定MyBatis 是否并且如何來自動映射數(shù)據(jù)表字段與對象的屬性。PARTIAL將只自動映射簡單的,沒有嵌套的結果。FULL 將自動映射所有復雜的結果。 |
NONE, PARTIAL, FULL |
PARTIAL |
|
defaultExecutorType |
配置和設定執(zhí)行器,SIMPLE 執(zhí)行器執(zhí)行其它語句。REUSE 執(zhí)行器可能重復使用prepared statements 語句,BATCH執(zhí)行器可以重復執(zhí)行語句和批量更新。 |
SIMPLE REUSE BATCH |
SIMPLE |
|
defaultStatementTimeout |
設置一個時限,以決定讓驅動器等待數(shù)據(jù)庫回應的多長時間為超時 |
正整數(shù) |
Not Set (null) |
例如:
<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 類型的簡稱。
它僅僅只是關聯(lián)到XML 配置,簡寫冗長的JAVA 類名。例如:
<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>
使用這個配置,“StudentEntity”就能在任何地方代替“com.manager.data.model.StudentEntity”被使用。
對于普通的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類型句柄
無論是MyBatis在預處理語句中設置一個參數(shù),還是從結果集中取出一個值時,類型處理器被用來將獲取的值以合適的方式轉換成Java類型。下面這個表格描述了默認的類型處理器。
|
類型處理器 |
Java類型 |
JDBC類型 |
|
BooleanTypeHandler |
Boolean,boolean |
任何兼容的布爾值 |
|
ByteTypeHandler |
Byte,byte |
任何兼容的數(shù)字或字節(jié)類型 |
|
ShortTypeHandler |
Short,short |
任何兼容的數(shù)字或短整型 |
|
IntegerTypeHandler |
Integer,int |
任何兼容的數(shù)字和整型 |
|
LongTypeHandler |
Long,long |
任何兼容的數(shù)字或長整型 |
|
FloatTypeHandler |
Float,float |
任何兼容的數(shù)字或單精度浮點型 |
|
DoubleTypeHandler |
Double,double |
任何兼容的數(shù)字或雙精度浮點型 |
|
BigDecimalTypeHandler |
BigDecimal |
任何兼容的數(shù)字或十進制小數(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-任何兼容的字符串類型,作為代碼存儲(而不是索引)。 |
你可以重寫類型處理器或創(chuàng)建你自己的類型處理器來處理不支持的或非標準的類型。要這樣做的話,簡單實現(xiàn)TypeHandler接口(org.mybatis.type),然后映射新的類型處理器類到Java類型,還有可選的一個JDBC類型。然后再typeHandlers中添加這個類型處理器。
新定義的類型處理器將會覆蓋已經(jīng)存在的處理Java的String類型屬性和VARCHAR參數(shù)及結果的類型處理器。要注意MyBatis不會審視數(shù)據(jù)庫元信息來決定使用哪種類型,所以你必須在參數(shù)和結果映射中指定那是VARCHAR類型的字段,來綁定到正確的類型處理器上。這是因為MyBatis直到語句被執(zhí)行都不知道數(shù)據(jù)類型的這個現(xiàn)實導致的。
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標簽。
<typeHandlers> <typeHandler javaType="String" jdbcType="VARCHAR" handler="liming.student.manager.type.LimingStringTypeHandler"/> </typeHandlers>
2.5 ObjectFactory對象工廠
每次MyBatis 為結果對象創(chuàng)建一個新實例,都會用到ObjectFactory。默認的ObjectFactory 與使用目標類的構造函數(shù)創(chuàng)建一個實例毫無區(qū)別,如果有已經(jīng)映射的參數(shù),那也可能使用帶參數(shù)的構造函數(shù)。
如果你重寫ObjectFactory 的默認操作,你可以通過繼承org.apache.ibatis.reflection.factory.DefaultObjectFactory創(chuàng)建一下你自己的。
ObjectFactory接口很簡單。它包含兩個創(chuàng)建用的方法,一個是處理默認構造方法的,另外一個是處理帶參數(shù)構造方法的。最終,setProperties方法可以被用來配置ObjectFactory。在初始化你的ObjectFactory實例后,objectFactory元素體中定義的屬性會被傳遞給setProperties方法。
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標簽
<objectFactory type="liming.student.manager.configuration.LimingObjectFactory"> <property name="someProperty" value="100"/> </objectFactory>
2.6 plugins插件
MyBatis允許你在某一點攔截已映射語句執(zhí)行的調用。默認情況下,MyBatis允許使用插件來攔截方法調用:
- Executor(update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
- ParameterHandler(getParameterObject, setParameters)
- ResultSetHandler(handleResultSets, handleOutputParameters)
- StatementHandler(prepare, parameterize, batch, update, query)
這些類中方法的詳情可以通過查看每個方法的簽名來發(fā)現(xiàn),而且它們的源代碼在MyBatis的發(fā)行包中有。你應該理解你覆蓋方法的行為,假設你所做的要比監(jiān)視調用要多。如果你嘗試修改或覆蓋一個給定的方法,你可能會打破MyBatis的核心。這是低層次的類和方法,要謹慎使用插件。
使用插件是它們提供的非常簡單的力量。簡單實現(xiàn)攔截器接口,要確定你想攔截的指定簽名。
2.7 environments環(huán)境
MyBatis 可以配置多個環(huán)境。這可以幫助你SQL 映射對應多種數(shù)據(jù)庫等。
2.8 mappers映射器
這里是告訴MyBatis 去哪尋找映射SQL 的語句??梢允褂妙惵窂街械馁Y源引用,或者使用字符,輸入確切的URL 引用。
例如:
<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>
相關文章
Java實現(xiàn)ip地址和int數(shù)字的相互轉換
這篇文章主要介紹了Java實現(xiàn)ip地址和int數(shù)字的相互轉換,幫助大家更好的利用Java處理數(shù)據(jù),感興趣的朋友可以了解下2020-09-09
在Java生產(chǎn)環(huán)境下進行性能監(jiān)控與調優(yōu)的詳細過程
在Java生產(chǎn)環(huán)境下進行性能監(jiān)控與調優(yōu)是一個復雜但重要的過程,它涉及到多個方面,包括代碼分析、JVM監(jiān)控、線程管理、垃圾收集優(yōu)化、內(nèi)存管理、數(shù)據(jù)庫交互等,下面我將提供一個詳細的概述和示例代碼,需要的朋友可以參考下2025-02-02
淺談web服務器項目中request請求和response的相關響應處理
這篇文章主要介紹了淺談web服務器項目中request請求和response的相關響應處理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07

