File tree 5 files changed +25
-66
lines changed
5 files changed +25
-66
lines changed Original file line number Diff line number Diff line change 1
1
<?xml version =" 1.0" encoding =" UTF-8" ?>
2
2
<!--
3
3
4
- Copyright 2010-2017 the original author or authors.
4
+ Copyright 2010-2019 the original author or authors.
5
5
6
6
Licensed under the Apache License, Version 2.0 (the "License");
7
7
you may not use this file except in compliance with the License.
95
95
96
96
<h4 >Con Java Config</h4 >
97
97
98
- <p >
99
- Cuando uses Java Config puedes obtener un mapper directamente desde un <code >SqlSessionTemplate</code > como sigue:
100
- </p >
101
-
102
98
<source ><![CDATA[
103
99
@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;
107
104
}]]> </source >
108
105
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 >
116
106
</subsection >
117
107
118
108
<subsection name =" Escanear mappers" id =" scan" >
Original file line number Diff line number Diff line change 1
1
<?xml version =" 1.0" encoding =" UTF-8" ?>
2
2
<!--
3
3
4
- Copyright 2010-2017 the original author or authors.
4
+ Copyright 2010-2019 the original author or authors.
5
5
6
6
Licensed under the Apache License, Version 2.0 (the "License");
7
7
you may not use this file except in compliance with the License.
86
86
87
87
<h4 >Java Config で設定する場合</h4 >
88
88
89
- <p >
90
- Java Config を使って Spring を設定する場合は、次のようにして <code >SqlSessionTemplate</code > から直接 Mapper を取得することができます。
91
- </p >
92
-
93
89
<source ><![CDATA[
94
90
@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;
98
95
}]]> </source >
99
96
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 >
105
97
</subsection >
106
98
107
99
<subsection name =" Mapper の自動検出" id =" scan" >
Original file line number Diff line number Diff line change 1
1
<?xml version =" 1.0" encoding =" UTF-8" ?>
2
2
<!--
3
3
4
- Copyright 2010-2017 the original author or authors.
4
+ Copyright 2010-2019 the original author or authors.
5
5
6
6
Licensed under the Apache License, Version 2.0 (the "License");
7
7
you may not use this file except in compliance with the License.
75
75
76
76
<h4 >자바설정 사용</h4 >
77
77
78
- <p >스프링의 Java Config기능을 사용하면 다음처럼 <code >SqlSessionTemplate</code > 에서 직접 매퍼를 얻을 수 있다. </p >
79
-
80
78
<source ><![CDATA[
81
79
@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;
85
84
}]]> </source >
86
85
87
- <p >마이바티스의 디폴트 <code >SqlSession</code >에서 매퍼를 리턴받을수 없다.
88
- 디폴트 <code >SqlSession</code >은 쓰레드에 안전하지 않고 생성된 <code >SqlSession</code >이 닫힐때까지만 살아있기 때문이다.
89
- 대신 샘플코드에서 보여주는 것처럼 <code ><a href =" sqlsession.html#SqlSessionTemplate" >SqlSessionTemplate</a ></code > 를 사용해야만 한다. </p >
90
86
</subsection >
91
87
92
88
<subsection name =" 매퍼 스캔" id =" scan" >
Original file line number Diff line number Diff line change 1
1
<?xml version =" 1.0" encoding =" UTF-8" ?>
2
2
<!--
3
3
4
- Copyright 2010-2017 the original author or authors.
4
+ Copyright 2010-2019 the original author or authors.
5
5
6
6
Licensed under the Apache License, Version 2.0 (the "License");
7
7
you may not use this file except in compliance with the License.
99
99
100
100
<h4 >With Java Config</h4 >
101
101
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
-
107
102
<source ><![CDATA[
108
103
@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;
112
108
}]]> </source >
113
109
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
-
123
110
<subsection name =" Scanning for mappers" id =" scan" >
124
111
<p >
125
112
There is no need to register all your mappers one by one.
Original file line number Diff line number Diff line change 1
1
<?xml version =" 1.0" encoding =" UTF-8" ?>
2
2
<!--
3
3
4
- Copyright 2010-2018 the original author or authors.
4
+ Copyright 2010-2019 the original author or authors.
5
5
6
6
Licensed under the Apache License, Version 2.0 (the "License");
7
7
you may not use this file except in compliance with the License.
81
81
82
82
<h4 >Java 配置</h4 >
83
83
84
- <p >
85
- 当使用 Spring 的 Java 配置时,你可以直接从一个 <code >SqlSessionTemplate</code > 获取映射器,就像下面这样:
86
- </p >
87
-
88
84
<source ><![CDATA[
89
85
@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;
93
90
}]]> </source >
94
91
95
- <p >
96
- 注意,你不能返回一个从 MyBatis 默认的 <code >SqlSession</code > 中获取的映射器,因为它是非线程安全的,并且只会在父 <code >SqlSession</code > 关闭之前有效。你必须使用 <code ><a href =" sqlsession.html#SqlSessionTemplate" >SqlSessionTemplate</a ></code > 代替,就像上面的例子所示。
97
- </p >
98
92
</subsection >
99
93
100
94
<subsection name =" 发现映射器" id =" scan" >
You can’t perform that action at this time.
0 commit comments