Heim  >  Artikel  >  Java  >  Erste Schritte mit MyBatis (6) --- Integration von Mybatis und Spring

Erste Schritte mit MyBatis (6) --- Integration von Mybatis und Spring

黄舟
黄舟Original
2016-12-21 14:35:511174Durchsuche

1. Integration benötigt

1.1, Methode

Daten im vorherigen Kapitel

erfordert sPRing zur Verwaltung von SqlsessionFactory

Spring- und Mybatis-Integration generiert Proxy Objekte und verwendet SqlSessionFactory zum Erstellen von SqlSession

(Spring- und Mybatis-Integration werden automatisch abgeschlossen)

Die Mapper der Persistenzschicht müssen von Spring verwaltet werden

2. Erstellen ein Projekt Umgebung integrieren

2.1, Projekt erstellen

Erste Schritte mit MyBatis (6) --- Integration von Mybatis und Spring

2.2, Daten

db.properties

#Datenbankkonfiguration Informationen
#Driver
driverClass=com.MySQL.jdbc.Driver
#Verbindungs-URL
jdbcUrl=jdbc:mysql://localhost:3306/mybatis?character=utf8#Benutzername
Benutzer = root
#Password
passWord=root
#Mindestanzahl der im Verbindungspool beibehaltenen Verbindungen
minPoolSize=10#Maximale Anzahl der im Verbindungspool beibehaltenen Verbindungen. Standard: 15 maxPoolSize=20#Maximale Leerlaufzeit, die Verbindung wird verworfen, wenn sie nicht innerhalb von 1800 Sekunden verwendet wird. Wenn es 0 ist, wird es niemals verworfen. Standard: 0 maxIdletime=1800#Die Anzahl der Verbindungen, die c3p0 gleichzeitig erhält, wenn die Verbindungen im Verbindungspool erschöpft sind. Standard: 3acquireIncrement=3#Die Anzahl der anfänglichen Verbindungen im Verbindungspool sollte zwischen minPoolSize und maxPoolSize liegen. Der Standardwert ist 3
initialPoolSize=15

2.3, Konfiguration

Konfiguration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd" > ;< ;!--Configuration - ->










2.4 POJO-Klassen und Schnittstellen

package com.pb.ssm.po;import java.util.Date;/**
*

* @ClassName: Autor

* @Description: TODO(Autor)

* @Autor Liu Nan

* @Datum 2015 -31.10. 12:39:33 Uhr

**/public class Author {    //作者id
   private Integer authorId;    //作者姓名
   private String authorUserName;    //作者密码
   private String authorPassword;    //作者邮箱
   private String authorEmail;    //作者介绍
   private String authroBio;    //注册时间
   private Date registerTime;     getAuthorUserName() {        return authorUserName;
   }    public void setAuthorUserName(String authorUserName) {        this.authorUserName = authorUserName;
   }    public String getAuthorPassword() {        return authorPassword;
   }    public void setAuthorPassword(String authorPassword) {       this.authorPassword = authorPassword;
   }    public String getAuthorEmail() {        return authorEmail;
   }    public void setAuthorEmail(String authorEmail) {        this.authorEmail = authorEmail;
   }    public String getAuthroBio() {        return ;
   }    public void setAuthroBio( String authroBio) {        this.authroBio = authroBio;
   }    public Date getRegisterTime() {        return registerTime;
   }    public void setRegisterTime(Date registerTime) {        this.registerTime = registerTime;
   }
   @Override public String toString() {        return "Author [authorId=" + authorId + ", authorUserName="
               + authorUserName + ", authorPassword=" +authorPassword                + ", authorEmail=" +authorEmail + ", authroBio=" + authroBio + ", registerTime=" + registerTime + "]";
    🎜>

package com.pb.ssm.mapper;import com.pb.ssm.po.Author;public interface AuthorMapper { /**
*

* @Title: findAuthorById

* @Description: TODO (Suche nach ID)

* @param @param id
* @param @ return Setting file

* @return Author Rückgabetyp

* @throws
*/
public Author findAuthorById(int id); **
*

* @Title: addAuthor

* @Description: TODO(Add)

* @param @param author
* @param @return Set Definierte Datei

* @return int Rückgabetyp

* @throws
*/
public int addAuthor(Autor Autor); /**
*

* @Title: updateAuthor

* @Description: TODO(update)

* @param @param author
* @param @return Set Definierte Datei

* @return int Rückgabetyp

* @throws
*/
public int updateAuthor(Autor Autor); > public int delteAuthor(int id);
}




mapper.xml





< ;?xml version="1.0" binding="UTF-8"?>br/> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http:/ /mybatis.org/dtd/mybatis-3-mapper.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} if>where author_id=#{authorId}delete from author where author_id= #{authorId}

3. Integration mit Mybatis-Konfigurationsdatei.xml

3.1、写applicationContext.xml

< ;!--加载数据库驱动  -->< ;property name="maxIdletime" value="${maxIdletime}"/>

 

3.2、测试

 

Paket com.pb.ssm.mapper;import java.io.InputStream;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session. SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com. pb.ssm.po.Author;public class AuthorMapperTest {    private ApplicationContext applicationContext;

   @Before    public void setUp() throws Exception {
       applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
}

   @Test    public void testFindAuthorById() {
       

       AuthorMapper authorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");
       Author author =. authorMapper.findAuthorBy ID(2 );
       System.out.println(author);
   
   }

   @Test    public void testAddAuthor() {        // 获取会话工厂
       AuthorMapper authorMapper = (AuthorMapper) applicationContext .getBean("authorMapper");
       
       Author author=new Author();
       author.setAuthorUserName("程序猿");
       author.setAuthorPassword("QWERdlfdad");
author.setAuthorEmail("QWER@QQ.com");        
       
       int  num = authorMapper.addAuthor(author);
   
       System.out.println("num="+num);
       System.out.println("添加后的ID : "+author.getAuthorid ()); ;
       Author author = authorMapper.findAuthorById(13);
       author.setAuthroBio("天天写代码");
       author.setAuthorUserName("码农");        int num=authorMapper.updateAuthor(author);
   
       System.out.println("num="+num);
       System.out.println(author);
   }

   @Test    public void testDeleteAuthor() {        // 获取会话工厂
       AuthorMapper authorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");        int num= authorMapper.delteAuthor(13);
       
   }

}


 

 

 

四、不使用mybatis配置文件

4.1、写ApplicationContext.xml

 


< ;!-- 注入数据源  -->

 

更改Mapper.xml,因为不能使用别名,所以type要写POJO类的全路径

br/>  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.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},register_time=#{registerTime}where author_id=#{authorId}< ;/update>vom Autor löschen, wobei author_id=#{authorId}

 

测试类同上

 以上就是MyBatis入门(六)---mybatis与spring的整合的内容,更多相关内容请关注PHP中文网(www.php.cn)! 


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn