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;
}
}
23we的更多相关文章
- Mysql 用户 创建与删除(基础1)
Mysql是最流行的关系型数据库管理系统之一,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个 ...
随机推荐
- UI与数据分离 与 UI的演进
解藕的好处:UI内部模块能够灵活的变化. MVC或者三层架构着重强调了数据.业务逻辑和UI的分离. (MVC中的C只是UI和业务逻辑模块间的一个中转组件,理论上应该是个轻模块.) 以前的关注的解藕技术 ...
- Write Once, Run Anywhere:这不是Java,这是C#
注意,本文目的并非挑起语言之争.虽然有为C#平反之意,但主要还是介绍Mono并进行简单的测试. UPDATED: 25th August 2012 更新了「Compile Once, Run Anyw ...
- opencv中mat的type
type表示了矩阵中元素的类型以及矩阵的通道个数,它是一系列的预定义的常量,其命名规则为CV_(位数)+(数据类型)+(通道数),由type()返回,但是返回值是int型,不是OpenCV预定义的宏( ...
- java的IO机制
BIO.NIO.AIO -----> Block-IO : inputStream 和OutputStream,Reader和Writer 1个连接,启动一个线程,这样导致很大的线程开销 NIO ...
- ios 设计模式总结
设计模式:备注:消息传递模型(Message Passing)是Objective-C语言的核心机制.在Objective-C中,没有方法调用这种说法,只有消息传递.在C++或Java中调用某个类的方 ...
- Ubuntu16.04+GTX1080配置TensorFlow并实现图像风格转换
1. TensorFlow TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,表达了高层次的机器学习计算,大幅简化了第一代系统,并且具备更好的灵活性和可延展性. Te ...
- Juicer 轻量级javascript模板引擎
juicer是一个javascript轻量级模板引擎. 使用方法 编译模板并根据数据立即渲染出结果 1 juicer(tpl, data); 仅编译模板暂不渲染,返回一个可重用的编译后的函数 1 va ...
- LeetCode(172)Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- UVA - 1220 Party at Hali-Bula (树形DP)
有 n 个员工,n-1个从属关系. 不能同时选择某个员工和他的直接上司,问最多可以选多少人,以及选法是否唯一. 树上的最大独立集问题.只不过多了一个判断唯一性. dp[u][0]表示不选这个点的状态, ...
- 并查集:POJ1182-食物链(并查集比较高端的应用)
食物链 Time Limit: 1000MS Memory Limit: 10000K Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C ...