栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

DBUtils增删改查代码冗余——封装为DaoUtils

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

DBUtils增删改查代码冗余——封装为DaoUtils

在Dao层中,对数据库表的增删改查存在代码冗余,可以把多余的封装为DaoUtils

public class DBUtils{
	public static final Properties PROPERTIES = new Properties();
	private static ThreadLocal threadLocal = new ThreadLocal<>();
	static{
		InputStream is = DBUtils.class.getResourceAsStream("/datebase.propert");
		PROPERTIES.load(is);
		Class.forName(PROPERTIES.getProperties("drive"));
	}
	public static Connection getConnection(){
		Connection connection = threadLocal.get();
		if(connection==null){
			connection = DriverManager.getConnecion(PROPERTIES.getProperties("url"),PROPERTIES.getProperties("user"),PROPERTIES.getProperties("password"));
			threadLocal.set(connection);
		}
	}
	public static void closeAll(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
		if(connection!=null){
			connection.close();
			threadLocal.remove();
		}
		if(preparedStatement!=null){
			preparedStatement.close();
		}
		if(resultSet!=null){
			resultSet.close();
		}		
	}
	public static void begin(){
		Connection connection = null;
		connection.getConnection();
		connection.setAutoCommit();
	}
	public static void commit(){
		Connection connection = null;
		connection.getConnection();
		connection.commit();
	}
	public static void rollback(){
		Connection connection = null;
		connection.getConnection();
		connection.rollback();
	}
}

添加RowMapper接口,给一个getRow方法

public interface RowMapper{
	public T getRow(ResultSet resultSet);
}

写个类实现

public class UserRowMapper implements RowMapper{
	@Override
	public User getRow(ResultSet resultSet){
		  User user = null;
        if(resultSet!=null){
            try {
                int id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                String address = resultSet.getString("address");
                String phone = resultSet.getString("phone");
                user = new User(id,username,password,address,phone);
                return user;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
	}
}
public class DaoUtil(){
	public int commonsUpdate(String sql,Object... args){
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		connection = DBUtils.getConnection();
		preparedStatement = connection.preparedStatement(sql);
		for(int i =0;i commonsSelect(String sql,RowMapper rowMapper,Object... args){
		 Connection connection = null;
		PreparedStatement preparedStatement = null;
		connection = DBUtils.getConnection();
		ResultSet resultSet  = null;
		preparedStatement = connection.preparedStatement(sql);
		List list = new ArrayList<>():
		if(args!=null){
			for(int i=0;i 

UserServer接口

public interface UserServer(){
	public int insert(User user);
	public int update(User user);
	public int delete(int id);
	public User select(int id);
	public List selectAll();
}

实现UserServer接口,写个UserServerimpl类

public class UserServerimpl implements UserServer{
	private DaoUtils daoUtils = new DaoUtils();
	@Override
	public int insert(User user){
		String sql = "insert into user(username,password) values(?,?);";
		Object[] args = {user.getUsername(),user.getPassword()};
		return daoUtils.commonsUpdate(sql,args);
	}
	@Override
	public int update(User user){
		String sql = "update user set username=?,password=? where id = ?;";
		Object[] args = {user.getUsername(),user.getPassword(),user.getId()};
		return daoUtils.commonsUpdate(sql,args);
	}
	@Override 
	public int delete(int id){
		String sql = "delete * from user where id = ?;";
		return daoUtils.commonsUpdate(sql.id);
	}
	@Override
	public User select(int id){
	String sql = "select * from user where id = ?;";
	List list = daoUtils.commonsQuery(sql,new UserRowMapper(),id);
	return list.get(0);
	}
	@Override
	public List selectAll(){
		String sql = "select * from user;";
		return daoUtils.commonsQuery(sql,new UserRowMapper(),null);
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785768.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号