DAO设计模式
DAO设计模式简介: DAO设计模式可以减少代码量,增强程序的可移植性,提高代码的可读性。
DAO(数据库操作对象)设计模式是 JavaEE 数据层的操作.主要由五部分组成:
1.数据库连接类:连接数据库并获取连接对象。
2.VO实体类:包含属性和表中字段完全对应的类。
3.DAO接口:提供了用户所有的操作方法(就如老师给学生提供一些学习方法)。
4.DAO实现类:实现DAO中所有的方法(就如老师给提供的方法看你如何去完成)。
5.DAO工厂类:为程序提供方法,如果要替换DAO实现类,只需要修改该Dao工厂类中的方法代码,
而不必邀修改所有的操作数据库代码(如代理人专门为需要的公司提供人才,也叫做服务层)。
下面以一个用户登录为例:
1.数据库建表:
2.数据库连接类 com.util(记得导入c3p0框架包)
package com.util; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0Util { // C3P0数据源 private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); public static DataSource getDataSource() { return dataSource; } public static Connection getConn() { Connection conne = null; try { conne = dataSource.getConnection(); } catch (Exception e) { e.printStackTrace(); } return conne; } // 关闭资源 public static void closeAll(ResultSet rs, Statement st, Connection conne) { try { if (null != rs) { rs.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (null != st) { st.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (conne != null) { conne.close(); } } catch (Exception e) { e.printStackTrace(); } } }
3.VO实体类 com.entity
package com.entity; import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = 1L; private int id; private String name; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
4.DAO接口类 com.dao
package com.dao; import com.entity.Person; public interface PersonDao { //根据对象查询 public Person findMaster(Person person); }
5.DAO实现类 com.daoImpl
package com.daoImpl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import com.dao.PersonDao; import com.entity.Person; import com.util.C3P0Util; public class PersonDaoImpl implements PersonDao { @Override public Person findMaster(Person person) { Connection conne = null; PreparedStatement pstmt = null; ResultSet rs = null; Person person2 = null; String sql = "SELECT * FROM person WHERE name = ? AND password = ?"; try { conne = C3P0Util.getConn(); // 建立了到特定数据库的连接之后,就可用该连接发送 SQL 语句。 pstmt = conne.prepareStatement(sql); pstmt.setString(1, person.getName()); pstmt.setString(2, person.getPassword()); rs = pstmt.executeQuery(); if (rs.next()) { person2 = new Person(); person2.setId(rs.getInt("id")); person2.setName(rs.getString("name")); person2.setPassword(rs.getString("password")); } } catch (Exception e) { e.printStackTrace(); } finally { C3P0Util.closeAll(rs, pstmt, conne); } return person2; } }
6.DAO工厂接口 com.service
package com.service; import com.entity.Person; public interface PersonService { public boolean login (Person person); }
7.DAO工厂实现类 com.serviceImpl
package com.serviceImpl; import com.dao.PersonDao; import com.daoImpl.PersonDaoImpl; import com.entity.Person; import com.service.PersonService; public class PersonServiceImpl implements PersonService { @Override public boolean login(Person person) { boolean islogin = false; PersonDao pd = new PersonDaoImpl(); Person ps2 = pd.findMaster(person); if (null != ps2) { islogin = true; } return islogin; } }
8.测试
package com.test; import java.util.Scanner; import org.junit.Test; import com.entity.Person; import com.service.PersonService; import com.serviceImpl.PersonServiceImpl; public class LoginTest { @Test public void loginTest() { Scanner sc = new Scanner(System.in, "UTF-8"); System.out.println("请输入用户名"); String loginName = sc.next(); System.out.println("请输入密码"); String loginPassword = sc.next(); Person person = new Person(); person.setName(loginName); person.setPassword(loginPassword); PersonService ps = new PersonServiceImpl(); boolean isLoginid = ps.login(person); if (isLoginid) { System.out.println("登录成功"); } else { System.out.println("用户名或密码错误"); } } }
运行结果如下: