Skip to content

Commit 5086e93

Browse files
committed
根据文档更新。
1 parent 183c8ad commit 5086e93

File tree

6 files changed

+39
-99
lines changed

6 files changed

+39
-99
lines changed
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package com.isea533.mybatis.controller.demo;
22

3-
import com.isea533.mybatis.model.Country;
43
import com.isea533.mybatis.model.Country2;
54
import com.isea533.mybatis.service.DemoService;
65
import org.springframework.beans.factory.annotation.Autowired;
7-
import org.springframework.http.HttpEntity;
8-
import org.springframework.http.HttpStatus;
9-
import org.springframework.http.ResponseEntity;
106
import org.springframework.stereotype.Controller;
117
import org.springframework.web.bind.annotation.RequestMapping;
128
import org.springframework.web.bind.annotation.RequestParam;
@@ -28,23 +24,12 @@ public String index() {
2824
return "index";
2925
}
3026

31-
@ResponseBody
32-
@RequestMapping("test1")
33-
public Country requestTest5(HttpEntity<String> message) {
34-
return demoService.selectById(35);
35-
}
36-
37-
@RequestMapping("test2")
38-
public ResponseEntity<Country> requestTest6() {
39-
return new ResponseEntity<Country>(demoService.selectById2(35), HttpStatus.OK);
40-
}
41-
4227
@ResponseBody
4328
@RequestMapping("test3")
4429
public List<Country2> requestTest7(
4530
@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
4631
@RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
4732
) {
48-
return demoService.selectPage2(pageNum, pageSize);
33+
return demoService.selectPage(pageNum, pageSize);
4934
}
5035
}

src/main/java/com/isea533/mybatis/model/Country2.java

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,16 @@
55

66
@Table(name="country")
77
public class Country2 {
8-
/**
9-
* This field was generated by MyBatis Generator.
10-
* This field corresponds to the database column country.id
11-
*
12-
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
13-
*/
148
@Id
159
private Integer id;
16-
17-
/**
18-
* This field was generated by MyBatis Generator.
19-
* This field corresponds to the database column country.countryname
20-
*
21-
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
22-
*/
2310
private String countryname;
24-
25-
/**
26-
* This field was generated by MyBatis Generator.
27-
* This field corresponds to the database column country.countrycode
28-
*
29-
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
30-
*/
3111
private String countrycode;
3212

33-
/**
34-
* This method was generated by MyBatis Generator.
35-
* This method returns the value of the database column country.id
36-
*
37-
* @return the value of country.id
38-
*
39-
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
40-
*/
13+
4114
public Integer getId() {
4215
return id;
4316
}
4417

45-
/**
46-
* This method was generated by MyBatis Generator.
47-
* This method sets the value of the database column country.id
48-
*
49-
* @param id the value for country.id
50-
*
51-
* @mbggenerated Mon Dec 08 11:25:39 CST 2014
52-
*/
5318
public void setId(Integer id) {
5419
this.id = id;
5520
}
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11
package com.isea533.mybatis.service;
22

33
import com.isea533.mybatis.mapperhelper.Mapper;
4+
import com.isea533.mybatis.pagehelper.PageHelper;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.stereotype.Service;
67

8+
import java.util.List;
9+
710
/**
811
* Created by liuzh on 2014/12/11.
912
*/
1013
@Service
11-
public class BaseService<T> {
14+
public abstract class BaseService<T> {
1215

1316
@Autowired
1417
protected Mapper<T> mapper;
1518

19+
public int save(T entity){
20+
return mapper.insert(entity);
21+
}
22+
23+
public int delete(T entity){
24+
return mapper.deleteByPrimaryKey(entity);
25+
}
26+
27+
/**
28+
* 单表分页查询
29+
*
30+
* @param pageNum
31+
* @param pageSize
32+
* @return
33+
*/
34+
public List<T> selectPage(int pageNum,int pageSize){
35+
PageHelper.startPage(pageNum, pageSize);
36+
//Spring4支持泛型注入
37+
return mapper.select(null);
38+
}
1639
}
Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,25 @@
11
package com.isea533.mybatis.service;
22

3-
import com.isea533.mybatis.mapper.CountryMapper;
4-
import com.isea533.mybatis.model.Country;
53
import com.isea533.mybatis.model.Country2;
6-
import com.isea533.mybatis.pagehelper.PageHelper;
7-
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.jdbc.core.BeanPropertyRowMapper;
9-
import org.springframework.jdbc.core.JdbcTemplate;
104
import org.springframework.stereotype.Service;
115

12-
import javax.sql.DataSource;
13-
import java.util.List;
14-
156
/**
167
* @author liuzh
178
*/
189
@Service
19-
public class DemoService extends BaseService<Country2> {
20-
@Autowired
21-
private CountryMapper countryMapper;
22-
23-
private JdbcTemplate jdbcTemplate;
24-
25-
@Autowired
26-
public void setDataSource(DataSource dataSource) {
27-
this.jdbcTemplate = new JdbcTemplate(dataSource);
28-
}
29-
30-
public Country selectById(int id) {
31-
return countryMapper.selectByPrimaryKey(id);
10+
public class DemoService extends BaseService<Country2>{
11+
12+
public int save(Country2 country2) {
13+
if (country2 == null) {
14+
throw new NullPointerException("保存的对象不能为空!");
15+
}
16+
if (country2.getCountrycode() == null || country2.getCountrycode().equals("")) {
17+
throw new RuntimeException("国家代码不能为空!");
18+
}
19+
if (country2.getCountryname() == null || country2.getCountryname().equals("")) {
20+
throw new RuntimeException("国家名称不能为空!");
21+
}
22+
return super.save(country2);
3223
}
3324

34-
public Country selectById2(int id) {
35-
Country country = jdbcTemplate.queryForObject("select * from country where id = :id", new BeanPropertyRowMapper<Country>(Country.class), id);
36-
return country;
37-
}
38-
39-
public List<Country> selectPage(int pageNum,int pageSize){
40-
PageHelper.startPage(pageNum, pageSize);
41-
return countryMapper.select(null);
42-
}
43-
44-
public List<Country2> selectPage2(int pageNum,int pageSize){
45-
PageHelper.startPage(pageNum, pageSize);
46-
//Spring4支持泛型注入
47-
return mapper.select(null);
48-
}
4925
}

src/main/resources/applicationContext.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@
3434
<property name="typeAliasesPackage" value="com.isea533.mybatis.model"/>
3535
<property name="plugins">
3636
<array>
37-
<!--<bean class="com.isea533.mybatis.mapperhelper.MapperInterceptor">
38-
<property name="properties">
39-
<value>
40-
mappers=com.isea533.mybatis.mapperhelper.Mapper
41-
</value>
42-
</property>
43-
</bean>-->
4437
<bean class="com.isea533.mybatis.pagehelper.PageHelper">
4538
<property name="properties">
4639
<value>

src/main/webapp/WEB-INF/jsp/index.jsp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@
8888
<th><spring:message code="request.url"/></th>
8989
<td>
9090
<select name="url" id="url">
91-
<option value="test1">test1 - selectByPrimaryKey</option>
92-
<option value="test2">test2 - jdbcTemplate</option>
9391
<option value="test3">test3 - Mybatis分页插件PageHelper</option>
9492
</select>
9593
</td>

0 commit comments

Comments
 (0)