simple-dev-2.0.2.jar包里有两个实现类不是很懂
问题:登陆模块里,不明白HuiyuanMapper里继承的SimpleMapper这个类的作用是什么以及HuiyuanService继承的SimpleBizservice这个类的作用是什么?我们一般不是serviceimpl继承service,然后mapper不是一个数据访问类吗?这里的操作我有点看不懂,请大佬帮我看看1.huiyuanservice
@Service("huiyuanService")
public class HuiyuanService extends SimpleBizservice<HuiyuanMapper> {
@Autowired
private HuiyuanMapper huiyuanMapper;
public List<GuanzhuVo> getStars(int id){
return huiyuanMapper.getStars(id);
}
public intdeduct(inthyId,double fee){
if(hyId<=0)
return -1;
Huiyuan huiyuan=load("where id="+hyId);
if(huiyuan==null)
return -2;
if(huiyuan.getYue()<fee)
return -3;
int count=this.executeUpdate(MessageFormat.format(" update huiyuan set yue=yue-{0,number,#} where id={1,number,#} ",fee,hyId));
return count;
}
2.HuiyuanMapper
package com.daowen.mapper;
import com.daowen.entity.Huiyuan;
import com.daowen.ssm.simplecrud.SimpleMapper;
import com.daowen.vo.GuanzhuVo;
import java.util.HashMap;
import java.util.List;
public interface HuiyuanMapper extends SimpleMapper<Huiyuan> {
List<GuanzhuVo> getStars(int id);//
List<Huiyuan> getEntityPlus(HashMap map);
Huiyuan loadPlus(HashMap map);
}
3.SimpleMapper<>
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.daowen.ssm.simplecrud;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
public interface SimpleMapper<T> {
@SelectProvider(
type = SqlBuilderDelegate.class,
method = "buildGetAllSql"
)
List<T> getAllEntity();
@SelectProvider(
type = SqlBuilderDelegate.class,
method = "buildGetListSql"
)
List<T> getEntitys(@Param("where") String var1);
@SelectProvider(
type = SqlBuilderDelegate.class,
method = "buildGetTopEntitySql"
)
List<T> getTopEntity(@Param("where") String var1, @Param("topCount") int var2);
@SelectProvider(
type = SqlBuilderDelegate.class,
method = "buildGetPageListSql"
)
List<T> getPageEntitys(@Param("where") String var1, @Param("pageIndex") int var2, @Param("pageSize") int var3);
@SelectProvider(
type = SqlBuilderDelegate.class,
method = "buildGetRecordCountSql"
)
int getRecordCount(@Param("where") String var1);
@SelectProvider(
type = SqlBuilderDelegate.class,
method = "buildSelectOneSql"
)
T selectOne(@Param("id") long var1);
@SelectProvider(
type = SqlBuilderDelegate.class,
method = "buildLoadSql"
)
T load(@Param("where") String var1);
@InsertProvider(
type = SqlBuilderDelegate.class,
method = "buildSaveSql"
)
@Options(
useGeneratedKeys = true,
keyColumn = "id"
)
int save(T var1);
@UpdateProvider(
type = SqlBuilderDelegate.class,
method = "buildUpdateSql"
)
int update(T var1);
@DeleteProvider(
type = SqlBuilderDelegate.class,
method = "buildDeleteSql"
)
int delete(String var1);
@Select({"${sql}"})
List<T> query(@Param("sql") String var1);
@Update({"${sql}"})
int nativeUpdate(@Param("sql") String var1);
@Select({"${sql}"})
List<HashMap<String, Object>> queryToMap(@Param("sql") String var1);
}
4.SimpleBizservice
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.daowen.ssm.simplecrud;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
public class SimpleBizservice<MapperClass extends SimpleMapper> implements Bizservice<MapperClass> {
@Autowired
private MapperClass mapper = null;
public SimpleBizservice() {
}
public MapperClass getMapper() {
return this.mapper;
}
public List getEntity() {
return this.mapper.getAllEntity();
}
public <T> T save(T t) {
Class<?>[] funIntf = this.mapper.getClass().getInterfaces();
Class<?> mapperClazz = null;
if (funIntf != null && funIntf.length > 0) {
mapperClazz = funIntf;
}
Class<?> clz = this.getEntityClass(mapperClazz);
if (clz != null && !clz.isInstance(t)) {
System.out.println("不是实体类");
return null;
} else {
this.mapper.save(t);
return t;
}
}
public List getPageEntitys(String where, int pageIndex, int pageSize) {
return this.mapper.getPageEntitys(where, pageIndex, pageSize);
}
public int getRecordCount(String where) {
return this.mapper.getRecordCount(where);
}
public List getEntity(String where) {
return this.mapper.getEntitys(where);
}
public <T> List<T> getTopEntity(String where, int topCount) {
return this.mapper.getTopEntity(where, topCount);
}
private Class<?> getEntityClass(Class<?> mapperClass) {
Type[] types = mapperClass.getGenericInterfaces();
Class<?> entityClass = null;
Type[] var4 = types;
int var5 = types.length;
for(int var6 = 0; var6 < var5; ++var6) {
Type type = var4;
if (type instanceof ParameterizedType) {
ParameterizedType t = (ParameterizedType)type;
if (t.getRawType() == SimpleMapper.class) {
entityClass = (Class)t.getActualTypeArguments();
break;
}
}
}
return entityClass;
}
public <T> int update(T t) {
return this.mapper.update(t);
}
public int delete(String where) {
return this.mapper.delete(where);
}
public Boolean isExist(String where) {
return this.getRecordCount(where) > 0;
}
public <T> T load(int id) {
Class<?>[] funIntf = this.mapper.getClass().getInterfaces();
Class<?> mapperClazz = null;
if (funIntf != null && funIntf.length > 0) {
mapperClazz = funIntf;
}
Class<?> entityClzz = this.getEntityClass(mapperClazz);
TableMetadata tm = TableMetadata.forClass(entityClzz);
String where = MessageFormat.format("where{0}={1}", tm.getPkColumn(), id);
List list = this.getEntity(where);
return list == null ? null : list.get(0);
}
public <T> T load(String where) {
List<T> list = this.mapper.getEntitys(where);
return list != null && list.size() != 0 ? list.get(0) : null;
}
public <T> List<T> query(String nativateSql) {
return this.mapper.query(nativateSql);
}
public List<HashMap<String, Object>> queryToMap(String nativateSql) {
return this.mapper.queryToMap(nativateSql);
}
public int executeUpdate(String sql) {
return this.mapper.nativeUpdate(sql);
}
}
通用mapper 可以参考mybatis-plus
页:
[1]