MyBatis 入門 (5) --- 遅延したロードとキャッシュ

黄舟
リリース: 2016-12-21 14:34:16
オリジナル
1266 人が閲覧しました

1. データベースの作成

1.1. データベースの作成


/*SQLyog EnterPRise v12.09 (64 ビット)MySQL - 5.7.9-log : データベース - mybatis
************************** *********************************************//*!40101 SET名前 utf8 */;/*!40101 SET SQL_MODE=''*/;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;CREATE DATABASE /*!32312 IF NOT EXISTS */`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;USE `mybatis`;/*テーブル `author` のテーブル構造 */DROP TABLE IF EXISTS `author`;CREATE TABLE `author` (
`author_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '作者ID主键',
`author_username` varchar(30) NOT NULL COMMENT '作者用户名',
`author_passWord` varchar(32) NOT NULL COMMENT '作者認証',
` author_email` varchar(50) NOT NULL COMMENT '作者邮箱',
`author_bio` varchar(1000) DEFAULT '这家伙很赖,什么也没留下' COMMENT '作者简介',
`register_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '注册時間间', PRIMARY KEY (`author_id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;/*テーブル `author` のデータ */insert into `author`(`author_id`,`author_username`,` author_password`,`author_email`,`author_bio`,`register_time`) 値 (1,'张三','123456','123@QQ.com','张三是个新手,刚开開始注册','2015- 10-29 10:23:59'),(2,'李四','123asf','lisi@163.com','魂牵梦萦 ','2015-10-29 10:24:29'),( 3,'王五','dfsd342','ww@sina.com','康熙王朝','2015-10-29 10:25:23'),(4,'赵六','123098sdfa', 'zhaoliu@qq.com','花午骨','2015-10-29 10:26:09'),(5,'钱七','zxasqw','qianqi@qq.com','これ家伙很赖,何么也没留下','2015-10-29 10:27:04'),(6,'张三丰','123456','zhangsf@qq.com','这家伙很赖,什么也没留下','2015-10-29 11:48:00'),(7,'张免忌','qwertyuiop','woji@163.com','这家伙很赖,什么也没留下','2015-10-29 11:48:24'),(8,'fdsf','ffff','fdsfds',NULL,NULL),(9,'fdsf','ffff','fdsfds ',NULL,NULL),(10,'aaaa','bbbb','ddd@qq.com','这家伙很赖,什么也没留下','2015-10-29 22:07:45 ');/*テーブル `blog` のテーブル構造 */DROP TABLE IF EXISTS `blog`;CREATE TABLE `blog` (
`blog_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'BlogId主键',
`blog_title` varchar(255) NOT NULL COMMENT 'blog标题',
`author_id` int(11) unsigned NOT NULL COMMENT '作者ID外键', PRIMARY KEY (`blog_id`), KEY `fk_author_id` (`author_id`), CONSTRAINT `fk_author_id` FOREIGN KEY (`author_id`) REFERENCES `author` (`author_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;/*テーブル `blog` のデータ */insert into `blog`(` blog_id`,`blog_title`,`author_id`) 値 (1,'小张的ブログ',1),(2,'小李',2),(3,'王五不是人',3),( 4,'赵地人',4),(5,'钱钱钱',5);/*テーブル `posts` のテーブル構造 */DROP TABLE IF EXISTS `posts`;CREATE TABLE `posts` (
` post_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '文章主键ID',
`post_subject` varchar(255) NOT NULL COMMENT '文章主题,标题',
`post_body` text NOT NULL COMMENT '文章内容最大3000字',
`blog_id` int(11) unsigned NOT NULL COMMENT 'ブログ主键做外键',
`createtime` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '文章创建時間间', PRIMARY KEY (`post_id`), KEY `fk_blog_id` (` blog_id`)、CONSTRAINT `fk_blog_id` FOREIGN KEY (`blog_id`) REFERENCES `blog` (`blog_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;/*テーブル `posts` のデータ */`posts`(`post_id`,`post_subject`,`post_body`,`blog_id`,`createtime`) 値に挿入(1、「MyBatis の概要 1」、「MyBatis とは何ですか? rnMyBatis は、カスタマイズされた SQL、ストアド プロシージャ、および高度なマッピングをサポートする優れた永続層フレームワークです。MyBatis は、ほとんどすべての JDBC コードとパラメータの手動設定と結果セットの取得を回避します。MyBatis単純な XML またはアノテーションを構成に使用し、ネイティブ マップを使用してインターフェイスと Java POJO (Plain Old Java Objects、通常の Java オブジェクト) をデータベース内のレコードにマッピングできます。 ,(2,'Mybatis 2 の紹介','MyBatis を使用するには、mybatis-x.x.x.jar ファイルをクラスパスに配置するだけです。',1,'2015-10-29 10:32:52 '),(3,' Oracle Learning','Oracle Database、Oracle RDBMS または略して Oracle とも呼ばれる、Oracle 社のリレーショナル データベース管理システムです',2,'2015-10-29 10:33:26' ),(4, 'JAVA Learning 1','Java は、1995 年 5 月に Sun Microsystems によって発売された Java オブジェクト指向プログラミング言語および Java プラットフォームの一般名です',3,'2015-10-29 10:34:17'),(5 ,'PL/SQL','PL/SQL は、手続き型言語/SQL と呼ばれるプログラミング言語でもあります。PL/SQL は、Oracle データベースを SQL ステートメントに拡張したものです。',4, '2015-10-29 10:37:52' ),(6,'CSS タグ セレクター','タグ セレクターrnID セレクターrnクラス セレクターrn特別セレクター',5,'2015-10-29 10 :39:44'),(7,'javascript','js: はフロントエンドですスクリプト言語',2,'2015-10-29 10:40:18');/*!40101 SET SQL_MODE=@OLD_SQL_MODE */ ;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;/*!40014 SET UNIQUE_CHECKS =@OLD_UNIQUE_CHECKS */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

データテーブルとデータ

2. プロジェクトを作成します

2.1. プロジェクトを作成します


3. エンティティ クラスとマッパー インターフェイスを確立する

3.1. 3 つのエンティティ クラス

package import java .util.Date;/**

*


* @ClassName: 著者

* @説明: TODO(著者)

* @著者 Liu Nan

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

**/public class Author { / /著者ID
private Integer authorId; //著者名
private String authorPassword; //著者メールアドレス
private String authorEmail //著者紹介
private String authroBio;日付 registerTime


public Integer getAuthorId() { return authorId;
} } public void set AuthorId(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="

}

;
}




package com.pb.mybatis.po;import java.util.List;public class Blog {
//Blog,ID
private Integer blogId;    //ブログ名标题
private String blogTitle;    //外键authorId //author
private Author author;    //文章列表
private Integer authorId;    
//文章列表
非公開リスト投稿リスト;    public integer getblogid(){return blogid;
} public void setblogid(integer blogid){this.blogid = blogid;
} public string getblogtitle(){return blogtitle; blogTitle = blogTitle;
} public 著者 getAuthor() { return author;
} public void setAuthor(Author author) { this.author = author;
} public Integer getAuthorId() { return authorId;
} public void setAuthorId(Integer authorId ) { this.authorId = authorId;
} 公開リスト getPostsList() { return photosList;
} public void setPostsList(ListpostsList) { this.postsList = photosList;
}


@Override public String toString() { return "ブログ [blogId=" + blogId + " , blogTitle=" + blogTitle + ", authorId=" + authorId + "]";
}







}



パッケージ com.pb.mybatis.po;import java.util.Date ;public class Posts { //文章ID

private Integer postId;    //文章标题

private String postTitle;    //文章本文内容

private String postBody;    
//外键
private Integer blogId;    //建立時間
private Date createTime;    

public Integer getPostId() { return postId;
} public void setPostId(Integer postId) { this.postId = postId;
} public String getPostTitle() { return postTitle;
} public void setPostTitle(String postTitle) { これ。 postTitle = postTitle;
} public String getPostBody() { return postBody;
} public void setPostBody(String postBody) { this.postBody = postBody;
} public Date getCreateTime() { return createTime;
} public void setCreateTime(Date createTime ) { this.createTime = createTime;
}

public Integer getBlogId() { return blogId;
} public void setBlogId(Integer blogId) { this.blogId = blogId;
}
@Override public String toString() { return "投稿 [postId=" + postId + ", postTitle=" + postTitle + ", postBody=" + postBody + ", createTime=" + createTime + ", blogId=" + blogId + "]";
}






}



3.2、3个接口

package com.pb.mybatis.mapper;import com.pb.mybatis.po.Author;public Interface AuthorMapper { /**
* *

* @Title: findAuthorById

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

* @return 著者の戻り値

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

package com.pb.mybatis.mapper;import com.pb.mybatis.po.Blog;public Interface BlogMapper {

/**
*

* @Title: findBlogById

* @Description: TODO (BLOGIDからBLOGを検索、著者を関連付け、投稿の遅延読み込みを実装)

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

* @return ブログの戻り値の種類

* @throws
*/
public Blog findBlogById(int blogId);

}

package com.pb.mybatis.mapper;import java.util.List;import com.pb.mybatis.po.Posts;public Interface PostsMapper { /**

* *

* @Title: findPostsByBlogId

* @Description: TODO (BLOGID に基づいて投稿リストを検索)

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

* @return List戻り値の型

* @throws
*/
public List findPostsByBlogId(int blogId);
}

四、建立mapper.xmlとconfiguration.xml

4.1、相对应的mapper.xml

AuthorMapp er.xml

p> PUBLIC "-//mybatis.org//DTD マッパー 3.0//EN"
"http://mybatis.org/ dtd/mybatis-3-mapper.dtd">>

BlogMapper.xml

br/> PUBLIC "-//mybatis.org//DTD マッパー 3.0//EN"
"http://mybatis .org/dtd/mybatis-3-mapper.dtd">

PostsMapper.xml

br/> PUBLIC "-// mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

4.2、configuration.xml

db.properties

ドライバー=com.mysql。 jdbc .Driver

url=jdbc:mysql://localhost:3306/mybatis?character=utf8

username=root

password=root



遅延ロードを有効にし、即時ロードをオフにします







br/> PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis .org/dtd/mybatis-3-config.dtd">










5.ジュニト

パッケージ com.pb.mybatis.mapper;import java.io.InputStream;import java.util.List;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 com.pb.mybatis.po.Blog;import com.pb.mybatis. po.Posts;public class BlogMapperTest { private SqlSessionFactory sqlSessionFactory;

@Before public void setUp() throws Exception {
String config="configuration.xml";
InputStream resource=Resources.getResourceAsStream(config) ;
sqlSessionFactory=new SqlSessionFactoryBuilder ().build(resource);
} /**
* *

* @Title: testFindBlogs

* @Description: TODO(遅延読み込み)

* @param 設定ファイル

* @return void 戻り値の型

* @throws
*/
@Test public void testFindBlogById() { //建立会话
SqlSession sqlSession=sqlSessionFactory.openSession();        //获取Mapperインターフェイス对象
BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class);        //调用Mapperメソッド
ブログ blog=blogMapper.findBlogById(1);

System.out.println(blog.getBlogId()+"..."+blog.getBlogTitle());
System.out.println( "=========== .println("===========開始延長加筆文章=============);
リスト list=blog.getPostsList();        /*int count=0;
for(Posts p:list){
System.out.println("count+"+count++);
System.out.println(p.getPostId());
System.out.println (p.getPostTitle());
System.out.println(p.getPostBody());
System.out.println(p.getCreateTime());
System.out.println(p.getBlogId());
}*/

}

}


结果:

DEBUG [main] - ==>  準備中: SELECT * FROM blog WHERE blog_id=?デバッグ [メイン] - ==>パラメータ: 1(整数)

DEBUG [main] - 1...小さな张的ブログ===========开始延時間加ダウンロード作者===== ======デバッグ [メイン] - ==>  準備: select * from author where author_id=?デバッグ [メイン] - ==>パラメータ: 1(整数)
DEBUG [main] - パラメータ: 1(整数)
DEBUG [main] -

六、一级缓存

6.1、mybatis一级缓存

mybatis、默认开启了一级存続中、sqlSession中默认有一hashMap

当查询時,先去hashMap中查找,

如果有れば直接,取り出し,不再操作データ库

如果無ければデータ库查找を去ってhaspMap内に放散追加、删除、修正、後有コミット時間会清空一级存存

sqlSession 相互不影响的、一级缓存

MyBatis 入門 (5) --- 遅延したロードとキャッシュ

6.2、上層の例

测试: 查询用户

パッケージ com.pb.mybatis.mapper;import static org.junit.Assert.*;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 com.pb.mybatis.po.Author;public class AuthorMapperTest { private SqlSessionFactory sqlSessionFactory;

@Before public void setUp() throws Exception {
String config="configuration.xml";
InputStream resource=Resources.getResourceAsStream(config);
sqlSes sionFactory=new SqlSessionFactoryBuilder().build(リソース);
}

@Test public void testFindAuthorById() { //获取会话工厂
SqlSession sqlSession=sqlSessionFactory.openSession();
AuthorMapper Mapper=sqlSession.getMapper(AuthorMapper.class);        //第一次查询
著者 author1=authorMapper.findAuthorById(2);
System.out.println(author1);        // 再次询同样的
著者 author2=authorMapper.findAuthorById(2);
System.out.println(author2);        
//再次查询不的同样的
作成者 author3=authorMapper.findAuthorById(4);
System.out.println(author3);
}

}

结果:

デバッグ [メイン] - ==>  準備: select * from author where author_id=?デバッグ [メイン] - ==>パラメータ: 2(整数)
DEBUG [main] - 著者 [authorId=2, authorUserName=李四, authorPassword=123asf, authorEmail=lisi@163.com, authroBio=魂牵梦萦 , registerTime=Thu Oct 29 10:24:29 CST 2015]
デバッグ [メイン] - ==>  準備: select * from author where author_id=?デバッグ [メイン] - ==>パラメータ: 4(整数)
DEBUG [main] -

6.3、当查询後,做事务,再查询第一次的

接口中做追加方法

/**
* *

* @Title: addAuthor

* @Description: TODO(追加)

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

* @return int 戻り値の型

* @throws
*/
public int addAuthor(作成者 author);

mapper.xml中のselect

INSERT INTO author(author_username,author_password,author_email,author_bio)
VALUES(#{authorUserName},#{authorPassword},#{authorEmail},#{ authroBio})

测试

パッケージ com.pb.mybatis.mapper;import static org.junit.Assert.*;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 com.pb.mybatis.po.Author;public class AuthorMapperTest { private SqlSessionFactory sqlSessionFactory;

@Before public void setUp() throws Exception {
String config="configuration.xml";
InputStream resource=Resources.getResourceAsStream(config);
sqlSes sionFactory=new SqlSessionFactoryBuilder().build(リソース);
}

@Test public void testFindAuthorById() { //获取会话工厂
SqlSession sqlSession=sqlSessionFactory.openSession();
AuthorMapper Mapper=sqlSession.getMapper(AuthorMapper.class);        //第一次查询
作成者 author1=authorMapper.findAuthorById(2);
System.out.println(author1);
System.out.println("=======下面有事务处理==== ====");
著者 newAuthor=new Author();
newAuthor.setAuthorEmail("qq.com@qq.com");
newAuthor.setAuthorPassword("fdsfds");
newAuthor.setAuthorUserName("郭靖" );
newAuthor.setAuthroBio("射雕英雄传");        int num=authorMapper.addAuthor(newAuthor);
sqlSession.commit();

System.out.println(newAuthor.getAuthorId());
System.out.println("num="+num);
システム。 out.println("再查询");        //再次查询同样的
Author author2=authorMapper.findAuthorById(2);
System.out.println(author2);

}



}

結果:

デバッグ [メイン] - ==>  準備: select * from author where author_id=?デバッグ [メイン] - ==>パラメータ: 2(整数)
DEBUG [main] -  準備: INSERT INTO author(author_username,author_password,author_email,author_bio) VALUES(?,?,?,?)
DEBUG [main] - ==>パラメータ: 郭靖(String)、fdsfds(String)、qq.com@qq.com(String)、射雕英雄传(String)
DEBUG [main] - DEBUG [main] - ==>  準備: select * from author where author_id=?デバッグ [メイン] - ==>パラメータ: 2(整数)
DEBUG [main] -

七、二级缓存

7.1、mybatis二级缓存一级缓存

MyBatis 入門 (5) --- 遅延したロードとキャッシュ

sqlSession は相互影響を受けません。一階層存続

Mapper(名前空間)は二階層存続

複数のsqlSessionは1つのMapperの二階層存続領域

二階層存続名前空間を共有でき、他のマッパーも独自の二階層存続領域名前空間

第1 つの名前空間のマッパーには両方に 1 つの二層保存領域があり、2 つのマッパーの名前空間が同じであれば、この 2 つのマッパーが実行する SQL 評価データは同じ二層保存領域に存在します。

1.二階層は、mapper のクラスに存在し、configuration.xml に設定されている二階層を除いて、個別のmapper.xml にも二階層が存在します


2. マッパーによって照合された pojo クラスはシリアル化を実装します

MyBatis 入門 (5) --- 遅延したロードとキャッシュ

3. authorMapper で 2 番目のレベルのキャッシュを有効にします

ssionFactory.openSession();
AuthorMapper authorMapper1 = sqlSession1.getMapper (Authormapper.class) // 最初のクエリ

Author1 = Authormapper1.FindAutHorByid (2);データなしでオフ。 ION SQLSESSION2 = sqlSessionFactory.openSession();

AuthorMapper2 = sqlSession2.getMapper(AuthorMapper.class);

sqlSession2.close();*/

// セッション ファクトリを取得します

SqlSession sqlSession3 = sqlSessionFactory .openSession( );

AuthorMapper authorMapper3 = sqlSession3 .getMapper(AuthorMapper.class); // 最初のクエリ 著者 author3 = authorMapper3.findAuthorById(2)

}

;



デバッグ [メイン] - キャッシュヒット率 [com. pb.mybatis.mapper.AuthorMapper]: 0.0DEBUG [main] - JDBC 接続を開く
DEBUG [main] - 接続を作成しました 873769339.
DEBUG [main] - JDBC 接続で autocommit を false に設定します [com.mysql] .jdbc.JDBC4Connection@ ​​3414a97b]
DEBUG [main] - ==> 準備: select * from author where author_id=? DEBUG [main] - ==> パラメータ: 2(整数)
DEBUG [main] - DEBUG [main] - JDBC 接続を閉じる [com.mysql.jdbc.JDBC4Connection@3414a97b]
DEBUG [ main] - 接続 873769339 を pool.
DEBUG [main] - キャッシュ ヒット率 [com.pb.mybatis.mapper.AuthorMapper]: 0.5


