Skip to content

Commit 3bf2a46

Browse files
committed
http://code.google.com/p/mybatis/issues/detail?id=100 : Some samples of code to show the 3 ways to integrate with spring by Eduardo Macarron
1 parent a1384c5 commit 3bf2a46

11 files changed

+177
-52
lines changed

src/test/java/sample/FooService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
import org.springframework.transaction.annotation.Transactional;
1919

2020
/**
21+
* FooService acts as a bussiness service.
2122
*
23+
* All calls to any method of FooService are transactional.
2224
*
2325
* @version $Id$
2426
*/
2527
@Transactional
2628
public interface FooService {
2729

28-
User doSomeBusinessStuff();
30+
User doSomeBusinessStuff(String userId);
2931

3032
}

src/test/java/sample/FooServiceImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
package sample;
1717

1818
/**
19+
* Impl of the FooService.
20+
*
21+
* FooService simply receives a userId and uses a mapper/dao to get a record from the database. .
1922
*
2023
* @version $Id$
2124
*/
@@ -27,11 +30,8 @@ public void setUserMapper(UserMapper userMapper) {
2730
this.userMapper = userMapper;
2831
}
2932

30-
public User doSomeBusinessStuff() {
31-
User user = new User();
32-
user.setId("u1");
33-
user.setName("Pocoyo");
34-
return this.userMapper.getUser(user);
33+
public User doSomeBusinessStuff(String userId) {
34+
return this.userMapper.getUser(userId);
3535
}
3636

3737
}

src/test/java/sample/MapperFactoryBeanTest.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2424

