前面已经介绍了如何使用通用Mapper(点这里),
这里写一下Spring中使用Mapper的方案,这也是项目中的最优方案;
由于spring4增加了对泛型注入的支持,这个特性对于Mybatis的通用Mapper来说,非常的有用,可以直接在service中写Mapper<T>
,通过BaseService<T>
来实现通用的Service;
Spring 抽象出Service方法– BaseService
public abstract class BaseService<T> {
// public abstract Mapper<T> getMapper();
//注入Mapper<T>
@Autowired
private Mapper<T> mapper;
/**
* 根据id查询数据
*
* @param id
* @return
*/
public T queryById(Long id) {
return mapper.selectByPrimaryKey(id);
}
/**
* 查询所有数据
*
* @return
*/
public List<T> queryAll() {
return mapper.select(null);
}
/**
* 根据条件查询一条数据,如果有多条数据会抛出异常
*
* @param record
* @return
*/
public T queryOne(T record) {
return mapper.selectOne(record);
}
/**
* 根据条件查询数据列表
*
* @param record
* @return
*/
public List<T> queryListByWhere(T record) {
return mapper.select(record);
}
/**
* 分页查询
*
* @param page
* @param rows
* @param record
* @return
*/
public PageInfo<T> queryPageListByWhere(Integer page, Integer rows, T record) {
// 设置分页条件
PageHelper.startPage(page, rows);
List<T> list = this.queryListByWhere(record);
return new PageInfo<T>(list);
}
/**
* 新增数据,返回成功的条数
*
* @param record
* @return
*/
public Integer save(T record) {
record.setCreated(new Date());
record.setUpdated(record.getCreated());
return mapper.insert(record);
}
/**
* 新增数据,使用不为null的字段,返回成功的条数
*
* @param record
* @return
*/
public Integer saveSelective(T record) {
record.setCreated(new Date());
record.setUpdated(record.getCreated());
return mapper.insertSelective(record);
}
/**
* 修改数据,返回成功的条数
*
* @param record
* @return
*/
public Integer update(T record) {
return mapper.updateByPrimaryKey(record);
}
/**
* 修改数据,使用不为null的字段,返回成功的条数
*
* @param record
* @return
*/
public Integer updateSelective(T record) {
record.setUpdated(new Date());
return mapper.updateByPrimaryKeySelective(record);
}
/**
* 根据id删除数据
*
* @param id
* @return
*/
public Integer deleteById(Long id) {
record.setUpdated(new Date());
return mapper.deleteByPrimaryKey(id);
}
/**
* 批量删除
* @param clazz
* @param property
* @param values
* @return
*/
public Integer deleteByIds(Class<T> clazz, String property, List<Object> values) {
Example example = new Example(clazz);
example.createCriteria().andIn(property, values);
return mapper.deleteByExample(example);
}
/**
* 根据条件做删除
*
* @param record
* @return
*/
public Integer deleteByWhere(T record) {
return mapper.delete(record);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
用ItemCartService继承BaseService来实现业务需求:
@Service
public class ItemCartService extends BaseService<ItemCart>{
//这里可以没有任何业务代码,当然也可以根据业务需求,写出相应的代码
}
- 1
- 2
- 3
- 4
- 5
接下来看如何在Controller中调用Service:
@Autowired
ItemCartService itemCartService;
@RequestMapping(method=RequestMethod.GET)
public ResponseEntity<List<ItemCart>> queryItemCart(@RequestParam(value="id" ,defaultValue="0")
Long parentId) {
try{
//构造查询条件
ItemCart record=new ItemCart();
record.setParentId(parentId);
List<ItemCart> itemCarts=itemCartService.queryListByWhere(record);
if(itemCarts == null || itemCarts.isEmpty()){
return ResponseEntity.status(HttpStatus.Not_FOUND).body(null);
}
return ResponseEntity.ok(itemCarts);
}catch(Exception e){
e.printStackTrace();
}
return ResponseEntity.staus(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
参考: