承接上文:JavaWeb课程复习资料(五)——配置Tomcat
目录
1、DAO层
2、DAOImpl层
1、DAO层
DAO层是接口层,故而需要使用【interface】修饰
源码如下:
代码语言:javascript复制package com.item.dao;
import com.item.pojo.UserInfo;
import java.util.List;
public interface UserInfoDAO {
/**
* 查询所有用户信息
* @return
*/
public List<UserInfo> GetAll();
/**
* 添加用户
* @param info
* @return
*/
public boolean InsertUserInfo(UserInfo info);
/**
* 单个ID查询
* @param id
* @return
*/
public UserInfo SelectById(int id);
/**
* 修改用户信息
* @param info
* @return
*/
public boolean UpdateById(UserInfo info);
/**
* 根据id删除用户
* @param id
* @return
*/
public boolean DeleteById(int id);
}
2、DAOImpl层
DAOImpl属于实现层,故而通过【】实现DAO层对应接口即可
编码过程中可以看到注释也是很完整的。
源码如下:
代码语言:javascript复制package com.item.daoimpl;
import com.item.dao.UserInfoDAO;
import com.item.jdbc.FactoryDB;
import com.item.pojo.UserInfo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
public class UserInfoDAOImpl implements UserInfoDAO {
Connection conn = null;
PreparedStatement pre = null;
ResultSet res = null;
@Override
public List<UserInfo> GetAll() {
try {
conn = FactoryDB.GetConnection();
pre = conn.prepareStatement("select * from userinfo");
res = pre.executeQuery();
List<UserInfo> list = new LinkedList<UserInfo>();
while (res.next()) {
list.add(new UserInfo(
res.getInt(1),
res.getDate(2),
res.getString(3),
res.getString(4)
));
}
FactoryDB.Close(conn, pre, res);
return list;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public boolean InsertUserInfo(UserInfo info) {
try {
conn = FactoryDB.GetConnection();
pre = conn.prepareStatement("INSERT INTO userinfo VALUES (0,NOW(),?,?);");
pre.setString(1, info.getUserName());
pre.setString(2, info.getIntroduce());
int isf = pre.executeUpdate();
FactoryDB.Close(conn, pre, res);
return isf != -1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
@Override
public UserInfo SelectById(int id) {
try {
conn = FactoryDB.GetConnection();
pre = conn.prepareStatement("select * from userinfo where id=?");
pre.setInt(1, id);
res = pre.executeQuery();
//判断查询结果是否为空
if (res.wasNull()) {
return null;
}
UserInfo u=null;
while (res.next()) {
u = new UserInfo(res.getInt(1), res.getDate(2), res.getString(3), res.getString(4));
}
FactoryDB.Close(conn, pre, res);
return u;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public boolean UpdateById(UserInfo info) {
try {
conn = FactoryDB.GetConnection();
pre = conn.prepareStatement("update userinfo set userName=?,introduce=? where id=?");
pre.setString(1, info.getUserName());
pre.setString(2, info.getIntroduce());
pre.setInt(3, info.getId());
int isf = pre.executeUpdate();
FactoryDB.Close(conn, pre, res);
return isf != -1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean DeleteById(int id) {
try {
conn = FactoryDB.GetConnection();
pre = conn.prepareStatement("delete from test.userinfo where id=?");
pre.setInt(1, id);
int isf = pre.executeUpdate();
FactoryDB.Close(conn, pre, res);
return isf != -1;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
到此DAO与DAOImpl层封装完毕。