(RT)JavaWeb Packaging
Cral & Rooteam
www.rooteam.top
Dao Interface(table_name is name of database table):
public interface IBaseDao {
List> findOne( @Param("name") String table_name,@Param("id")int id );
List> findAll( @Param("name") String table_name);
int addOne( @Param("name")String table_name,@Param("params") Object ...params );
void updateOne ( @Param("id") int id, @Param("name") String table_name, @Param("params")Object []params );
void deleteOne(@Param("name")String table_name, @Param("id")int id);
}
Dao Implement :
<mapper namespace="top.rooteam.base.dao.IBaseDao" >
<select id="findOne" resultType="hashmap">
select * from ${name} where id=#{id}
select>
<select id="findAll" resultType="hashmap">
select * from ${name}
select>
<insert id="addOne" >
insert into ${name} values
<foreach collection="params" item="param" open="(" separator="," close=")" >
#{param}
foreach>
insert>
<update id="updateOne ">
update ${name}
<set>
<foreach collection="params" item="param" >
${param},
foreach>
set>
where id=#{id}
update>
<delete id="deleteOne ">
delete from ${name} where id=#{id}
delete >
mapper>
Service Interface
public interface IBaseService {
void delete( int id );
List findAll();
int addOne( T t );
void updateOne ( T t );
}
Service Implement
@Service
public abstract class BaseService<T> implements IBaseService<T>{
//提供一个抽象方法 当前类的子类需要提供具体实现类的 Dao
public abstract IBaseDao getBaseDao();
//提供一个抽象方法 当前类的子类需要提供 entity的 Class 对象
public abstract Class getClasss();
public Class clsss;
{
clsss = getClasss();
}
/**
* 根据 id 查找一个对象
*/
@Override
public T queryOne(int id) {
String name = clsss.getSimpleName().toLowerCase();
Map