首頁  >  文章  >  Java  >  MyBatis入門(六)---mybatis與spring的整合

MyBatis入門(六)---mybatis與spring的整合

黄舟
黄舟原創
2016-12-21 14:35:511174瀏覽

一、整合需要

1.1、方法

上一章中的資料

需要sPRing透過單例方式管理SqlsessionFactoryFactoryFactor

spring和mybatis整合來產生代理對象,使用SqlsessionFactoryFactorySmlSessionkand.完成)

持久層的mapper都需要由spring進行管理

二、建立專案整合環境

2.1、建立專案

2.2、資料配置資訊MyBatis入門(六)---mybatis與spring的整合

2.2、資料配置資訊


driverClass=com.MySQL.jdbc.Driver
#連接url
jdbcUrl=jdbc:mysql://localhost:3306/mybatis?character=utf8#使用者名稱
user=root
#密碼
passWord=p中保留的最小連線數
minPoolSize=10#連線池中保留的最大連線數。 Default: 15 maxPoolSize=20#最大空閒時間,1800秒內未使用則連線被丟棄。若為0則永不丟棄。 Default: 0 maxIdletime=1800#當連線池中的連線耗盡的時候c3p0一次同時取得的連線數。 Default: 3acquireIncrement=3#連線池中初始化連線數 應在minPoolSize與maxPoolSize之間取值。預設為3
initialPoolSize=15


 

2.3、confinguration

"1.0" encoding="UTF-8"?>"1.0" encoding="UTF-8"?>「P層Config 3.0//EN"

 "http://mybatis.org/dtd/mybatis-3-config.dtd">

   
   
   INSERT INTO author(author_username,author_password,author_email,author_bio)
VALUES(#{authorUserName},#{authorPassword},#{authorEmail},#{authroBio})
update authorauthor_username=#{authorUserName},author_password=#{authorPassword},author_email=#{authorEmail},author_bio=#{authroBio},register_time=#{registerTime} set>where author_id=#{authorId}delete from author where author_id=#{authorId} mapper>


 

三、使用Mybatis設定檔.xml整合

3.1、寫applicationContext.xml

;>

 

3.2、測試

 

套件com.pb.ssm.mapper; 導入java.io.InputStream; 導入 Iceg.apache.ibatis.io.Resources; 導入o​​rg.apache.ibatis.session.SqlSession; 導入og .apache.ibatis.session.SqlSessionFactory;導入org.apache.ibatis.session.SqlSessionFactoryBuilder;導入org.junit.Before;導入org.junit.Test;導入org.springframework.context.ApplicationConfrtext;導入org.spring.Testwork.context. support.ClassPathXmlApplicationContext;導入com.pb。 =new ClassPathXmlApplicationContext("ApplicationContext.xml");
   }

   @Test    public void testFindAuthorBy編號( ) {
       

     遠   authorauthor =authorMapper.findAuthorById(2);
       System.out.println(author);
   
}

   @Test    public void testAddAuthor () {        //取得會話工廠
       AuthorMapperauthorMapper = (AuthorMapper)
       authorauthor=new Author();
       author.setAuthorUserName("程式猿") ;
    author .setAuthorPassword(" QWERdlfdad");
       author.setAuthorEmail("QWER@QQ.com");        
       
       int  num =authorMapper.addAuthor(author);
   
     1System.="的ID:"+author.getAuthorId());
   }

   @Test    public void testUpdateAuthor() {        //取得會話工廠
apper          Author writer =authorMapper.findAuthorById(13);
       author.setAuthroBio("天天寫代碼");
       author.setAuthorUserName("碼農");        int num=authorMapper.updateAuthor(author);
   
       System.out.println("num="+num);
    public void testDeleteAuthor() {        //取得會話工廠
       AuthorMapperauthorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");        int num=authorMapper.delteAuthor(13);
       
   }

}


 Context.xml

 

bean> ;
其中author_id=#{id}插入作者(author_username,author_password,author_email,author_UEbio)
#UEbio( {authorPassword},#{authorEmail},#{authroBio})> ;更新作者author_username= #{authorUserName},author_password=#{authorPassword}, author_email=#{authorEmail},author_bio=#{authroBio },register_time=#{registerTime}whereauthor_id=#{authorId}從作者中刪除,其中author_id=#{authorId}

 

測試類別同上

--mybatis與spring的整合的內容,更多相關內容請關注PHP中文網(www.php.cn)! 


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn