Skip to content

Commit 5879993

Browse files
committed
Change sample code for creating Mapper bean using JavaConfig on doc
Fixes gh-124
1 parent df42fae commit 5879993

File tree

5 files changed

+25
-66
lines changed

5 files changed

+25
-66
lines changed

src/site/es/xdoc/mappers.xml

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2010-2017 the original author or authors.
4+
Copyright 2010-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -95,24 +95,14 @@
9595

9696
<h4>Con Java Config</h4>
9797

98-
<p>
99-
Cuando uses Java Config puedes obtener un mapper directamente desde un <code>SqlSessionTemplate</code> como sigue:
100-
</p>
101-
10298
<source><![CDATA[
10399
@Bean
104-
public UserMapper userMapper() throws Exception {
105-
SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
106-
return sessionTemplate.getMapper(UserMapper.class);
100+
public MapperFactoryBean<UserMapper> userMapper() throws Exception {
101+
MapperFactoryBean<UserMapper> factoryBean = new MapperFactoryBean<>(UserMapper.class);
102+
factoryBean.setSqlSessionFactory(sqlSessionFactory());
103+
return factoryBean;
107104
}]]></source>
108105

109-
<p>
110-
Ten en cuenta que no puedes devolver un mapper obtenido de la una <code>SqlSession</code> normal de MyBatis
111-
porque no sería thread safe y solo viviría hasta que la <code>SqlSession</code> que lo creó se cierre.
112-
Debes usar un
113-
<code><a href="sqlsession.html#SqlSessionTemplate">SqlSessionTemplate</a></code> en su lugar,
114-
como se muestra en el ejemplo.
115-
</p>
116106
</subsection>
117107

118108
<subsection name="Escanear mappers" id="scan">

src/site/ja/xdoc/mappers.xml

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2010-2017 the original author or authors.
4+
Copyright 2010-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -86,22 +86,14 @@
8686

8787
<h4>Java Config で設定する場合</h4>
8888

89-
<p>
90-
Java Config を使って Spring を設定する場合は、次のようにして <code>SqlSessionTemplate</code> から直接 Mapper を取得することができます。
91-
</p>
92-
9389
<source><![CDATA[
9490
@Bean
95-
public UserMapper userMapper() throws Exception {
96-
SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
97-
return sessionTemplate.getMapper(UserMapper.class);
91+
public MapperFactoryBean<UserMapper> userMapper() throws Exception {
92+
MapperFactoryBean<UserMapper> factoryBean = new MapperFactoryBean<>(UserMapper.class);
93+
factoryBean.setSqlSessionFactory(sqlSessionFactory());
94+
return factoryBean;
9895
}]]></source>
9996

100-
<p>
101-
ここで、MyBatis のデフォルトの <code>SqlSession</code> から取得した Mapper を返してはいけません。
102-
なぜなら、この Mapper はスレッドセーフではなく、取得元の <code>SqlSession</code> がクローズされると破棄されてしまうからです。
103-
上記の例のように、必ず <code><a href="sqlsession.html#SqlSessionTemplate">SqlSessionTemplate</a></code> を使って Mapper を取得するようにしてください。
104-
</p>
10597
</subsection>
10698

10799
<subsection name="Mapper の自動検出" id="scan">

src/site/ko/xdoc/mappers.xml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2010-2017 the original author or authors.
4+
Copyright 2010-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -75,18 +75,14 @@
7575

7676
<h4>자바설정 사용</h4>
7777

78-
<p>스프링의 Java Config기능을 사용하면 다음처럼 <code>SqlSessionTemplate</code> 에서 직접 매퍼를 얻을 수 있다. </p>
79-
8078
<source><![CDATA[
8179
@Bean
82-
public UserMapper userMapper() throws Exception {
83-
SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
84-
return sessionTemplate.getMapper(UserMapper.class);
80+
public MapperFactoryBean<UserMapper> userMapper() throws Exception {
81+
MapperFactoryBean<UserMapper> factoryBean = new MapperFactoryBean<>(UserMapper.class);
82+
factoryBean.setSqlSessionFactory(sqlSessionFactory());
83+
return factoryBean;
8584
}]]></source>
8685

87-
<p>마이바티스의 디폴트 <code>SqlSession</code>에서 매퍼를 리턴받을수 없다.
88-
디폴트 <code>SqlSession</code>은 쓰레드에 안전하지 않고 생성된 <code>SqlSession</code>이 닫힐때까지만 살아있기 때문이다.
89-
대신 샘플코드에서 보여주는 것처럼 <code><a href="sqlsession.html#SqlSessionTemplate">SqlSessionTemplate</a></code> 를 사용해야만 한다. </p>
9086
</subsection>
9187

9288
<subsection name="매퍼 스캔" id="scan">

src/site/xdoc/mappers.xml

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2010-2017 the original author or authors.
4+
Copyright 2010-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -99,27 +99,14 @@
9999

100100
<h4>With Java Config</h4>
101101

102-
<p>
103-
When using Spring Java Config you can get a mapper directly out
104-
of an <code>SqlSessionTemplate</code> like follows:
105-
</p>
106-
107102
<source><![CDATA[
108103
@Bean
109-
public UserMapper userMapper() throws Exception {
110-
SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
111-
return sessionTemplate.getMapper(UserMapper.class);
104+
public MapperFactoryBean<UserMapper> userMapper() throws Exception {
105+
MapperFactoryBean<UserMapper> factoryBean = new MapperFactoryBean<>(UserMapper.class);
106+
factoryBean.setSqlSessionFactory(sqlSessionFactory());
107+
return factoryBean;
112108
}]]></source>
113109

114-
<p>
115-
Note that you can not return a mapper got from the MyBatis default <code>SqlSession</code> because
116-
it would not be thread safe and will only live until the <code>SqlSession</code>
117-
it was created from is closed. You must use an
118-
<code><a href="sqlsession.html#SqlSessionTemplate">SqlSessionTemplate</a></code> instead,
119-
as shown in the sample.
120-
</p>
121-
</subsection>
122-
123110
<subsection name="Scanning for mappers" id="scan">
124111
<p>
125112
There is no need to register all your mappers one by one.

src/site/zh/xdoc/mappers.xml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2010-2018 the original author or authors.
4+
Copyright 2010-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -81,20 +81,14 @@
8181

8282
<h4>Java 配置</h4>
8383

84-
<p>
85-
当使用 Spring 的 Java 配置时,你可以直接从一个 <code>SqlSessionTemplate</code> 获取映射器,就像下面这样:
86-
</p>
87-
8884
<source><![CDATA[
8985
@Bean
90-
public UserMapper userMapper() throws Exception {
91-
SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
92-
return sessionTemplate.getMapper(UserMapper.class);
86+
public MapperFactoryBean<UserMapper> userMapper() throws Exception {
87+
MapperFactoryBean<UserMapper> factoryBean = new MapperFactoryBean<>(UserMapper.class);
88+
factoryBean.setSqlSessionFactory(sqlSessionFactory());
89+
return factoryBean;
9390
}]]></source>
9491

95-
<p>
96-
注意,你不能返回一个从 MyBatis 默认的 <code>SqlSession</code> 中获取的映射器,因为它是非线程安全的,并且只会在父 <code>SqlSession</code> 关闭之前有效。你必须使用 <code><a href="sqlsession.html#SqlSessionTemplate">SqlSessionTemplate</a></code> 代替,就像上面的例子所示。
97-
</p>
9892
</subsection>
9993

10094
<subsection name="发现映射器" id="scan">

0 commit comments

Comments
 (0)