• 动态实现类(对数据库的增删改查)


    当我们对数据库进行操作时,通常会定义一个接口类,然后定义很多的实现类来实现这个接口的所有方法,这样就会产生很多的实现类出来,增加了代码的冗余度,所以我们可以通过

    动态代理模式来动态生成实现类来代替所有的实现类,再通过一个工厂来生成实现类,这样就大大减小的代码的重复性了,增强了灵活性!

    实现步骤:

    1.数据库接口类

    package mybatis.dao;
    
    
    
    import mybatis.entity.Users;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * @Author lance
     * */
    public interface UserDaoInf {
        /**
         * 添加用户
         */
         void saveUser(Users users);
    
        /**
         * 修改用户
         */
         void updateUser(Users users);
    
        /**
         * 删除用户
         */
        void deleteUser(String uid);
    
        /**
         * 根据ID查询某条用户数据
         */
        Users findUsersById(String uid);
    
        /**
         * 查询所有的用户信息,保存在一个集合中
         */
        List<Users> findAll();
        /**
         * 统计查询
         */
        int userCount();
        /**
         * 模糊查询
         */
        List<Users> likeUsers(String name);
        /**
         * 查询所有的用户信息
         */
        List<Users> findAll2();
        /**
         * 查询单条的用户信息
         */
        Map<String ,Object> findUserMap(String uid);
        /**
         * 查询多条的用户信息
         */
        List<Map<String,Object>> findUserListMap();
    }

    2.动态代理实现类

    package mybatis.dao.proxy;
    
    
    import mybatis.tools.MyBatisUtil;
    import org.apache.ibatis.session.SqlSession;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    /**
     * 描述:
     * 动态代理实现类
     *
     * @author lance
     * @create 2018-10-15 9:33
     */
    public class MethodProxy implements InvocationHandler{
    
        private Class<?> infClass;
    
        public MethodProxy(Class<?> infClass){
            this.infClass = infClass;
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            SqlSession session = MyBatisUtil.getSqlSession();
                try{
                   Object mapper = session.getMapper(infClass);
                   Object returnValue = method.invoke(mapper,args);
                   session.commit();
                   return returnValue;
                }catch (Exception e){
                    session.rollback();
                    throw new RuntimeException(e.getMessage());
                }finally {
                    session.close();
                }
        }
    }

    3.代理工厂

    package mybatis.dao.proxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Proxy;
    
    /**
     * 描述:
     * 代理对象工厂
     * @author lance
     * @create 2018-10-15 11:42
     */
    public class MethodProxyFactory {
    
        public static <T> T createProxy(Class<T> infClass){
            //调用回调处理器
            InvocationHandler handler = new MethodProxy(infClass);
            return (T)Proxy.newProxyInstance(infClass.getClassLoader(),new Class[]{infClass},handler);
        }
    
    }

    4.测试

    import mybatis.dao.UserDaoInf;
    import mybatis.dao.proxy.MethodProxyFactory;
    import mybatis.entity.Users;
    import org.junit.Test;
    
    import java.util.List;
    
    
    /**
     * 描述:
     *
     * @author lance
     * @create 2018-09-10 15:15
     */
    public class UserDaoImplTest {
    
        @Test
        public void testFindUserById2(){
            //动态生成代理对象
            UserDaoInf dao = MethodProxyFactory.createProxy(UserDaoInf.class);
            Users user = dao.findUsersById("1001");
            System.out.println(user.getUid());
            System.out.println(user.getUserName());
    
            List<Users> list = dao.findAll();
            for (Users users : list) {
                System.out.println(users.getUid());
            }
        }
    
    }

    5.效果图

  • 相关阅读:
    bzoj3293 分金币
    考前模板整理
    CF785D Anton and School
    容斥法解决错排问题
    CF1248F Catowice City
    CF1248E Queue in the Train
    CF1244F Chips
    CF1244C The Football Season
    Noip2016Day1T2 天天爱跑步
    Noip2015Day2T3 运输计划
  • 原文地址:https://www.cnblogs.com/gepuginy/p/9800515.html
Copyright © 2020-2023  润新知