봄의 MyBatis 통합 (주 판)

1. 수입에 의존해야

<! - MyBatis로에서 제공 MyBatis로와 Spring 통합 패키지 ->
<의존성>
  <의 groupId> org.mybatis </의 groupId>
  <artifactId를>의 MyBatis 스프링 </ artifactId를>
  <버전> 1.3 . 0 </ 버전>
</ 의존성>
<! - MyBatis로 핵심 jar 파일 ->
<의존성>
  <의 groupId> org.mybatis </의 groupId>
  <artifactId를>의 MyBatis </ artifactId를>
  <버전> 3.4 . 1 </ 버전>
</ 의존성>
코드보기

2.entity 엔티티 클래스

 

 3.dao 층 인터페이스

 

 4.service 인터페이스

 

 5.serviceimpl 구현 클래스

 

 6.Dao.xml 배치

 

 7.jdbc.properties 구성

 

 8.applicationContext.xml 큰 프로필

<? XML 버전 = " 1.0 " 인코딩 = " UTF-8 " ?>
<콩의 xmlns = " http://www.springframework.org/schema/beans " 
       XMLNS : XSI = " http://www.w3.org/2001/XMLSchema-instance "

       XMLNS : AOP = " http://www.springframework.org/schema/aop " 
       XMLNS : 문맥 = " http://www.springframework.org/schema/context " XMLNS : 텍사스 = " HTTP : //www.springframework .ORG / 스키마 / TX "

       XSI :의 schemaLocation = " http://www.springframework.org/schema/beans 
       HTTP : // www.springframework.org/schema/beans/spring-beans.xsd 

        HTTP : // www.springframework.org/schema/aop 
        HTTP : // www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context. XSD  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx.xsd "> 
   <컨텍스트 : 성분 주사 베이스 -package = " com.spring " />
    <! - 내장 된 스프링 구성 데이터 소스 데이터 소스 ->
    <콩 ID = " 는 dataSource "  클래스 = " org.springframework.jdbc.datasource.DriverManagerDataSource " >
        <속성 명 = " driverClassName " 값 = " $ {} jdbc.driver " > </ property>를
        <속성 이름 = " URL " 값 = " $ {jdbc.url} " > </ 부동산>
        <속성 이름 = " 사용자 이름 " 값 = " $ {jdbc.username} " > </ 부동산>
        <속성 이름 = " 암호 " 값 = " $ {jdbc.password} " > </ 부동산>
    </ 콩>

    <! - 프로퍼티 파일의 도입 ->
    <배경 : 속성 - 자리 위치 = " jdbc.properties.properties " />
 <! - & LT; & ndash를 , 등록 DAO 층 : Mapper 프록시 객체의 ndash 및; &! 한다
     <빈 ID = " PersonDao "  클래스 = " org.mybatis.spring.mapper.MapperFactoryBean " >
        <속성 명 = " mapperInterface " 값 = " com.spring.dao.PersonDao " > </ property>를
        <속성 명 = " SqlSessionFactory는 "  REF = " SqlSessionFactory는 " > </ property>를
    </ 콩>
    및 LT;! & ndash를配置서비스层对象& ndash를 & 한다
    <콩 ID = " personService "  클래스 = " com.spring.service.PersonServiceImpl " >
        <속성 명 = " DAO "  REF = " personDao " > </ property>를
    </ 콩> ->
    <! - SqlSessionFactory는 공장 SQLSESSION는 객체를 생성 ->
    <콩 ID = " SqlSessionFactory는 "  클래스 = " org.mybatis.spring.SqlSessionFactoryBean " >
        <속성 명 = " 은 dataSource "  REF = " 은 dataSource " > </ property>를
        <! - MyBatis로 큰 프로필 ->
        <속성 명 = " typeAliasesPackage " 값 = " com.spring.entity " > </ property>를
        <- 스캔 SQL 프로파일 :! 매퍼의 XML 파일이 필요합니다 ->
        <속성 명 = " mapperLocations " 값 = " 클래스 경로 *. 매퍼 / * XML " />
    </ 콩>
    <! - MapperScannerConfigurer 스캔 매퍼 문서 스캐너 ->
    <빈 클래스 = " org.mybatis.spring.mapper.MapperScannerConfigurer " >
        <속성 명 = " basePackage " 값 = " com.spring.dao " > </ property>를
    </ 콩>

    <! - TransactionManager에 트랜잭션 관리자 ->
    <콩 ID = " 의 transactionManager "  클래스 = " org.springframework.jdbc.datasource.DataSourceTransactionManager " >
        <속성 명 = " 은 dataSource "  REF = " 은 dataSource " > </ property>를
    </ 콩>
    <! - 트랜잭션 공지 ->
    <TX : 조언 트랜잭션 매니저 = " 의 transactionManager " ID = " txAdvice " >
        <텍사스 : 속성>
            <! - 시리즈 방법은 트랜잭션 격리 수준 및 전파 동작을 설정하기 ->
            <TX : 방법 이름 = " 얻을 * " 분리 = " READ_COMMITTED " 전파 = " 필수 " />
        </ TX : 속성>
    </ TX : 조언>
</ 콩>
코드보기

9. 테스트 클래스

 

 결과 :

 

추천

출처www.cnblogs.com/mayuan01/p/11797670.html