Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 937b0f3

Browse files
committed
更新到Mapper3.2.2,增加增删改查和分页例子。
1 parent 65d8707 commit 937b0f3

File tree

15 files changed

+424
-10718
lines changed

15 files changed

+424
-10718
lines changed

pom.xml

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<!-- 编译jdk版本 -->
2323
<jdk.version>1.6</jdk.version>
2424
<!-- 依赖版本 -->
25-
<mapper.version>3.2.2-SNAPSHOT</mapper.version>
25+
<mapper.version>3.2.2</mapper.version>
2626
<pagehelper.version>4.0.0</pagehelper.version>
2727
<mysql.version>5.1.29</mysql.version>
2828
<spring.version>4.1.2.RELEASE</spring.version>
@@ -50,14 +50,14 @@
5050
</dependency>
5151
<dependency>
5252
<groupId>javax.servlet.jsp</groupId>
53-
<artifactId>javax.servlet.jsp-api</artifactId>
54-
<version>2.3.1</version>
53+
<artifactId>jsp-api</artifactId>
54+
<version>2.1</version>
5555
<scope>provided</scope>
5656
</dependency>
5757
<dependency>
58-
<groupId>javax.servlet.jsp.jstl</groupId>
59-
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
60-
<version>1.2.1</version>
58+
<groupId>javax.servlet</groupId>
59+
<artifactId>jstl</artifactId>
60+
<version>1.2</version>
6161
</dependency>
6262
<dependency>
6363
<groupId>javax.ws.rs</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.isea533.mybatis.controller.demo;
2+
3+
import com.github.pagehelper.PageInfo;
4+
import com.isea533.mybatis.model.Country;
5+
import com.isea533.mybatis.service.CountryService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.servlet.ModelAndView;
12+
13+
import java.util.List;
14+
15+
/**
16+
* @author liuzh_3nofxnp
17+
* @since 2015-09-19 17:15
18+
*/
19+
@Controller
20+
public class CountryController {
21+
22+
@Autowired
23+
private CountryService countryService;
24+
25+
private String page_list = "index";
26+
27+
private String redirect_list = "redirect:list";
28+
29+
@RequestMapping(value = {"list", "index", "index.html", ""})
30+
public ModelAndView getList(Country country,
31+
@RequestParam(required = false, defaultValue = "1") int page,
32+
@RequestParam(required = false, defaultValue = "10") int rows) {
33+
ModelAndView result = new ModelAndView(page_list);
34+
List<Country> countryList = countryService.selectByCountry(country, page, rows);
35+
result.addObject("pageInfo", new PageInfo<Country>(countryList));
36+
result.addObject("queryParam", country);
37+
result.addObject("page", page);
38+
result.addObject("rows", rows);
39+
return result;
40+
}
41+
42+
@RequestMapping(value = "view", method = RequestMethod.GET)
43+
public ModelAndView view(Country country) {
44+
ModelAndView result = new ModelAndView();
45+
if (country.getId() != null) {
46+
country = countryService.selectByKey(country.getId());
47+
}
48+
result.addObject("country", country);
49+
return result;
50+
}
51+
52+
@RequestMapping(value = "save", method = RequestMethod.POST)
53+
public ModelAndView save(Country country) {
54+
ModelAndView result = new ModelAndView(redirect_list);
55+
if (country.getId() != null) {
56+
countryService.updateAll(country);
57+
} else {
58+
countryService.save(country);
59+
}
60+
return result;
61+
}
62+
63+
@RequestMapping("delete")
64+
public String delete(Integer id) {
65+
countryService.delete(id);
66+
return redirect_list;
67+
}
68+
69+
}

src/main/java/com/isea533/mybatis/controller/demo/DemoController.java

-60
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.isea533.mybatis.service;
2+
3+
import com.isea533.mybatis.model.Country;
4+
5+
import java.util.List;
6+
7+
/**
8+
* @author liuzh_3nofxnp
9+
* @since 2015-09-19 17:17
10+
*/
11+
public interface CountryService extends IService<Country> {
12+
13+
/**
14+
* 根据条件分页查询
15+
*
16+
* @param country
17+
* @param page
18+
* @param rows
19+
* @return
20+
*/
21+
List<Country> selectByCountry(Country country, int page, int rows);
22+
23+
}

src/main/java/com/isea533/mybatis/service/DemoService.java renamed to src/main/java/com/isea533/mybatis/service/IService.java

+17-26
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,27 @@
2424

2525
package com.isea533.mybatis.service;
2626

27-
import com.isea533.mybatis.model.Country;
28-
import org.springframework.beans.factory.annotation.Autowired;
2927
import org.springframework.stereotype.Service;
30-
import tk.mybatis.mapper.common.Mapper;
28+
29+
import java.util.List;
3130

