DAO(Date Access Object) 模型就是写一个类,把访问数据库的代码封装起来,DAO在数据库与业务逻辑(Service)之间。
1、实体域,即操作的对象,例如我们操作的表示user表,那么就需要先写一个User类;
2、DAO模型需要先提供一个DAO接口;
3、然后再提供一个DAO接口的实现类;
4、再编写一个DAO工厂,Service通过工厂来获取DAO
一. BaseDAO类 主要连接数据库 和数据库之间进行交互
public class BaseDAO {
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql:///newss2230";
private static final String username="root";
private static final String password="123";
Connection con;
PreparedStatement ps;
ResultSet rs;
public Connection getConnection() throws Exception{
Class.forName(driver);
if(con==null||con.isClosed()){
con=DriverManager.getConnection(url, username, password);
}
/*Context context=new InitialContext();
DataSource ds=(DataSource)context.lookup("java:comp/env/news");
con=ds.getConnection();*/
return con;
}
public int executeUpdate(String sql,Object...paras) throws Exception{
getConnection();
ps= con.prepareStatement(sql);
for (int i = 0; i < paras.length; i++) {
ps.setObject(i+1, paras[i]);
}
int count = ps.executeUpdate();
return count;
}
public ResultSet executeQuery(String sql,Object...paras) throws Exception{
getConnection();
ps= con.prepareStatement(sql);
for (int i = 0; i < paras.length; i++) {
ps.setObject(i+1, paras[i]);
}
return ps.executeQuery();
}
public void closeResource(){
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(con!=null){
con.close();
}
} catch (Exception e) {
}
}
}
二.IUserInfoDAO接口(登陆接口)
public interface IUserInfoDAO {
//01.写一个登录系统的方法
public UserInfo isLogin(UserInfo info) throws Exception;
//02.写一个查询所有用户信息的方法
public List<UserInfo> findAll() throws Exception;
public boolean delete(Integer uid) throws Exception;
public boolean addUser(UserInfo info)throws Exception;
public UserInfo updateshow(Integer uid)throws Exception;
public boolean updateUser(UserInfo userinfo)throws Exception;
}
三.UserInfoDAOImpl实现类
public class UserInfoDAOImpl extends BaseDAO implements IUserInfoDAO {
@Test
public void testLogin() throws Exception{
UserInfo info=new UserInfo();
info.setUname("admin");
info.setUpwd("admin");
UserInfo result=isLogin(info);
if (result!=null) {
System.out.println("success");
}else{
System.out.println("failed");
}
}
@Override
public UserInfo isLogin(UserInfo info) throws Exception {
String sql="select * from userinfo where uname=? and upwd=?";
Object[] paras={info.getUname(),info.getUpwd()};
ResultSet rs = executeQuery(sql, paras);
UserInfo userinfo=null;
if(rs!=null){
if(rs.next()){
//如果有Data ,取出该行 ,每个列的值,赋值给单个UserInfo实例的对应属性
userinfo=new UserInfo();
userinfo.setUid(rs.getInt("uid"));
userinfo.setUname(rs.getString("uname"));
userinfo.setUpwd(rs.getString("upwd"));
}
closeResource();
}
return userinfo;
}
@Override
public List<UserInfo> findAll() throws Exception {
List<UserInfo> list=new ArrayList<UserInfo>();
String sql="select * from userinfo";
ResultSet rs = executeQuery(sql);
UserInfo userinfo=null;
if(rs!=null){
while(rs.next()){
//如果有Data ,取出该行 ,每个列的值,赋值给单个UserInfo实例的对应属性
userinfo=new UserInfo();
userinfo.setUid(rs.getInt("uid"));
userinfo.setUname(rs.getString("uname"));
userinfo.setUpwd(rs.getString("upwd"));
list.add(userinfo);
}
closeResource();
}
return list;
}
@Override
public boolean delete(Integer uid) throws Exception {
boolean flag=false;
String sql="delete from userinfo where uid=?";
int count = executeUpdate(sql, uid);
if (count>0) {
flag=true;
}
return flag;
}
@Override
public boolean addUser(UserInfo info) throws Exception {
boolean flag=false;//添加失败
String sql="insert into userinfo(uname,upwd) values(?,?)";
Object[] paras={info.getUname(),info.getUpwd()};
int count = executeUpdate(sql, paras);
if (count>0) {
flag=true;
}
return flag;
}
@Override
/**
* 根据主键获取单个对象
*/
public UserInfo updateshow(Integer uid) throws Exception {
UserInfo userinfo = new UserInfo();
String sql="select * from userinfo where uid = ?";
ResultSet rs = this.executeQuery(sql, uid);
if(rs!=null){
if(rs.next()){
userinfo.setUid(rs.getInt("uid"));
userinfo.setUname(rs.getString("uname"));
userinfo.setUpwd(rs.getString("upwd"));
}
closeResource();
}
return userinfo;
}
@Override
public boolean updateUser(UserInfo userinfo) throws Exception {
boolean flag=false;
String sql="update userinfo set uname=?,upwd=? where uid=?";
Object[] paras={userinfo.getUname(),userinfo.getUpwd(),userinfo.getUid()};
int count = executeUpdate(sql, paras);
if (count>0) {
flag=true;
}
return flag;
}
}