国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java+Spring+MySql環境中安裝和配置MyBatis的教程

2019-11-26 14:27:15
字體:
來源:轉載
供稿:網友

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

Booleanboolean

任何兼容的布爾值

ByteTypeHandler

Bytebyte

任何兼容的數字或字節類型

ShortTypeHandler

Shortshort

任何兼容的數字或短整型

IntegerTypeHandler

Integerint

任何兼容的數字和整型

LongTypeHandler

Longlong

任何兼容的數字或長整型

FloatTypeHandler

Floatfloat

任何兼容的數字或單精度浮點型

DoubleTypeHandler

Doubledouble

任何兼容的數字或雙精度浮點型

BigDecimalTypeHandler

BigDecimal

任何兼容的數字或十進制小數類型

StringTypeHandler

String

CHARVARCHAR類型

ClobTypeHandler

String

CLOBLONGVARCHAR類型

NStringTypeHandler

String

NVARCHARNCHAR類型

NClobTypeHandler

String

NCLOB類型

ByteArrayTypeHandler

byte[]

任何兼容的字節流類型

BlobTypeHandler

byte[]

BLOBLONGVARBINARY類型

DateTypeHandler

Datejava.util

TIMESTAMP類型

DateOnlyTypeHandler

Datejava.util

DATE類型

TimeOnlyTypeHandler

Datejava.util

TIME類型

SqlTimestampTypeHandler

Timestampjava.sql

TIMESTAMP類型

SqlDateTypeHandler

Datejava.sql

DATE類型

SqlTimeTypeHandler

Timejava.sql

TIME類型

ObjectTypeHandler

Any

其他或未指定類型

EnumTypeHandler

Enumeration類型

VARCHAR-任何兼容的字符串類型,作為代碼存儲(而不是索引)。


 
你可以重寫類型處理器或創建你自己的類型處理器來處理不支持的或非標準的類型。要這樣做的話,簡單實現TypeHandler接口(org.mybatis.type),然后映射新的類型處理器類到Java類型,還有可選的一個JDBC類型。然后再typeHandlers中添加這個類型處理器。
新定義的類型處理器將會覆蓋已經存在的處理Java的String類型屬性和VARCHAR參數及結果的類型處理器。要注意MyBatis不會審視數據庫元信息來決定使用哪種類型,所以你必須在參數和結果映射中指定那是VARCHAR類型的字段,來綁定到正確的類型處理器上。這是因為MyBatis直到語句被執行都不知道數據類型的這個現實導致的。

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允許使用插件來攔截方法調用:

  • Executor(update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
  • ParameterHandler(getParameterObject, setParameters)
  • ResultSetHandler(handleResultSets, handleOutputParameters)
  • StatementHandler(prepare, parameterize, batch, update, query)

這些類中方法的詳情可以通過查看每個方法的簽名來發現,而且它們的源代碼在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> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盐边县| 温宿县| 永城市| 象山县| 安多县| 汨罗市| 封开县| 陵水| 分宜县| 突泉县| 淮安市| 长丰县| 新乡县| 永定县| 宜兰县| 夹江县| 孝昌县| 庆云县| 都兰县| 临泉县| 专栏| 茌平县| 临洮县| 黄山市| 宁波市| 民权县| 蓝山县| 岳阳县| 贵溪市| 新平| 盈江县| 弥渡县| 湖口县| 长武县| 隆回县| 武胜县| 云南省| 大荔县| 万宁市| 安化县| 万宁市|