• 23we


    package com.test.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import org.apache.tomcat.jdbc.pool.DataSource;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    
    public abstract class AbstractBaseDataSourceDao {
    	private static final Logger LOGGER=LoggerFactory.getLogger("AbstractBaseDataSourceDao");
    	protected Connection conn=null;
    	protected PreparedStatement ps=null;
    	protected ResultSet rst=null;
    	@Autowired
    	protected DataSource dataSource;
        /** 
         * 设置dataSource 
         */  
        public void setDataSource(DataSource dataSource) {  
            this.dataSource = dataSource;  
            try {
    			conn=dataSource.getConnection();
    		} catch (SQLException e) {
    			LOGGER.error("数据库连接异常:", e.getMessage());
    		}
        }  
    }
    
    package com.test.dao;
    
    import java.util.List;
    
    import org.springframework.transaction.annotation.Transactional;
    
    import com.test.model.User;
    
    public interface IUserDao {
    	@Transactional
    	public boolean add(User user);
    	public boolean add(List<User> lists);
    	public boolean update(User user);
    	public boolean delete(int id);
    	public void delete(List<Integer> lists);
    	public User findById(int id);
    	public User findByName(String name);
    	public List<User> findAllUser();
    }
    
    package com.test.dao.imp;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    import org.apache.tomcat.jdbc.pool.DataSource;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.dao.DataAccessException;
    import org.springframework.data.redis.connection.RedisConnection;
    import org.springframework.data.redis.core.RedisCallback;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import com.test.dao.AbstractBaseDataSourceDao;
    import com.test.dao.IUserDao;
    import com.test.model.User;
    
    public class UserDao extends AbstractBaseDataSourceDao implements IUserDao {
    	private static final Logger LOGGER = LoggerFactory.getLogger("UserDao");
    
    	public int executeUpdate(String sql, Object[] args) {
    		try {
    			ps = conn.prepareStatement(sql);
    			if (args != null && args.length > 0) {
    				for (int i = 0; i < args.length; i++) {
    					ps.setObject(i + 1, args[i]);
    				}
    			}
    			int rows = ps.executeUpdate();
    			if (rows > 0) {
    				conn.commit();
    				return rows;
    			}
    		} catch (SQLException e) {
    			LOGGER.error("执行增删改sql异常:", e.getMessage());
    		}
    		return 0;
    	}
    
    	public ResultSet executeQuery(String sql, Object[] args) {
    		try {
    			ps = conn.prepareStatement(sql);
    			if (args != null && args.length > 0) {
    				for (int i = 0; i < args.length; i++) {
    					ps.setObject(i + 1, args[i]);
    				}
    			}
    			rst = ps.executeQuery();
    		} catch (SQLException e) {
    			LOGGER.error("执行查询sql异常:", e.getMessage());
    		}
    		return rst;
    	}
    
    	public void closeAll(Connection conn, PreparedStatement ps, ResultSet rst) {
    		try {
    			if (rst != null) {
    				rst.close();
    			}
    			if (ps != null) {
    				ps.close();
    			}
    			if (conn != null) {
    				conn.close();
    			}
    		} catch (SQLException e) {
    			LOGGER.error("关闭数据库连接异常:", e.getMessage());
    		}
    	}
    
    	@Override
    	public boolean add(final User user) {
    		String sql = "insert into users(uname, upasswd) values(?,?);";
    		String[] args = new String[] { user.getUname(), user.getUpasswd() };
    		int row = executeUpdate(sql, args);
    		if (row <= 0) {
    			LOGGER.error("数据行不存在, 用户 = " + user.getUname());
    			return false;
    		} else {
    			return true;
    		}
    
    	}
    
    	@Override
    	public boolean add(final List<User> lists) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	@Override
    	public boolean update(final User user) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	@Override
    	public boolean delete(int id) {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	@Override
    	public void delete(final List<Integer> lists) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public User findById(int id) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	public User findByName(final String name) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	public List<User> findAllUser() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    }
    
    package RedisTest;
    import java.util.ArrayList;  
    import java.util.List;  
    import junit.framework.Assert;  
    import org.junit.Test;  
    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.test.context.ContextConfiguration;  
    import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;  
    import com.test.dao.IUserDao;  
    import com.test.model.User;
      
    /**  
     * 测试 
     */    
    @ContextConfiguration(locations = {"classpath:config/applicationContext.xml"})  
    public class RedisTest extends AbstractJUnit4SpringContextTests {  
          
        @Autowired  
        private IUserDao userDao;
          
        /** 
         * 新增 
         */  
        @Test  
        public void testAddUser() {  
            User user = new User();  
            user.setUname("user1");
            user.setUpasswd("java2000_wl1");  
            boolean result = userDao.add(user);  
            Assert.assertTrue(result);  
        }  
         
        /** 
         * 批量新增 普通方式 
         */  
        @Test  
        public void testAddUsers1() {  
            List<User> list = new ArrayList<User>();  
            for (int i = 10; i < 50000; i++) {  
                User user = new User();  
                user.setUname("user"+ i);
                user.setUpasswd("java2000_wl"+ i);  
                list.add(user);  
            }  
            long begin = System.currentTimeMillis();  
            for (User user : list) {  
                userDao.add(user);  
            }  
            System.out.println(System.currentTimeMillis() -  begin);  
        }  
          
        /** 
         * 批量新增 pipeline方式 
         */  
        @Test  
        public void testAddUsers2() {  
            List<User> list = new ArrayList<User>();  
            for (int i = 50000; i < 100000; i++) {  
                User user = new User();  
                user.setUname("user"+ i);
                user.setUpasswd("java2000_wl"+ i);  
                list.add(user);  
            }  
            long begin = System.currentTimeMillis();  
            boolean result = userDao.add(list);  
            System.out.println(System.currentTimeMillis() - begin);  
            Assert.assertTrue(result);  
        }  
           
        /** 
         * 修改 
         */  
        @Test  
        public void testUpdate() {  
            User user = new User();  
            user.setUname("user1");
            user.setUpasswd("new_password"); 
            boolean result = userDao.update(user);  
            Assert.assertTrue(result);  
        }  
        /** 
         * 获取 
         */  
        @Test  
        public void testGetUserId() {  
        	int id = 4;  
        	User user = userDao.findById(id);  
        	Assert.assertNotNull(user);  
        	Assert.assertEquals(user.getUname(), "java2000_wl1");  
        }       
        /** 
         * 获取 
         */  
        @Test  
        public void testGetUserName() {  
            String id = "user1";  
            User user = userDao.findByName(id);  
            Assert.assertNotNull(user);  
            Assert.assertEquals(user.getUname(), "java2000_wl1");  
        }       
        /** 
         * 通过key删除单个 
         */  
        @Test  
        public void testDelete() {  
            int key = 4;  
            userDao.delete(key);  
        }  
          
        /** 
         * 批量删除 
         */  
        @Test  
        public void testDeletes() {  
            List<Integer> list = new ArrayList<Integer>();  
            for (int i = 0; i < 100000; i++) {  
                list.add(i);  
            }  
            userDao.delete(list);  
        }  
    
        /** 
         * 设置userDao 
         * @param userDao the userDao to set 
         */  
        public void setUserDao(IUserDao userDao) {  
            this.userDao = userDao;  
        }  
    }  
    
  • 相关阅读:
    爬虫的基础知识(贰)
    Django内置标签
    WPF ToggleButton Style
    编写一个简单的COM组件
    C# 调用DOS 命令
    【转】 C#中检查网络是否连通的二种方法
    c++中的类型转换
    COM笔记-包容与聚合
    windows笔记-在可执行文件或DLL的多个实例之间共享静态数据
    编程之路
  • 原文地址:https://www.cnblogs.com/asoks/p/5343495.html
Copyright © 2020-2023  润新知