途中にコミット操作を追加

@Test public void testCache() { // セッションファクトリーを取得します

SqlSession1 = sqlSessionFactory.openSession();

AuthorMapper authorMapper1 = sqlSession1.getMapper(AuthorMapper.class) // 最初のクエリ

AuthorMapper1.findAuthorById(2); // データがないため閉じる必要があります。キャッシュ領域に書き込むことができます sqlSession1 .close(); // セッションファクトリーを取得します SqlSession sqlSession2 = sqlSessionFactory.openSession(); AuthorMapper authorMapper2 = sqlSession2.getMapper(AuthorMapper.class);
Auth または author2 = authorMapper2.findAuthorById( 2); //更新
author2.setAuthorUserName("Chairman");
author2.setAuthroBio("Company");
authorMapper2.updateAuthor(author2) // コミットによりキャッシュ領域がクリアされます sqlSession2.commit();
sqlSession2 .close(); // セッションファクトリーを取得します sqlSession3 = sqlSessionFactory.openSession();
AuthorMapper authorMapper3 = sqlSession3.getMapper(AuthorMapper.class); sqlSession3 = use using using using sqlSession ' sqlSession ' ‐ Mapper3.findAuthorById(2);
sqlSession3.close();
}

結果:

DEBUG [main] - キャッシュ ヒット率 [com.pb.mybatis.mapper.AuthorMapper]: 0.0DEBUG [main] - JDBC 接続を開きます
DEBUG [main] - 作成された接続 873769339.
DEBUG [main] - 自動コミットを false に設定しますJDBC 接続上 [com.mysql.jdbc.JDBC4Connection@3414a97b]
DEBUG [main] - ==>  準備: select * from author where author_id=?デバッグ [メイン] - ==>パラメータ: 2(整数)
DEBUG [main] - DEBUG [main] - JDBC 接続を閉じる[com.mysql.jdbc.JDBC4Connection@3414a97b]
DEBUG [main] - 接続 873769339 を pool.
DEBUG [main] - キャッシュ ヒット率 [com.pb.mybatis.mapper.AuthorMapper]: 0.5DEBUG [main] - JDBC 接続を開く
DEBUG [main] - プールから接続 873769339 をチェックアウトしました。
DEBUG [main] - JDBC 接続で autocommit を false に設定します [com.mysql.jdbc.JDBC4Connection@3414a97b]
DEBUG [main] - ==>  準備: 著者の更新 SET author_username=?、author_password=?、author_email=?、author_bio=?、register_time=?ここで author_id=?デバッグ [メイン] - ==>パラメータ: 董事长(String), 123asf(String), lisi@163.com(String), 公司(String), 2015-10-29 10:24:29.0(Timestamp), 2(Integer)
DEBUG [main] - DEBUG [main] - JDBC 接続の自動コミットを true にリセット [com.mysql.jdbc.JDBC4Connection@3414a97b]
DEBUG [main] - JDBC 接続を閉じています [com.mysql.jdbc.JDBC4Connection@3414a97b]
DEBUG [main] - 接続 873769339 をプールに返しました。
DEBUG [main] - キャッシュ ヒット率 [com.pb.mybatis.mapper. AuthorMapper]: 0.3333333333333333DEBUG [main] - JDBC 接続を開きます
DEBUG [main] - プールから接続 873769339 をチェックアウトしました。
DEBUG [main] - JDBC 接続で autocommit を false に設定します [com.mysql.jdbc.JDBC4Connection@3414a97b]
デバッグ [メイン] - ==>  準備: select * from author where author_id=?デバッグ [メイン] - ==>パラメータ: 2(整数)
DEBUG [main] - DEBUG [main] - JDBC 接続を閉じる[com.mysql.jdbc.JDBC4Connection@3414a97b]
DEBUG [main] - 接続 873769339 をプールに返しました。