2525
/**
26-
*
26+
* Example of MyBatis-Spring integration usage.
2727
*
2828
* @version $Id$
2929
*/
@@ -32,13 +32,45 @@
3232
public class MapperFactoryBeanTest {
3333

3434
@Autowired
35-
private FooService service;
35+
private FooService fooService1;
36+
37+
@Autowired
38+
private FooService fooService2;
39+
40+
@Autowired
41+
private FooService fooService3;
42+
43+
public void setFooService1(FooService fooService1) {
44+
this.fooService1 = fooService1;
45+
}
46+
47+
public void setFooService2(FooService fooService2) {
48+
this.fooService2 = fooService2;
49+
}
50+
51+
public void setFooService3(FooService fooService3) {
52+
this.fooService3 = fooService3;
53+
}
54+
55+
@Test
56+
public void testFooService1() throws Exception {
57+
User user = this.fooService1.doSomeBusinessStuff("u1");
58+
Assert.assertNotNull(user);
59+
Assert.assertEquals("Pocoyo",user.getName());
60+
}
61+
62+
@Test
63+
public void testFooService2() throws Exception {
64+
User user = this.fooService2.doSomeBusinessStuff("u1");
65+
Assert.assertNotNull(user);
66+
Assert.assertEquals("Pocoyo",user.getName());
67+
}
3668

3769
@Test
38-
public void testFactoryBean() throws Exception {
39-
User user = this.service.doSomeBusinessStuff();
70+
public void testFooService3() throws Exception {
71+
User user = this.fooService3.doSomeBusinessStuff("u1");
4072
Assert.assertNotNull(user);
41-
Assert.assertEquals(user.getId(), "u1");
73+
Assert.assertEquals("Pocoyo",user.getName());
4274
}
4375

4476
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2010 The myBatis Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package sample;
17+
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.test.context.ContextConfiguration;
23+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
24+
25+
/**
26+
* Example of MyBatis-Spring integration usage
27+
*
28+
* @version $Id$
29+
*/
30+
@ContextConfiguration(locations = {"classpath:sample/context.xml"})
31+
@RunWith(SpringJUnit4ClassRunner.class)
32+
public class MyBatisSampleTest {
33+
34+
@Autowired
35+
private FooService fooService1;
36+
@Autowired
37+
private FooService fooService2;
38+
@Autowired
39+
private FooService fooService3;
40+
41+
public void setFooService1(FooService fooService1) {
42+
this.fooService1 = fooService1;
43+
}
44+
45+
public void setFooService2(FooService fooService2) {
46+
this.fooService2 = fooService2;
47+
}
48+
49+
public void setFooService3(FooService fooService3) {
50+
this.fooService3 = fooService3;
51+
}
52+
53+
@Test
54+
public void testFooService1() throws Exception {
55+
User user = this.fooService1.doSomeBusinessStuff("u1");
56+
Assert.assertNotNull(user);
57+
Assert.assertEquals("Pocoyo",user.getName());
58+
}
59+
60+
@Test
61+
public void testFooService2() throws Exception {
62+
User user = this.fooService2.doSomeBusinessStuff("u1");
63+
Assert.assertNotNull(user);
64+
Assert.assertEquals("Pocoyo",user.getName());
65+
}
66+
67+
@Test
68+
public void testFooService3() throws Exception {
69+
User user = this.fooService3.doSomeBusinessStuff("u1");
70+
Assert.assertNotNull(user);
71+
Assert.assertEquals("Pocoyo",user.getName());
72+
}
73+
}

src/test/java/sample/User.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package sample;
1717

1818
/**
19-
*
19+
* A simple bean that holds User info.
2020
*
2121
* @version $Id$
2222
*/

src/test/java/sample/UserMapper.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@
1515
*/
1616
package sample;
1717

18-
import java.util.List;
19-
2018
/**
2119
*
20+
* Mapper/DAO inteface.
2221
*
2322
* @version $Id$
2423
*/
2524
public interface UserMapper {
2625

27-
// @Select("select * from users where id=#{id} and name=#{name}")
28-
User getUser(User user);
29-
30-
// @Select("select * from users")
31-
List<User> getUsers();
26+
User getUser(String userId);
3227

3328
}

src/test/java/sample/UserMapperSqlSessionImpl.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
*/
1616
package sample;
1717

18-
import java.util.List;
19-
2018
import org.apache.ibatis.session.SqlSession;
2119
import org.apache.ibatis.session.SqlSessionFactory;
2220

2321
/**
24-
*
22+
* This DAO is injected with a SqlSessionFactory that is used to get a SqlSession and call MyBatis API.
2523
*
2624
* @version $Id$
2725
*/
@@ -33,19 +31,11 @@ public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
3331
this.sqlSessionFactory = sqlSessionFactory;
3432
}
3533

36-
public User getUser(User user) {
34+
public User getUser(String userId) {
3735
SqlSession session = sqlSessionFactory.openSession();
38-
user = (User) session.selectOne("sample.UserMapper.getUser", user);
36+
User user = (User) session.selectOne("sample.UserMapper.getUser", userId);
3937
session.close();
4038
return user;
4139
}
4240

43-
public List<User> getUsers() {
44-
SqlSession session = sqlSessionFactory.openSession();
45-
@SuppressWarnings("unchecked")
46-
List<User> users = (List<User>) session.selectOne("sample.UserMapper.getUsers");
47-
session.close();
48-
return users;
49-
}
50-
5141
}

src/test/java/sample/UserMapperTemplateImpl.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,19 @@
1515
*/
1616
package sample;
1717

18-
import java.util.List;
19-
2018
import org.mybatis.spring.support.SqlSessionDaoSupport;
2119

2220
/**
23-
*
21+
* This DAO extends SqlSessionDaoSupport and uses SqlSessionTemplate instead of MyBatis API
22+
* SqlSessions are handled by Spring.
23+
* MyBatis exceptions are translated to Spring Data Exceptions.
2424
*
2525
* @version $Id$
2626
*/
2727
public class UserMapperTemplateImpl extends SqlSessionDaoSupport implements UserMapper {
2828

29-
public User getUser(User user) {
30-
return (User) getSqlSessionTemplate().selectOne("sample.UserMapper.getUser", user);
31-
}
32-
33-
public List<User> getUsers() {
34-
return getSqlSessionTemplate().selectList("sample.UserMapper.getUser");
29+
public User getUser(String userId) {
30+
return (User) getSqlSessionTemplate().selectOne("sample.UserMapper.getUser", userId);
3531
}
3632

3733
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2010 The myBatis Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Sample for three usage scenarios.
19+
* <ul>
20+
* <li>Extending SqlSessionDaoSupport</li>
21+
* <li>Using directly MyBatis API (SqlSession)</li>
22+
* <li>Injecting Mappers (in fact this means that there is no DAO implementation)</li>
23+
* </ul>
24+
*
25+
* @version $Id$
26+
*/
27+
package sample;

src/test/resources/sample/UserMapper.xml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@
2323
-->
2424
<mapper namespace="sample.UserMapper">
2525

26-
<select id="getUser" resultType="sample.User" parameterType="sample.User">
27-
select * from users where id=#{id} and name=#{name}
28-
</select>
29-
30-
<select id="getUsers" resultType="sample.User" >
31-
select * from users
26+
<select id="getUser" resultType="sample.User" parameterType="java.lang.String">
27+
select * from users where id=#{value}
3228
</select>
3329

3430
</mapper>

0 commit comments

Comments
 (0)