我们观察一个表单,可以想象出每一个表单就像一个类一样,列名对应着属性,每一条数据都可以看成一个具体的对象。所以,我们可以根据这个表单来定义属于它的一个类。而针对这个类,我们可以创建出对象来表示一条数据,然后通过修改这个对象的属性来对这个表单进行增删改查。而针对这个思想,JAVA中出现了这样的一种开发模式:DAO模式
DAO模式
概念
DAO DataBase Access Object 数据库访问对象。 把访问数据库的工作写在一个类中。 访问数据库事,调用这个类中的方法去完成。
设计思路
对数据库中的每张表的CRUD操作写到一个类中。
比如:对student表的操作 创建一个StudentDao
表 接口 实现类
student StudentDao StudentDaoImpl
实体类
在文章开头我们提出把一个表单来变成一个类,很多开发者把这种类叫做实体类。
实体类一般都会归属为javabean类(咖啡豆),然后会统一放入一个包中,包名一般会起为bean或eneity(实体)
一般什么样的类会叫做JavaBean?
- 1.有私有的属性
- 2.针对私有属性提供setter和getter方法
- 3.显式定义无参构造
- 4.实现Serializable接口 (序列化)
明白了上面的概念,我们来写代码:
接口
import java.util.List;
import com.yht.bean.Emp;
public interface EmpDao {
public int insertEmp(Emp emp);
public int deleteEmpById(Emp emp);
public int updateEmp(Emp emp1,Emp emp2);
public List<Emp> findAllEmp();
}
实现类
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.yht.bean.Emp;
import com.yht.dao.EmpDao;
import com.yht.utils.JdbcUtil;
public class EmpDaoImpl implements EmpDao {
@Override
public int insertEmp(Emp emp) {
int result = 0;
Connection conn = null;
PreparedStatement ps = null;
try{
conn = JdbcUtil.getConnection();
String sql = "insert into emp values (null,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setObject(1, emp.getName());
ps.setObject(2, emp.getGender());
ps.setObject(3, emp.getSalary());
ps.setObject(4, emp.getJoinDate());
result = ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally {
JdbcUtil.closeAll(ps, conn, null);
}
return result;
}
@Override
public int deleteEmpById(Emp emp) {
int result = 0;
Connection conn = null;
PreparedStatement ps = null;
try{
conn = JdbcUtil.getConnection();
String sql = "delete from emp where id = ?";
ps = conn.prepareStatement(sql);
ps.setObject(1, emp.getId());
result = ps.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
JdbcUtil.closeAll(ps, conn, null);
}
return result;
}
@Override
public int updateEmp(Emp emp1,Emp emp2) {
int result = 0;
Connection conn = null;
PreparedStatement ps = null;
try{
conn = JdbcUtil.getConnection();
String sql = "update emp set name = ? where name = ? ";
ps = conn.prepareStatement(sql);
ps.setObject(1, emp2.getName());
ps.setObject(2, emp1.getName());
result = ps.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
JdbcUtil.closeAll(ps, conn, null);
}
return result;
}
@Override
public List<Emp> findAllEmp() {
List<Emp> list = new ArrayList<>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
String sql = "select * from emp";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Emp e = new Emp();
e.setId(rs.getInt("id"));
e.setName(rs.getString("name"));
e.setGender(rs.getString("gender"));
e.setSalary(rs.getDouble("salary"));
e.setJoinDate(rs.getString("join_date"));
list.add(e);
}
}catch(Exception e){
e.printStackTrace();
}finally {
JdbcUtil.closeAll(ps, conn, rs);
}
return list;
}
}
实体类
import java.util.Date;
public class Emp {
private int id;
private String name;
private String gender;
private double salary;
private String joinDate;
public Emp(int id, String name, String gender, double salary, String joinDate) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.salary = salary;
this.joinDate = joinDate;
}
public Emp() {
super();
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getJoinDate() {
return joinDate;
}
public void setJoinDate(String joinDate) {
this.joinDate = joinDate;
}
}
测试类
import java.util.List;
import java.util.Scanner;
import com.yht.bean.Emp;
import com.yht.dao.EmpDao;
import com.yht.dao.impl.EmpDaoImpl;
public class EmpTest {
public static void main(String[] args) {
//test1();
//test2();
//test3();
test4();
}
private static void test4() {
EmpDao dao = new EmpDaoImpl();
List<Emp> list = dao.findAllEmp();
for(Emp e:list){
System.out.println(e.getId()+"\t"+e.getName()+"\t"+e.getGender()+"\t"+e.getSalary()+"\t"+e.getJoinDate());
}
}
private static void test3() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的姓名");
String oldname = sc.nextLine();
System.out.println("请输入修改后的姓名");
String newname = sc.nextLine();
Emp emp1 = new Emp();
emp1.setName(oldname);
Emp emp2 = new Emp();
emp2.setName(newname);
EmpDao dao = new EmpDaoImpl();
int result = dao.updateEmp(emp1, emp2);
if(result!=0)
System.out.println("修改成功");
else
System.out.println("修改失败");
}
private static void test2() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的编号");
String id = sc.nextLine();
Emp emp = new Emp();
emp.setId(Integer.parseInt(id));
EmpDao dao = new EmpDaoImpl();
int result = dao.deleteEmpById(emp);
if(result!=0)
System.out.println("删除成功");
else
System.out.println("删除失败");
}
private static void test1() {
Scanner sc = new Scanner(System.in);
System.out.println("输入你的名字");
String name = sc.nextLine();
System.out.println("请输入你的性别");
String gender = sc.nextLine();
System.out.println("请输入你的工资");
String salary = sc.nextLine();
System.out.println("请输入你的入职时间");
String joinDate = sc.nextLine();
Emp emp = new Emp();
emp.setName(name);
emp.setGender(gender);
emp.setSalary(Double.parseDouble(salary));
emp.setJoinDate(joinDate);
EmpDao dao = new EmpDaoImpl();
int result = dao.insertEmp(emp);
if(result != 0)
System.out.println("插入成功");
else
System.out.println("插入失败");
}
}