8.2、 ehcache.xml を構築します

ehcache.xml

xsi:noNamespaceSchemaLocation="../config/ehcache. xsd">MyBatis 入門 (5) --- 遅延したロードとキャッシュ maxElementsInMemory="1000" maxElementsOnDisk="10000000"

="false"

overflowToDisk="false"

timeToIdleSeconds=" 120"

timeToLiveSeconds="120"

diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">










8.3、マッパーのキャッシュ内に指定type


8.4、测试

@Test public void testCache() { // セッション ファクトリを取得します
SqlSession sqlSession1 = sqlSessionFactory.openSession();
AuthorMapper1 = sqlSession1.getMapper(AuthorMapper.class); // 最初のクエリ
著者 author1 = authorMapper1.findAuthorById (2 ); //キャッシュ領域にデータを書き込むことができないため、閉じる必要があります。 sqlSession2.getMapper(AuthorMapper.class);
author2 = authorMapper2.findAuthorById(2); setAuthorUserName("Chairman");
author2.setAuthroBio("Company");
authorMapper2.updateAuthor(author2); // コミットするとキャッシュ領域がクリアされます sqlSession2 .commit();
sqlSession2.close();使用する 使用する 使用する' sqlSession ' sqlSession ' sqlSession 3 = sqlSession 3. pper(AuthorMapper.class); // 最初のクエリ
著者 author3 = authorMapper3.findAuthorById(2);
sqlSession3.close();
}





