A Simple Caching Example on MyBatis using EhCache
BY EDWIN,
ON JULY 25, 2013
Today i will show a simple example on how to
combine ehcache caching framework with MyBatis ORM. I use Maven as my
build tool, and Netbeans as my IDE.
Okay, here is my pom.xml,
01
<
project
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
02
xsi:schemaLocation
=
"http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
03
<
modelVersion
>4.0.0</
modelVersion
>
04
<
groupId
>com.edw.ehcache</
groupId
>
05
<
artifactId
>MyBatisEhCache</
artifactId
>
06
<
version
>1.0-SNAPSHOT</
version
>
07
<
packaging
>jar</
packaging
>
08
<
name
>MyBatisEhCache</
name
>
09
<
url
>http://maven.apache.org</
url
>
10
<
properties
>
11
<
project.build.sourceEncoding
>UTF-8</
project.build.sourceEncoding
>
12
</
properties
>
13
<
dependencies
>
14
<
dependency
>
15
<
groupId
>junit</
groupId
>
16
<
artifactId
>junit</
artifactId
>
17
<
version
>3.8.1</
version
>
18
<
scope
>test</
scope
>
19
</
dependency
>
20
<
dependency
>
21
<
groupId
>org.mybatis</
groupId
>
22
<
artifactId
>mybatis</
artifactId
>
23
<
version
>3.2.2</
version
>
24
</
dependency
>
25
<
dependency
>
26
<
groupId
>org.mybatis.caches</
groupId
>
27
<
artifactId
>mybatis-ehcache</
artifactId
>
28
<
version
>1.0.2</
version
>
29
</
dependency
>
30
<
dependency
>
31
<
groupId
>log4j</
groupId
>
32
<
artifactId
>log4j</
artifactId
>
33
<
version
>1.2.17</
version
>
34
</
dependency
>
35
<
dependency
>
36
<
groupId
>mysql</
groupId
>
37
<
artifactId
>mysql-connector-java</
artifactId
>
38
<
version
>5.1.6</
version
>
39
</
dependency
>
40
<
dependency
>
41
<
groupId
>net.sf.ehcache</
groupId
>
42
<
artifactId
>ehcache</
artifactId
>
43
<
version
>2.7.0</
version
>
44
</
dependency
>
45
<
dependency
>
46
<
groupId
>org.slf4j</
groupId
>
47
<
artifactId
>slf4j-log4j12</
artifactId
>
48
<
version
>1.7.5</
version
>
49
</
dependency
>
50
</
dependencies
>
51
</
project
>
And i create a simple mysql table,
1
CREATE
TABLE
`testing`
(
2
`Id`
int
(11)
NOT
NULL
AUTO_INCREMENT,
3
`
name
`
varchar
(30)
NOT
NULL
DEFAULT
''
,
4
`address`
varchar
(255)
NOT
NULL
DEFAULT
''
,
5
PRIMARY
KEY
(`Id`),
6
UNIQUE
KEY
`ix`
(`
name
`)
7
)
ENGINE=InnoDB AUTO_INCREMENT=11
DEFAULT
CHARSET=latin1;
and a simple bean and xml representation from
my table,
01
package
com.edw.mybatisehcache.bean;
02
03
import
java.io.Serializable;
04
05
public
class
Testing
implements
Serializable
{
06
07
private
Integer
id;
08
private
String
name;
09
private
String
address;
10
11
//
setter and getter
12
13
@Override
14
public
String
toString() {
15
return
"testing{"
+
"id="
+
id +
",
name="
+
name +
",
address="
+
address +
'}'
;
16
}
17
}
01
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
03
<
mapper
namespace
=
"com.edw.mybatisehcache.mapper.TestingMapper"
>
04
05
<
cache
type
=
"org.mybatis.caches.ehcache.EhcacheCache"
/>
06
07
<
resultMap
id
=
"Testings"
type
=
"com.edw.mybatisehcache.bean.Testing"
>
08
<
id
column
=
"id"
property
=
"id"
jdbcType
=
"BIGINT"
/>
09
<
result
column
=
"name"
property
=
"name"
jdbcType
=
"VARCHAR"
/>
10
<
result
column
=
"address"
property
=
"address"
jdbcType
=
"VARCHAR"
/>
11
</
resultMap
>
12
13
<
select
id
=
"select"
resultMap
=
"Testings"
>
14
select
15
*
16
from
testing
17
</
select
>
18
</
mapper
>
An xml configuration to load my database
connection,
01
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
03
PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
04
"http://mybatis.org/dtd/mybatis-3-config.dtd">
05
<
configuration
>
06
<
environments
default
=
"development"
>
07
<
environment
id
=
"development"
>
08
<
transactionManager
type
=
"JDBC"
/>
09
<
dataSource
type
=
"UNPOOLED"
>
10
<
property
name
=
"driver"
value
=
"com.mysql.jdbc.Driver"
/>
11
<
property
name
=
"url"
value
=
"jdbc:mysql://localhost/test"
/>
12
<
property
name
=
"username"
value
=
"root"
/>
13
<
property
name
=
"password"
value
=
""
/>
14
</
dataSource
>
15
</
environment
>
16
</
environments
>
17
<
mappers
>
18
<
mapper
resource
=
"com/edw/mybatisehcache/xml/TestingMapper.xml"
/>
19
</
mappers
>
20
</
configuration
>
A java code to load my xml configuration,
01
package
com.edw.mybatisehcache.config;
02
03
import
java.io.Reader;
04
import
org.apache.ibatis.io.Resources;
05
import
org.apache.ibatis.session.SqlSessionFactory;
06
import
org.apache.ibatis.session.SqlSessionFactoryBuilder;
07
08
public
class
MyBatisSqlSessionFactory
{
09
10
private
static
final
SqlSessionFactory
FACTORY;
11
12
static
{
13
try
{
14
Reader
reader =
Resources.getResourceAsReader(
"com/edw/mybatisehcache/xml/Configuration.xml"
);
15
FACTORY
=
new
SqlSessionFactoryBuilder().build(reader);
16
}
catch
(Exception
e){
17
throw
new
RuntimeException(
"Fatal
Error. Cause: "
+
e, e);
18
}
19
}
20
21
public
static
SqlSessionFactory
getSqlSessionFactory() {
22
return
FACTORY;
23
}
24
}
A java interface to do handle queries,
1
package
com.edw.mybatisehcache.mapper;
2
3
import
com.edw.mybatisehcache.bean.Testing;
4
import
java.util.List;
5
6
public
interface
TestingMapper
{
7
public
List
select();
8
}
And this is my ehcache.xml configuration,
01
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
05
<
ehcache
>
06
07
<
diskStore
path
=
"F:\\cache"
/>
08
09
<
defaultCache
eternal
=
"true"
maxElementsInMemory
=
"1000"
10
overflowToDisk
=
"true"
diskPersistent
=
"true"
timeToIdleSeconds
=
"0"
11
timeToLiveSeconds
=
"0"
memoryStoreEvictionPolicy
=
"LRU"
statistics
=
"true"
/>
12
</
ehcache
>
This is my main java class, as you can see i
try to do a repeated simple select queries,
01
package
com.edw.mybatisehcache.main;
02
03
import
com.edw.mybatisehcache.bean.Testing;
04
import
com.edw.mybatisehcache.config.MyBatisSqlSessionFactory;
05
import
com.edw.mybatisehcache.mapper.TestingMapper;
06
import
java.util.List;
07
import
org.apache.ibatis.session.SqlSession;
08
import
org.apache.ibatis.session.SqlSessionFactory;
09
import
org.apache.log4j.Logger;
10
11
public
class
Main
{
12
13
private
static
Logger
logger = Logger.getLogger(Main.
class
);
14
15
public
static
void
main(String[]
args) {
16
17
for
(
int
i
=
0
;
i <
3
;
i++) {
18
SqlSessionFactory
sqlSessionFactory =
MyBatisSqlSessionFactory.getSqlSessionFactory();
19
SqlSession
sqlSession = sqlSessionFactory.openSession();
20
TestingMapper
testingMapper = sqlSession.getMapper(TestingMapper.
class
);
21
22
List
testings = testingMapper.select();
23
for
(Testing
testing : testings) {
24
logger.debug(testing);
25
}
26
sqlSession.close();
27
28
try
{
29
Thread.sleep(
3000
);
30
}
catch
(Exception
e) {
31
logger.error(e,
e);
32
}
33
}
34
}
35
}
This is what is written on my netbeans’
console,
1
2013-07-25
15:30:10,648 [Segment] DEBUG
net.sf.ehcache.store.disk.Segment:779 - fault removed 0 from heap
2
2013-07-25
15:30:10,648 [Segment] DEBUG
net.sf.ehcache.store.disk.Segment:796 - fault added 0 on disk
3
2013-07-25
15:30:13,722 [Cache] DEBUG net.sf.ehcache.Cache:1970 - Cache:
com.edw.mybatisehcache.mapper.TestingMapper store hit for
2026218237:1652924294:com.edw.mybatisehcache.mapper.TestingMapper.select:0:2147483647:select
4
*
5
from
testing
6
2013-07-25
15:30:13,722 [Main] DEBUG com.edw.mybatisehcache.main.Main:24 -
testing{id=1, name=edw, address=Ciledug}
7
2013-07-25
15:30:13,722 [Main] DEBUG com.edw.mybatisehcache.main.Main:24 -
testing{id=2, name=kamplenk, address=Cikokol}
8
2013-07-25
15:30:13,722 [Main] DEBUG com.edw.mybatisehcache.main.Main:24 -
testing{id=3, name=nugie, address=Pamulang}
9
2013-07-25
15:30:13,722 [Main] DEBUG com.edw.mybatisehcache.main.Main:24 -
testing{id=4, name=tebek, address=Karawaci}
Have fun
01 |
< project
xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
02 |
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd" > |
03 |
< modelVersion >4.0.0</ modelVersion > |
04 |
< groupId >com.edw.ehcache</ groupId > |
05 |
< artifactId >MyBatisEhCache</ artifactId > |
06 |
< version >1.0-SNAPSHOT</ version > |
07 |
< packaging >jar</ packaging > |
08 |
< name >MyBatisEhCache</ name > |
09 |
< url >http://maven.apache.org</ url > |
10 |
< properties > |
11 |
< project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > |
12 |
</ properties > |
13 |
< dependencies > |
14 |
< dependency > |
15 |
< groupId >junit</ groupId > |
16 |
< artifactId >junit</ artifactId > |
17 |
< version >3.8.1</ version > |
18 |
< scope >test</ scope > |
19 |
</ dependency > |
20 |
< dependency > |
21 |
< groupId >org.mybatis</ groupId > |
22 |
< artifactId >mybatis</ artifactId > |
23 |
< version >3.2.2</ version > |
24 |
</ dependency > |
25 |
< dependency > |
26 |
< groupId >org.mybatis.caches</ groupId > |
27 |
< artifactId >mybatis-ehcache</ artifactId > |
28 |
< version >1.0.2</ version > |
29 |
</ dependency > |
30 |
< dependency > |
31 |
< groupId >log4j</ groupId > |
32 |
< artifactId >log4j</ artifactId > |
33 |
< version >1.2.17</ version > |
34 |
</ dependency >
|
35 |
< dependency > |
36 |
< groupId >mysql</ groupId > |
37 |
< artifactId >mysql-connector-java</ artifactId > |
38 |
< version >5.1.6</ version > |
39 |
</ dependency > |
40 |
< dependency > |
41 |
< groupId >net.sf.ehcache</ groupId > |
42 |
< artifactId >ehcache</ artifactId > |
43 |
< version >2.7.0</ version > |
44 |
</ dependency > |
45 |
< dependency > |
46 |
< groupId >org.slf4j</ groupId > |
47 |
< artifactId >slf4j-log4j12</ artifactId > |
48 |
< version >1.7.5</ version > |
49 |
</ dependency >
|
50 |
</ dependencies > |
51 |
</ project > |
1 |
CREATE
TABLE
`testing`
( |
2 |
`Id`
int (11)
NOT
NULL
AUTO_INCREMENT, |
3 |
` name `
varchar (30)
NOT
NULL
DEFAULT
'' , |
4 |
`address`
varchar (255)
NOT
NULL
DEFAULT
'' , |
5 |
PRIMARY
KEY
(`Id`), |
6 |
UNIQUE
KEY
`ix`
(` name `) |
7 |
)
ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT
CHARSET=latin1; |
01 |
package
com.edw.mybatisehcache.bean; |
02 |
03 |
import
java.io.Serializable; |
04 |
05 |
public
class
Testing
implements
Serializable
{ |
06 |
07 |
private
Integer
id; |
08 |
private
String
name; |
09 |
private
String
address; |
10 |
11 |
//
setter and getter |
12 |
13 |
@Override |
14 |
public
String
toString() { |
15 |
return
"testing{"
+
"id="
+
id + ",
name=" +
name + ",
address=" +
address + '}' ; |
16 |
}
|
17 |
} |
01 |
xml
version = "1.0"
encoding = "UTF-8"
?> |
02 |
|
03 |
< mapper
namespace = "com.edw.mybatisehcache.mapper.TestingMapper"
> |
04 |
|
05 |
< cache
type = "org.mybatis.caches.ehcache.EhcacheCache" /> |
06 |
|
07 |
< resultMap
id = "Testings"
type = "com.edw.mybatisehcache.bean.Testing"
> |
08 |
< id
column = "id"
property = "id"
jdbcType = "BIGINT"
/> |
09 |
< result
column = "name"
property = "name"
jdbcType = "VARCHAR"
/> |
10 |
< result
column = "address"
property = "address"
jdbcType = "VARCHAR"
/> |
11 |
</ resultMap >
|
12 |
13 |
< select
id = "select"
resultMap = "Testings" > |
14 |
select
|
15 |
* |
16 |
from
testing
|
17 |
</ select >
|
18 |
</ mapper > |
01 |
xml
version = "1.0"
encoding = "UTF-8"
?> |
02 |
|
03 |
PUBLIC
"-//mybatis.org//DTD Config 3.0//EN" |
04 |
"http://mybatis.org/dtd/mybatis-3-config.dtd"> |
05 |
< configuration > |
06 |
< environments
default = "development" > |
07 |
< environment
id = "development" > |
08 |
< transactionManager
type = "JDBC" /> |
09 |
< dataSource
type = "UNPOOLED" > |
10 |
< property
name = "driver"
value = "com.mysql.jdbc.Driver" /> |
11 |
< property
name = "url"
value = "jdbc:mysql://localhost/test" /> |
12 |
< property
name = "username"
value = "root" /> |
13 |
< property
name = "password"
value = "" /> |
14 |
</ dataSource > |
15 |
</ environment >
|
16 |
</ environments > |
17 |
< mappers >
|
18 |
< mapper
resource = "com/edw/mybatisehcache/xml/TestingMapper.xml"
/>
|
19 |
</ mappers >
|
20 |
</ configuration > |
01 |
package
com.edw.mybatisehcache.config; |
02 |
03 |
import
java.io.Reader; |
04 |
import
org.apache.ibatis.io.Resources; |
05 |
import
org.apache.ibatis.session.SqlSessionFactory; |
06 |
import
org.apache.ibatis.session.SqlSessionFactoryBuilder; |
07 |
08 |
public
class
MyBatisSqlSessionFactory
{ |
09 |
10 |
private
static
final
SqlSessionFactory
FACTORY; |
11 |
12 |
static
{ |
13 |
try
{ |
14 |
Reader
reader =
Resources.getResourceAsReader( "com/edw/mybatisehcache/xml/Configuration.xml" ); |
15 |
FACTORY
= new
SqlSessionFactoryBuilder().build(reader); |
16 |
}
catch
(Exception
e){ |
17 |
throw
new
RuntimeException( "Fatal
Error. Cause: " +
e, e); |
18 |
} |
19 |
} |
20 |
21 |
public
static
SqlSessionFactory
getSqlSessionFactory() { |
22 |
return
FACTORY; |
23 |
} |
24 |
} |
1 |
package
com.edw.mybatisehcache.mapper; |
2 |
3 |
import
com.edw.mybatisehcache.bean.Testing; |
4 |
import
java.util.List; |
5 |
6 |
public
interface
TestingMapper
{ |
7 |
public
List
|
8 |
} |
01 |
xml
version = "1.0"
encoding = "UTF-8" ?> |
02 |
|
05 |
< ehcache > |
06 |
|
07 |
< diskStore
path = "F:\\cache"
/> |
08 |
|
09 |
< defaultCache
eternal = "true"
maxElementsInMemory = "1000" |
10 |
overflowToDisk = "true"
diskPersistent = "true"
timeToIdleSeconds = "0" |
11 |
timeToLiveSeconds = "0"
memoryStoreEvictionPolicy = "LRU"
statistics = "true"
/> |
12 |
</ ehcache > |
01 |
package
com.edw.mybatisehcache.main; |
02 |
03 |
import
com.edw.mybatisehcache.bean.Testing; |
04 |
import
com.edw.mybatisehcache.config.MyBatisSqlSessionFactory; |
05 |
import
com.edw.mybatisehcache.mapper.TestingMapper; |
06 |
import
java.util.List; |
07 |
import
org.apache.ibatis.session.SqlSession; |
08 |
import
org.apache.ibatis.session.SqlSessionFactory; |
09 |
import
org.apache.log4j.Logger; |
10 |
11 |
public
class
Main
{ |
12 |
13 |
private
static
Logger
logger = Logger.getLogger(Main. class ); |
14 |
15 |
public
static
void
main(String[]
args) { |
16 |
17 |
for
( int
i
= 0 ;
i < 3 ;
i++) { |
18 |
SqlSessionFactory
sqlSessionFactory =
MyBatisSqlSessionFactory.getSqlSessionFactory(); |
19 |
SqlSession
sqlSession = sqlSessionFactory.openSession(); |
20 |
TestingMapper
testingMapper = sqlSession.getMapper(TestingMapper. class ); |
21 |
22 |
List |
23 |
for
(Testing
testing : testings) { |
24 |
logger.debug(testing); |
25 |
} |
26 |
sqlSession.close(); |
27 |
28 |
try
{ |
29 |
Thread.sleep( 3000 ); |
30 |
}
catch
(Exception
e) { |
31 |
logger.error(e,
e); |
32 |
} |
33 |
} |
34 |
} |
35 |
} |
1 |
2013-07-25
15:30:10,648 [Segment] DEBUG
net.sf.ehcache.store.disk.Segment:779 - fault removed 0 from heap |
2 |
2013-07-25
15:30:10,648 [Segment] DEBUG
net.sf.ehcache.store.disk.Segment:796 - fault added 0 on disk |
3 |
2013-07-25
15:30:13,722 [Cache] DEBUG net.sf.ehcache.Cache:1970 - Cache:
com.edw.mybatisehcache.mapper.TestingMapper store hit for
2026218237:1652924294:com.edw.mybatisehcache.mapper.TestingMapper.select:0:2147483647:select |
4 |
* |
5 |
from
testing |
6 |
2013-07-25
15:30:13,722 [Main] DEBUG com.edw.mybatisehcache.main.Main:24 -
testing{id=1, name=edw, address=Ciledug} |
7 |
2013-07-25
15:30:13,722 [Main] DEBUG com.edw.mybatisehcache.main.Main:24 -
testing{id=2, name=kamplenk, address=Cikokol} |
8 |
2013-07-25
15:30:13,722 [Main] DEBUG com.edw.mybatisehcache.main.Main:24 -
testing{id=3, name=nugie, address=Pamulang} |
9 |
2013-07-25
15:30:13,722 [Main] DEBUG com.edw.mybatisehcache.main.Main:24 -
testing{id=4, name=tebek, address=Karawaci} |
4 comments:
zhengjx20160721
adidas superstar shoes
supra footwear
jordan concords
christian louboutin shoes
longchamp le pliage
rolex watches
fit flops
coach outlet store online clearances
celine bags
ray ban sunglasses outlet
kate spade outlet
oakley outlet
louis vuitton outlet online
louis vuitton
ray ban sunglasses outlet
toms shoes
replica rolex watches
michael kors outlet clearance
jordan retro 4
kobe 8
coach factory outlet online
rolex watches
nike free uk
coach factory outlet
air max
kate spade handbags
louis vuitton outlet stores
cheap ray ban sunglasses
kd 8
ralph lauren polo
air jordan 4
jordan 6s
air jordan homme
michael kors outlet clearance
rolex watches
louis vuitton handbags
designer handbags
coach outlet online
cheap basketball shoes
oakley canada
yeezy sneakers
basketball shoes
michael kors factory outlet
fitflops sale clearance
air jordan retro
links of london sale
cheap uggs
discount sunglasses
cheap nfl jerseys
true religion sale
michael kors handbags
http://www.raybanglasses.in.net
tiffany jewellery
ralph lauren polo shirts
nike dunks
tiffany online
oakley store online
oakley sunglasses,oakley outlet sunglasses
cheap real jordans
adidas nmd
ray ban uk,cheap ray ban sunglasses
nike air max 95
christian louboutin sale
ultra boost 3.0
ralph lauren outlet
adidas superstar
jordan shoes
moncler jackets
クロムハーツ
kate spade outlet
ray ban sunglasses
zzzzz2018.7.28
Alors qu'est-ce que la créativité Encore une fois, basket nike femme presto en tirant Dictionary.com, j'ai découvert 3 définitions, et toutes sont précieuses dans ce contexte Avoir la flexibilité ou la puissance électrique pour produire Les êtres humains sont des animaux inventifs. Si je mesure 5 '5 et peser 155lbs plus sain, je ne serai asics duomax homme vraisemblablement pas un secondeur de la NFL de 6'6 250lbs, même si je vais avoir l'air incroyable pour ma forme de système. Pour 46,33 EUR, vous pouvez obtenir; le White Green Oscar asics gel lyte v prix avec Oscar le dessin animé de Sesamestreet sur les côtés. La variété courte BW signifie Big Window. Je vous recommande de vous en tenir à des couleurs fraîches comme le blanc, les pastels ou le bleu de glace. C'est l'élément le plus essentiel adidas zx flux femme rouge pour un briquet excellent.
Post a Comment