创建DAO模式的步骤

时间:2021-01-18 05:34:26

1.建立数据库epet 2.创建实体类,和相对应的数据库是对应的 3.创建Dao的基类接口类BaseDao 4.创建Dao的实现类BaseDaoImpl 5.创建具体表的Dao类

6.创建具体表的Dao实现类 7.创建业务逻辑层级的接口类PetService 8.创建业务逻辑层的接口实现类:PetServiceImpl  9.创建测试类

创建DAO模式的步骤

下面写一个登录的操作用DAO模式:

创建DAO模式的步骤

1.创建实体类:人的实体类

 package com.beiwo.epet.entity;

 import java.io.Serializable;

 public class Master implements Serializable{

     private static final long serialVersionUID = -975099318646595542L;

     private int id;
private String loginId;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static long getSerialversionuid() {
return serialVersionUID;
} }

2.接口类的基类:

 package com.beiwo.epet.entity;

 import java.io.Serializable;

 public class Master implements Serializable{

     private static final long serialVersionUID = -975099318646595542L;

     private int id;
private String loginId;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static long getSerialversionuid() {
return serialVersionUID;
} } package com.beiwo.epet.dao; import com.beiwo.epet.entity.Master; public interface MasterDao extends BaseDao{ /**
*
* @param loginId 用户名
* @param password 密码
* @return Master
*/
public Master findMasterByLoginIdAndPassword(String loginId,String password); }

3.接口类的基类的实现类

 package com.beiwo.epet.dao;

 import com.beiwo.epet.entity.Master;

 public interface MasterDao extends BaseDao{

     /**
*
* @param loginId 用户名
* @param password 密码
* @return Master
*/
public Master findMasterByLoginIdAndPassword(String loginId,String password); } package com.beiwo.epet.dao; import com.beiwo.epet.entity.Master; public interface MasterDao extends BaseDao{ /**
*
* @param loginId 用户名
* @param password 密码
* @return Master
*/
public Master findMasterByLoginIdAndPassword(String loginId,String password); }

4.创建业务逻辑层级的接口类PetService

 package com.beiwo.epet.service;

 import com.beiwo.epet.entity.Master;

 public interface MasterService {

     public Master login(String loginId,String password);

 }

5.创建业务逻辑层的接口实现类

 package com.beiwo.epet.service.impl;

 import com.beiwo.epet.dao.MasterDao;
import com.beiwo.epet.dao.impl.MasterDaoImpl;
import com.beiwo.epet.entity.Master;
import com.beiwo.epet.service.MasterService; public class MasterServiceImpl implements MasterService{ @Override
public Master login(String loginId, String password) {
MasterDao masterDao=new MasterDaoImpl();
return masterDao.findMasterByLoginIdAndPassword(loginId, password);
} }

6.测试类:

 package com.beiwo.epet.test;

 import java.util.Scanner;

 import org.junit.Test;

 import com.beiwo.epet.entity.Master;
import com.beiwo.epet.service.MasterService;
import com.beiwo.epet.service.impl.MasterServiceImpl; public class TestMasterDao { @Test
public void testLogin(){
MasterService masterService=new MasterServiceImpl(); Scanner input=new Scanner(System.in); System.out.println("请输入用户名:");
String loginId=input.nextLine().trim();//去掉两端的空格 System.out.println("请输入密码:");
String password=input.nextLine().trim(); Master master=masterService.login(loginId, password); if(null!=master){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
} } }