ホームページ  >  記事  >  Java  >  MyBatis 入門 (6)---mybatis と Spring の統合

MyBatis 入門 (6)---mybatis と Spring の統合

黄舟
黄舟オリジナル
2016-12-21 14:35:511174ブラウズ

1. 統合には

1.1、メソッドが必要です

前の章のデータには、シングルトンを通じて SqlsessionFactory を管理するための sPRing が必要です

spring と mybatis の統合はプロキシ オブジェクトを生成し、SqlSessionFactory を使用して SqlSession を作成します

(spring と mybatis の統合)自動的に完了)

永続層のマッパーはspring

2で管理する必要がある。プロジェクト統合環境の作成

2.1、プロジェクトの作成

MyBatis 入門 (6)---mybatis と Spring の統合2.2、data

db.properties

#databaseの構成情報

#ドライバー

driverClass=com.MySQL.jdbc.Driver
#接続URL
jdbcUrl=jdbc:mysql://localhost:3306/mybatis?character=utf8#ユーザー名
user=root
#パスワード
passWord=root
#接続プール
minPoolSize=10#で予約される接続の最小数。接続プールに予約される接続の最大数。デフォルト: 15 maxPoolSize=20#最大アイドル時間。1800 秒以内に使用されない場合、接続は破棄されます。 0 の場合は破棄されません。デフォルト: 0 maxIdletime=1800#コネクションプール内のコネクションが枯渇したときにc3p0が同時に取得するコネクションの数。デフォルト: 3acquireIncrement=3#接続プール内の初期接続の数は、minPoolSize と maxPoolSize の間にある必要があります。デフォルトは 3
initialPoolSize=15


2.3, configuration

/p> PUBLIC "-//mybatis. org //DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">< ;/settings>環境 id="開発 ">












2.4 POJO クラスとインターフェイス

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

* @ClassName: 著者

* @説明: TODO(著者)

* @著者 Liu Nan

* @date 2015-10-31 12:39:33 pm

**/public class Author { //Authorid
private Integer authorId;    //作者姓名
private String authorUserName;    //作成者パスワード
private String authorPassword;    //作成者邮箱
private String authorEmail;    //作成者介绍
private String authroBio;    //注册時間
private Date registerTime;    


public Integer getAuthorId() { return authorId;
} public void setAuthorId(Integer authorId) { this.authorId = authorId;
} public String 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 authroBio;
} 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 "著者 [authorId=" + authorId + ", authorUserName="
+ authorUserName + ", authorPassword="著者パスワード+ ", authorEmail=" + authorEmail + ", authroBio=" + authroBio + ", registerTime=" + registerTime + "]";
}


}

接続口

package com.pb.ssm.mapper;import com.pb.ssm.po.Author;public インターフェース AuthorMapper { /**
* *

* @Title: findAuthorById

* @Description: TODO(IDで検索)
* @param @param id
* @param @return 設定ファイル

* @return 著者の戻り値

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

* @Title: addAuthor

* @Description: TODO(追加)

* @param @param author
* @param @return 設定ファイル

* @return int 戻り値の型

* @throws
* /
public int addAuthor(著者 著者); /**
* *

* @Title: updateAuthor

* @Description: TODO(更新)

* @param @param author
* @param @return 設定ファイル

* @return int 戻り値の型

* @throws
*/
public int updateAuthor(著者 著者)
/**
* * 削除

* @Title: delteAuthor

* @Description: TODO(IDで削除)

* @param @param id
* @param @return 設定ファイル

* @return int 戻り値の型

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

mapper.xml

/p> PUBLIC "-//mybatis.org//DTD マッパー 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})
author_username=#{authorUserName},author_password=#{authorPassword},author_email=#{authorEmail},author_bio=#{authroBio},register_time=#{registerTime}< ;/ if>where author_id=#{authorId}作成者から削除 where author_id= #{authorId}



3. Mybatis 設定ファイル .xml 統合を使用します

3.1、applicationContext.xml



3.2、测试

パッケージ 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;インポート org.apache.ibatis.session.SqlSessionFactoryBuilder;インポート org.junit.Before;インポート org.junit.Test;インポート org.springframework.context.ApplicationContext;インポート org.springframework.context.support.ClassPathXmlApplicationContext;インポート 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 = authorMapper.findAuthorById(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());
}

@Test public void testUpdateAuthor() { // 获取会话工厂
AuthorMapper authorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");
著者 author = authorMapper.fi ndAuthorById(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

を使用しません

bean>where author_id=#{id}INSERT INTO author(author_username,author_password,author_email,author_bio)
VALUES(#{authorUserName},#{authorPassword},#{authorEmail},#{authroBio})
> ;著者を更新author_username= #{authorUserName},author_password=#{authorPassword},author_email=#{authorEmail},author_bio=#{authroBio},register_time=#{registerTime}where author_id=#{authorId}author_id=#{authorId} の著者から削除

测试类同上

以上即ち MyBatis 入门(六)---mybatis と spring の整合内容、より多くの関連内容请关注 PHP中文网(www.php.cn)!

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。