创建DAO模式的步骤
1.建立数据库epet 2.创建实体类,和相对应的数据库是对应的 3.创建Dao的基类接口类BaseDao 4.创建Dao的实现类BaseDaoImpl 5.创建具体表的Dao类
6.创建具体表的Dao实现类 7.创建业务逻辑层级的接口类PetService 8.创建业务逻辑层的接口实现类:PetServiceImpl 9.创建测试类
下面写一个登录的操作用DAO模式:
1.创建实体类:人的实体类
1 package com.beiwo.epet.entity; 2 3 import java.io.Serializable; 4 5 public class Master implements Serializable{ 6 7 private static final long serialVersionUID = -975099318646595542L; 8 9 private int id; 10 private String loginId; 11 private String password; 12 public int getId() { 13 return id; 14 } 15 public void setId(int id) { 16 this.id = id; 17 } 18 public String getLoginId() { 19 return loginId; 20 } 21 public void setLoginId(String loginId) { 22 this.loginId = loginId; 23 } 24 public String getPassword() { 25 return password; 26 } 27 public void setPassword(String password) { 28 this.password = password; 29 } 30 public static long getSerialversionuid() { 31 return serialVersionUID; 32 } 33 34 35 36 }
2.接口类的基类:
1 package com.beiwo.epet.entity; 2 3 import java.io.Serializable; 4 5 public class Master implements Serializable{ 6 7 private static final long serialVersionUID = -975099318646595542L; 8 9 private int id; 10 private String loginId; 11 private String password; 12 public int getId() { 13 return id; 14 } 15 public void setId(int id) { 16 this.id = id; 17 } 18 public String getLoginId() { 19 return loginId; 20 } 21 public void setLoginId(String loginId) { 22 this.loginId = loginId; 23 } 24 public String getPassword() { 25 return password; 26 } 27 public void setPassword(String password) { 28 this.password = password; 29 } 30 public static long getSerialversionuid() { 31 return serialVersionUID; 32 } 33 34 35 36 } 37 38 39 40 41 42 43 44 package com.beiwo.epet.dao; 45 46 import com.beiwo.epet.entity.Master; 47 48 public interface MasterDao extends BaseDao{ 49 50 /** 51 * 52 * @param loginId 用户名 53 * @param password 密码 54 * @return Master 55 */ 56 public Master findMasterByLoginIdAndPassword(String loginId,String password); 57 58 }
3.接口类的基类的实现类
1 package com.beiwo.epet.dao; 2 3 import com.beiwo.epet.entity.Master; 4 5 public interface MasterDao extends BaseDao{ 6 7 /** 8 * 9 * @param loginId 用户名 10 * @param password 密码 11 * @return Master 12 */ 13 public Master findMasterByLoginIdAndPassword(String loginId,String password); 14 15 } 16 17 18 19 20 21 package com.beiwo.epet.dao; 22 23 import com.beiwo.epet.entity.Master; 24 25 public interface MasterDao extends BaseDao{ 26 27 /** 28 * 29 * @param loginId 用户名 30 * @param password 密码 31 * @return Master 32 */ 33 public Master findMasterByLoginIdAndPassword(String loginId,String password); 34 35 }
4.创建业务逻辑层级的接口类PetService
1 package com.beiwo.epet.service; 2 3 import com.beiwo.epet.entity.Master; 4 5 public interface MasterService { 6 7 public Master login(String loginId,String password); 8 9 10 }
5.创建业务逻辑层的接口实现类
1 package com.beiwo.epet.service.impl; 2 3 import com.beiwo.epet.dao.MasterDao; 4 import com.beiwo.epet.dao.impl.MasterDaoImpl; 5 import com.beiwo.epet.entity.Master; 6 import com.beiwo.epet.service.MasterService; 7 8 public class MasterServiceImpl implements MasterService{ 9 10 @Override 11 public Master login(String loginId, String password) { 12 MasterDao masterDao=new MasterDaoImpl(); 13 return masterDao.findMasterByLoginIdAndPassword(loginId, password); 14 } 15 16 }
6.测试类:
1 package com.beiwo.epet.test; 2 3 import java.util.Scanner; 4 5 import org.junit.Test; 6 7 import com.beiwo.epet.entity.Master; 8 import com.beiwo.epet.service.MasterService; 9 import com.beiwo.epet.service.impl.MasterServiceImpl; 10 11 public class TestMasterDao { 12 13 @Test 14 public void testLogin(){ 15 MasterService masterService=new MasterServiceImpl(); 16 17 Scanner input=new Scanner(System.in); 18 19 System.out.println("请输入用户名:"); 20 String loginId=input.nextLine().trim();//去掉两端的空格 21 22 System.out.println("请输入密码:"); 23 String password=input.nextLine().trim(); 24 25 26 Master master=masterService.login(loginId, password); 27 28 if(null!=master){ 29 System.out.println("登陆成功"); 30 }else{ 31 System.out.println("登陆失败"); 32 } 33 34 35 36 } 37 38 39 }