3231
/**
33-
* @author liuzh
32+
* 通用接口
3433
*/
3534
@Service
36-
public class DemoService extends BaseService<Country> {
37-
38-
@Autowired
39-
private Mapper<Country> countryMapper;
40-
41-
public int save(Country country) {
42-
if (country == null) {
43-
throw new NullPointerException("保存的对象不能为空!");
44-
}
45-
if (country.getCountrycode() == null || country.getCountrycode().equals("")) {
46-
throw new RuntimeException("国家代码不能为空!");
47-
}
48-
if (country.getCountryname() == null || country.getCountryname().equals("")) {
49-
throw new RuntimeException("国家名称不能为空!");
50-
}
51-
return super.save(country);
52-
}
53-
54-
public void test() {
55-
int result = countryMapper.selectCount(null);
56-
System.out.println(result);
57-
}
35+
public interface IService<T> {
36+
37+
T selectByKey(Object key);
38+
39+
int save(T entity);
40+
41+
int delete(Object key);
42+
43+
int updateAll(T entity);
44+
45+
int updateNotNull(T entity);
46+
47+
List<T> selectByExample(Object example);
5848

49+
//TODO 其他...
5950
}

src/main/java/com/isea533/mybatis/service/BaseService.java renamed to src/main/java/com/isea533/mybatis/service/impl/BaseService.java

+24-18
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,48 @@
2222
* THE SOFTWARE.
2323
*/
2424

25-
package com.isea533.mybatis.service;
25+
package com.isea533.mybatis.service.impl;
2626

27-
import tk.mybatis.mapper.common.Mapper;
28-
import com.github.pagehelper.PageHelper;
27+
import com.isea533.mybatis.service.IService;
2928
import org.springframework.beans.factory.annotation.Autowired;
3029
import org.springframework.stereotype.Service;
30+
import tk.mybatis.mapper.common.Mapper;
3131

3232
import java.util.List;
3333

3434
/**
3535
* Created by liuzh on 2014/12/11.
3636
*/
3737
@Service
38-
public abstract class BaseService<T> {
38+
public abstract class BaseService<T> implements IService<T> {
3939

4040
@Autowired
4141
protected Mapper<T> mapper;
4242

43-
public int save(T entity){
43+
@Override
44+
public T selectByKey(Object key) {
45+
return mapper.selectByPrimaryKey(key);
46+
}
47+
48+
public int save(T entity) {
4449
return mapper.insert(entity);
4550
}
4651

47-
public int delete(T entity){
48-
return mapper.deleteByPrimaryKey(entity);
52+
public int delete(Object key) {
53+
return mapper.deleteByPrimaryKey(key);
4954
}
5055

51-
/**
52-
* 单表分页查询
53-
*
54-
* @param pageNum
55-
* @param pageSize
56-
* @return
57-
*/
58-
public List<T> selectPage(int pageNum,int pageSize){
59-
PageHelper.startPage(pageNum, pageSize);
60-
//Spring4支持泛型注入
61-
return mapper.select(null);
56+
public int updateAll(T entity) {
57+
return mapper.updateByPrimaryKey(entity);
6258
}
59+
60+
public int updateNotNull(T entity) {
61+
return mapper.updateByPrimaryKeySelective(entity);
62+
}
63+
64+
public List<T> selectByExample(Object example) {
65+
return mapper.selectByExample(example);
66+
}
67+
68+
//TODO 其他...
6369
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.isea533.mybatis.service.impl;
2+
3+
import com.github.pagehelper.PageHelper;
4+
import com.isea533.mybatis.model.Country;
5+
import com.isea533.mybatis.service.CountryService;
6+
import org.springframework.stereotype.Service;
7+
import tk.mybatis.mapper.entity.Example;
8+
import tk.mybatis.mapper.util.StringUtil;
9+
10+
import java.util.List;
11+
12+
/**
13+
* @author liuzh_3nofxnp
14+
* @since 2015-09-19 17:17
15+
*/
16+
@Service("countryService")
17+
public class CountryServiceImpl extends BaseService<Country> implements CountryService {
18+
19+
@Override
20+
public List<Country> selectByCountry(Country country, int page, int rows) {
21+
Example example = new Example(Country.class);
22+
Example.Criteria criteria = example.createCriteria();
23+
if (StringUtil.isNotEmpty(country.getCountryname())) {
24+
criteria.andLike("countryname", "%" + country.getCountryname() + "%");
25+
}
26+
if (StringUtil.isNotEmpty(country.getCountrycode())) {
27+
criteria.andLike("countrycode", "%" + country.getCountrycode() + "%");
28+
}
29+
if (country.getId() != null) {
30+
criteria.andEqualTo("id", country.getId());
31+
}
32+
//分页查询
33+
PageHelper.startPage(page, rows);
34+
return selectByExample(example);
35+
}
36+
37+
}

src/main/resources/applicationContext.xml

+8
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,15 @@
8787

8888
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
8989
<property name="basePackage" value="com.isea533.mybatis.mapper"/>
90+
<!-- 3.2.2版本新特性,markerInterface可以起到mappers配置的作用,详细情况需要看Marker接口类 -->
9091
<property name="markerInterface" value="com.isea533.mybatis.util.MyMapper"/>
92+
<!-- 通用Mapper通过属性注入进行配置,默认不配置时会注册Mapper<T>接口
93+
<property name="properties">
94+
<value>
95+
mappers=tk.mybatis.mapper.common.Mapper
96+
</value>
97+
</property>
98+
-->
9199
</bean>
92100

93101
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">

0 commit comments

Comments
 (0)