Skip to content

Commit 953e998

Browse files
committed
增加测试,增加mybatis3.3.1新功能的例子
1 parent e371d59 commit 953e998

File tree

5 files changed

+261
-0
lines changed

5 files changed

+261
-0
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
<groupId>org.springframework.boot</groupId>
4141
<artifactId>spring-boot-starter-freemarker</artifactId>
4242
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
</dependency>
4347

4448
<dependency>
4549
<groupId>mysql</groupId>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2016 abel533@gmail.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package tk.mybatis.springboot.mapper;
26+
27+
import org.apache.ibatis.annotations.*;
28+
import tk.mybatis.springboot.model.City2;
29+
30+
import java.util.List;
31+
32+
/**
33+
* mybatis3.3.1版本新增功能测试
34+
*
35+
* @author liuzh
36+
* @since 2016-03-06 17:22
37+
*/
38+
public interface MyBatis331Mapper {
39+
40+
/**
41+
* 批量插入
42+
*
43+
* @param cities
44+
* @return
45+
*/
46+
@Insert("<script>" +
47+
"insert into city (id, name, state) values " +
48+
"<foreach collection=\"list\" item=\"city\" separator=\",\" >" +
49+
"(#{city.id}, #{city.cityName}, #{city.cityState})" +
50+
"</foreach>" +
51+
"</script>")
52+
@Options(useGeneratedKeys = true, keyProperty = "id")
53+
int insertCities(List<City2> cities);
54+
55+
@Results(id = "cityResult", value = {
56+
@Result(property = "id", column = "id", id = true),
57+
@Result(property = "cityName", column = "name", id = true),
58+
@Result(property = "cityState", column = "state", id = true)
59+
})
60+
@Select("select id, name, state from city where id = #{id}")
61+
City2 selectByCityId(Integer id);
62+
63+
64+
@ResultMap("cityResult")
65+
@Select("select id, name, state from city")
66+
List<City2> selectAll();
67+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2016 abel533@gmail.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package tk.mybatis.springboot.mapper;
26+
27+
import org.junit.Assert;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
import org.springframework.beans.factory.annotation.Autowired;
33+
import org.springframework.boot.test.SpringApplicationConfiguration;
34+
import org.springframework.test.annotation.Rollback;
35+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36+
import org.springframework.test.context.web.WebAppConfiguration;
37+
import org.springframework.transaction.annotation.Transactional;
38+
import tk.mybatis.springboot.Application;
39+
import tk.mybatis.springboot.model.City2;
40+
41+
import java.util.ArrayList;
42+
import java.util.List;
43+
44+
/**
45+
* @author liuzh
46+
* @since 2016-03-06 17:42
47+
*/
48+
@RunWith(SpringJUnit4ClassRunner.class)
49+
@WebAppConfiguration
50+
@Transactional
51+
@SpringApplicationConfiguration(Application.class)
52+
public class MyBatis331Test {
53+
private Logger logger = LoggerFactory.getLogger(getClass());
54+
55+
@Autowired
56+
private MyBatis331Mapper mapper;
57+
58+
@Test
59+
@Rollback
60+
public void testInsertList() {
61+
List<City2> city2List = new ArrayList<City2>();
62+
city2List.add(new City2("石家庄", "河北"));
63+
city2List.add(new City2("邯郸", "河北"));
64+
city2List.add(new City2("秦皇岛", "河北"));
65+
Assert.assertEquals(3, mapper.insertCities(city2List));
66+
for (City2 c2 : city2List) {
67+
logger.info(c2.toString());
68+
Assert.assertNotNull(c2.getId());
69+
}
70+
}
71+
72+
@Test
73+
public void testSelectById(){
74+
City2 city2 = mapper.selectByCityId(1);
75+
logger.info(city2.toString());
76+
Assert.assertNotNull(city2);
77+
Assert.assertNotNull(city2.getCityName());
78+
Assert.assertNotNull(city2.getCityState());
79+
}
80+
81+
@Test
82+
public void testSelectAll(){
83+
List<City2> city2List = mapper.selectAll();
84+
for(City2 c2 : city2List){
85+
logger.info(c2.toString());
86+
Assert.assertNotNull(c2);
87+
Assert.assertNotNull(c2.getCityName());
88+
Assert.assertNotNull(c2.getCityState());
89+
}
90+
}
91+
92+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
SET FOREIGN_KEY_CHECKS=0;
2+
3+
-- ----------------------------
4+
-- Table structure for city
5+
-- ----------------------------
6+
DROP TABLE IF EXISTS `city`;
7+
CREATE TABLE `city` (
8+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
9+
`name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
10+
`state` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
11+
PRIMARY KEY (`id`)
12+
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
13+
14+
-- ----------------------------
15+
-- Records of city
16+
-- ----------------------------
17+
INSERT INTO `city` VALUES ('1', '石家庄', '河北');
18+
INSERT INTO `city` VALUES ('2', '邯郸', '河北');
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2016 abel533@gmail.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package tk.mybatis.springboot.model;
26+
27+
/**
28+
* mybatis 3.3.1新功能测试使用
29+
*
30+
* @author liuzh
31+
* @since 2016-01-22 22:16
32+
*/
33+
public class City2 {
34+
private Integer id;
35+
36+
private String cityName;
37+
38+
private String cityState;
39+
40+
public City2() {
41+
}
42+
43+
public City2(String cityName, String cityState) {
44+
this.cityName = cityName;
45+
this.cityState = cityState;
46+
}
47+
48+
public Integer getId() {
49+
return id;
50+
}
51+
52+
public void setId(Integer id) {
53+
this.id = id;
54+
}
55+
56+
public String getCityName() {
57+
return cityName;
58+
}
59+
60+
public void setCityName(String cityName) {
61+
this.cityName = cityName;
62+
}
63+
64+
public String getCityState() {
65+
return cityState;
66+
}
67+
68+
public void setCityState(String cityState) {
69+
this.cityState = cityState;
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return "City2{" +
75+
"id=" + id +
76+
", cityName='" + cityName + '\'' +
77+
", cityState='" + cityState + '\'' +
78+
'}';
79+
}
80+
}

0 commit comments

Comments
 (0)