1.MyBatis簡介與配置MyBatis+Spring+MySql
1.1MyBatis簡介
MyBatis 是一個可以自定義SQL、存儲過程和高級映射的持久層框架。MyBatis 摒除了大部分的JDBC代碼、手工設置參數和結果集重獲。MyBatis 只使用簡單的XML 和注解來配置和映射基本數據類型、Map 接口和POJO 到數據庫記錄。相對Hibernate和Apache OJB等“一站式”ORM解決方案而言,Mybatis 是一種“半自動化”的ORM實現。
需要使用的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環境
(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數據庫
建立一個學生選課管理數據庫。
表:學生表、班級表、教師表、課程表、學生選課表。
邏輯關系:每個學生有一個班級;每個班級對應一個班主任教師;每個教師只能當一個班的班主任;
使用下面的sql進行建數據庫,先建立學生表,插入數據(2條以上)。
更多sql請下載項目源文件,在resource/sql中。
/* 建立數據庫 */ 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) ); /*插入學生數據*/ INSERT INTO STUDENT_TBL (STUDENT_ID, STUDENT_NAME, STUDENT_SEX, STUDENT_BIRTHDAY, CLASS_ID) VALUES (123456, '某某某', '女', '1980-08-01', 121546 )
創建連接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環境
順序隨便,現在的順序是因為可以盡量的少的修改寫好的文件。
1.2.3.1創建實體類: 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創建數據訪問接口
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創建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> <!-- 查詢學生,根據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創建MyBatis的mapper配置文件
在src/main/resource中創建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。
和數據訪問接口對應的實現bean。通過MapperFactoryBean創建出來。需要執行接口類全稱和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測試,創建一個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 修改操作運行過程細節的重要的步驟。下方這個表格描述了這些設置項、含義和默認值。
| 設置項 | 描述 | 允許值 | 默認值 |
| cacheEnabled | 對在此配置文件下的所有cache 進行全局性開/關設置。 | true | false | true |
| lazyLoadingEnabled | 全局性設置懶加載。如果設為‘false',則所有相關聯的都會被初始化加載。 | true | false | true |
| aggressiveLazyLoading | 當設置為‘true'的時候,懶加載的對象可能被任何懶屬性全部加載。否則,每個屬性都按需加載。 | true | false | true |
| multipleResultSetsEnabled | 允許和不允許單條語句返回多個數據集(取決于驅動需求) | true | false | true |
| useColumnLabel | 使用列標簽代替列名稱。不同的驅動器有不同的作法。參考一下驅動器文檔,或者用這兩個不同的選項進行測試一下。 | true | false | true |
| useGeneratedKeys | 允許JDBC 生成主鍵。需要驅動器支持。如果設為了true,這個設置將強制使用被生成的主鍵,有一些驅動器不兼容不過仍然可以執行。 | true | false | false |
| autoMappingBehavior | 指定MyBatis 是否并且如何來自動映射數據表字段與對象的屬性。PARTIAL將只自動映射簡單的,沒有嵌套的結果。FULL 將自動映射所有復雜的結果。 | NONE, PARTIAL, FULL | PARTIAL |
| defaultExecutorType | 配置和設定執行器,SIMPLE 執行器執行其它語句。REUSE 執行器可能重復使用prepared statements 語句,BATCH執行器可以重復執行語句和批量更新。 | SIMPLE REUSE BATCH | SIMPLE |
| defaultStatementTimeout | 設置一個時限,以決定讓驅動器等待數據庫回應的多長時間為超時 | 正整數 | 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 類型的簡稱。
它僅僅只是關聯到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類型,有許多內建的類型別名。它們都是大小寫不敏感的,由于重載的名字,要注意原生類型的特殊處理。
| 別名 | 映射的類型 |
| _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在預處理語句中設置一個參數,還是從結果集中取出一個值時,類型處理器被用來將獲取的值以合適的方式轉換成Java類型。下面這個表格描述了默認的類型處理器。
| 類型處理器 | Java類型 | JDBC類型 |
| BooleanTypeHandler | Boolean,boolean | 任何兼容的布爾值 |
| ByteTypeHandler | Byte,byte | 任何兼容的數字或字節類型 |
| ShortTypeHandler | Short,short | 任何兼容的數字或短整型 |
| IntegerTypeHandler | Integer,int | 任何兼容的數字和整型 |
| LongTypeHandler | Long,long | 任何兼容的數字或長整型 |
| FloatTypeHandler | Float,float | 任何兼容的數字或單精度浮點型 |
| DoubleTypeHandler | Double,double | 任何兼容的數字或雙精度浮點型 |
| BigDecimalTypeHandler | BigDecimal | 任何兼容的數字或十進制小數類型 |
| StringTypeHandler | String | CHAR和VARCHAR類型 |
| ClobTypeHandler | String | CLOB和LONGVARCHAR類型 |
| NStringTypeHandler | String | NVARCHAR和NCHAR類型 |
| NClobTypeHandler | String | NCLOB類型 |
| ByteArrayTypeHandler | byte[] | 任何兼容的字節流類型 |
| 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-任何兼容的字符串類型,作為代碼存儲(而不是索引)。 |
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 為結果對象創建一個新實例,都會用到ObjectFactory。默認的ObjectFactory 與使用目標類的構造函數創建一個實例毫無區別,如果有已經映射的參數,那也可能使用帶參數的構造函數。
如果你重寫ObjectFactory 的默認操作,你可以通過繼承org.apache.ibatis.reflection.factory.DefaultObjectFactory創建一下你自己的。
ObjectFactory接口很簡單。它包含兩個創建用的方法,一個是處理默認構造方法的,另外一個是處理帶參數構造方法的。最終,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允許你在某一點攔截已映射語句執行的調用。默認情況下,MyBatis允許使用插件來攔截方法調用:
這些類中方法的詳情可以通過查看每個方法的簽名來發現,而且它們的源代碼在MyBatis的發行包中有。你應該理解你覆蓋方法的行為,假設你所做的要比監視調用要多。如果你嘗試修改或覆蓋一個給定的方法,你可能會打破MyBatis的核心。這是低層次的類和方法,要謹慎使用插件。
使用插件是它們提供的非常簡單的力量。簡單實現攔截器接口,要確定你想攔截的指定簽名。
2.7 environments環境
MyBatis 可以配置多個環境。這可以幫助你SQL 映射對應多種數據庫等。
2.8 mappers映射器
這里是告訴MyBatis 去哪尋找映射SQL 的語句。可以使用類路徑中的資源引用,或者使用字符,輸入確切的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>
新聞熱點
疑難解答