在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);
}
}



