基于SSM框架的封装

(RT)JavaWeb Packaging

基于SSM框架封装

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 map =  getBaseDao().queryOne(name,id).get(0);
        T t = hashMapToEntity(map);
        return t;
    }
    /**
    * 查找所有对象
    */
    @Override
    public List queryAll() {
        List ts = new ArrayList<>();
        String name = clsss.getSimpleName().toLowerCase();
        List> list =  getBaseDao().queryAll( name );
        for (HashMap hashMap : list) {
            ts.add( hashMapToEntity( hashMap ) );
        }
        return ts;
    }

    /**
    * 添加一个 对象
    */
    @Override
    public int add(T t) {
        //获取表名
        String tableName = clsss.getSimpleName().toLowerCase();
        List list= new ArrayList<>();
        //将参数放入数组中
        for (Field field : t.getClass().getDeclaredFields()) {
            field.setAccessible(true);//权限
            try {
                list.add(field.get(t));
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return getBaseDao().add( tableName , list.toArray() );
    }

    /**
    * 更新一个对象
    */
    @Override
    public void update(T t) {
        int id = 0;
        String tableName = getTableName();
        List list= new ArrayList<>();
        for (Field field : t.getClass().getDeclaredFields()) {
            field.setAccessible(true);//权限
            try {
                if ( field.get(t) == null ) {
                    continue;
                }
                if (("id").equals( field.getName()) ) { 
                    id = (Integer) field.get(t);
                    continue ;
                }
                //拼接成 :变量名='值' 的形式
                list.add( field.getName()+"="+ "'" + field.get(t) + "'" );
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        getBaseDao().update( id, tableName , list.toArray() );

    }
    /**
    * 根据id 删除 一个对象
    */
    @Override
    public void del(int id) {
        String name = clsss.getSimpleName();
        name = name.toLowerCase();
        getBaseDao().del(name, id);
    }   
}
 
  

                            
                        
                    
                    
                    

你可能感兴趣的:((RT)JavaWeb,Packaging)