Each service requires almost the following methods:
1, Querybyid
2, Queryall
3, Queryone
4, Querylistbywhere
5, Querypagelistbywhere
6. Save
7. Update
8, Deletebyid
9, Deletebyids
10, Deletebywhere
The following problems are encountered when encapsulating Baseservice:
So we can do this in the following way:
Package com.taotao.manage.service;
Import Java.util.Date;
Import java.util.List;
Import org.springframework.beans.factory.annotation.Autowired;
Import Com.github.abel533.entity.Example;
Import Com.github.abel533.mapper.Mapper;
Import Com.github.pagehelper.PageHelper;
Import Com.github.pagehelper.PageInfo;
Import Com.taotao.manage.pojo.BasePojo;
Public abstract class Baseservice<t extends Basepojo> {//@Autowired//private mapper<t> Mapper;
Public abstract mapper<t> getmapper (); /** * Query data by ID * * @param ID * @return */public T Querybyid (Long ID) {return this.
Getmapper (). Selectbyprimarykey (ID); /** * Query All data * * @return */public list<t> Queryall () {return This.getmappe
R (). Select (null); }/** * Query a data based on criteria, if more than one data throws an exception * * @param record * @return */public T Queryone (t Reco RD) {return this.getmapper (). SelectOne (Record); /** * Query data list by criteria * * @param record * @return */public list<t> Querylistbywhere (T record)
{return This.getmapper (). Select (record); }/** * Paging query * * @param page * @param rows * @param record * @return */Publi C pageinfo<t> querypagelistbywhere (Integer page, integer rows, T record) {//Set paging condition Pagehelper.star
Tpage (page, rows);
list<t> list = This.querylistbywhere (record);
return new pageinfo<t> (list);
}/** * Added data, returns the number of successful bars * * @param record * @return */public Integer Save (T record) {
record.setcreated (New Date ());
Record.setupdated (record.getcreated ());
Return This.getmapper (). Insert (record); }/** * New data, using non-nullable fields, returns the number of successful bars * * @param record * @return */public Integer Savesele Ctive (T record) {record.setcreated (new DAte ());
Record.setupdated (record.getcreated ());
Return This.getmapper (). Insertselective (record);
}/** * Modify data, return successful number of bars * * @param record * @return */public Integer update (T record) {
Return This.getmapper (). Updatebyprimarykey (record); }/** * Modify data, use non-null fields, return successful number of bars * * @param record * @return */public Integer Updatese
Lective (T record) {record.setupdated (New Date ());
Return This.getmapper (). Updatebyprimarykeyselective (record);
/** * Delete data by ID * * @param ID * @return */public Integer Deletebyid (Long ID) {
Return This.getmapper (). Deletebyprimarykey (ID); }/** * Bulk Delete * @param clazz * @param property * @param values * @return */Public I Nteger Deletebyids (class<t> clazz, String property, list<object> values) {Example Example = new Examp
Le (Clazz); ExampLe.createcriteria (). Andin (property, values);
Return This.getmapper (). Deletebyexample (example);
}/** * Delete according to conditions * * @param record * @return */public Integer deletebywhere (T record) {
Return This.getmapper (). Delete (record);
}
}
Retrofit Itemcatservice with Baseservice
Package com.taotao.manage.service;
Import java.util.List;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;
Import Com.github.abel533.mapper.Mapper;
Import Com.taotao.manage.mapper.ItemCatMapper;
Import Com.taotao.manage.pojo.ItemCat;
@Service public
class Itemcatservice extends baseservice<itemcat>{
@Autowired
Private Itemcatmapper Itemcatmapper;
Public list<itemcat> Queryitemcat (Long parentid) {
// itemcat record = new Itemcat (); Record.setparentid (parentid); return This.itemCatMapper.select (record); }
@Override public
mapper<itemcat> getmapper () {
//TODO auto-generated method stub
return this.itemcatmapper;
}
}
In controller:
Test
generic injection of Spring4
optimization of Baseservice
Test:
Test: Runtime injects the implementation class of a specific generic mapper sub-interface:
problem
Itemcatmapper is not used when encoding, whether to delete it. Can't
Cause: This object is used by the spring runtime to inject it into the baseservice.