結果:

DEBUG [main] - クラスパスにある ehcache.xml から ehcache を構成します: file:/E:/mywork/MybatisDemo2/bin/ehcache.xml
DEBUG [main] - URL から ehcache を構成します: file:/E:/mywork /MybatisDemo2/bin/ehcache.xml
DEBUG [main] - InputStream からの ehcache の構成
DEBUG [main] - ehcache 属性の無視 xmlns:xsi
DEBUG [main] - ehcache 属性の無視 xsi:noNamespaceSchemaLocation
DEBUG [main] - ディスク ストアパス: F:developehcache
DEBUG [main] - デフォルトの構成で新しい CacheManager を作成します
DEBUG [main] - propertyString が null です。
DEBUG [main] - CacheManagerEventListenerFactory クラスが指定されていません。スキップ中...
DEBUG [main] - BootstrapCacheLoaderFactory クラスが指定されていません。スキップ中...
DEBUG [main] - CacheWriter ファクトリが設定されていません。スキップ中...
DEBUG [main] - CacheExceptionHandlerFactory クラスが指定されていません。スキップ中...
DEBUG [main] - com.pb.mybatis.mapper.AuthorMapper の net.sf.ehcache.store.NotifyingMemoryStore を初期化しました
DEBUG [main] - 初期化されたキャッシュ: com.pb.mybatis.mapper.AuthorMapper
DEBUG [main] - CacheDecoratorFactory がdefaultCache 用に構成されていません。 'com.pb.mybatis.mapper.AuthorMapper' をスキップします。
DEBUG [main] - キャッシュ ヒット率 [com.pb.mybatis.mapper.AuthorMapper]: 0.0DEBUG [main] - JDBC 接続を開く
DEBUG [main] -接続 1286943672 を作成しました。
DEBUG [main] - JDBC 接続で自動コミットを false に設定 [com.mysql.jdbc.JDBC4Connection@4cb533b8]
DEBUG [main] - ==>  準備: select * from author where author_id=?デバッグ [メイン] - ==>パラメータ: 2(整数)
DEBUG [main] - DEBUG [main] - JDBC 接続を閉じる[com.mysql.jdbc.JDBC4Connection@4cb533b8]
DEBUG [main] - 接続 1286943672 をプールに返しました。
DEBUG [main] - キャッシュ ヒット率 [com.pb.mybatis.mapper.AuthorMapper]: 0.5DEBUG [main] - JDBC 接続を開く
DEBUG [main] - プールから接続 1286943672 をチェックアウトしました。
DEBUG [main] - JDBC 接続で autocommit を false に設定します [com.mysql.jdbc.JDBC4Connection@4cb533b8]
DEBUG [main] - ==>  準備: 著者の更新 SET author_username=?、author_password=?、author_email=?、author_bio=?、register_time=?ここで author_id=?デバッグ [メイン] - ==>パラメータ: 董事长(String), 123asf(String), lisi@163.com(String), 公司(String), 2015-10-29 10:24:29.0(Timestamp), 2(Integer)
DEBUG [main] - DEBUG [main] - JDBC 接続の自動コミットを true にリセット [com.mysql.jdbc.JDBC4Connection@4cb533b8]
DEBUG [main] - JDBC 接続を閉じています [com.mysql.jdbc.JDBC4Connection@4cb533b8]
DEBUG [main] - 接続 1286943672 をプールに返しました。
DEBUG [main] - キャッシュ ヒット率 [com.pb.mybatis.mapper. AuthorMapper]: 0.3333333333333333DEBUG [main] - JDBC 接続を開きます
DEBUG [main] - プールから接続 1286943672 をチェックアウトしました。
DEBUG [main] - JDBC 接続で autocommit を false に設定します [com.mysql.jdbc.JDBC4Connection@4cb533b8]
デバッグ [メイン] - ==>  準備: select * from author where author_id=?デバッグ [メイン] - ==>パラメータ: 2(整数)
DEBUG [main] - DEBUG [main] - JDBC 接続を閉じる[com.mysql.jdbc.JDBC4Connection@4cb533b8]
DEBUG [main] - 接続 1286943672 をプールに返しました。

以上は MyBatis 入门(五)---延時間追加ダウンロード、保存コンテンツ、さらなる相关コンテンツ请关注PHP中文网(www.php.cn)!


関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!