public static void main(String[] args) throws Exception{UserManager userManager = new UserManager();Collection<UserModel> col = userManager.getUserByDepId("0101");System.out.println(col);}public class UserManager{/*** 根據部門編號來獲取該部門下的所有人員* @param depId 部門編號* @return 該部門下的所有人員*/public Collection<UserModel> getUserByDepId(String depId)throws Exception{Collection<UserModel> col = new ArrayList<UserModel>();Connection conn = null;try{conn = this.getConnection();String sql = "select * from tbl_user u,tbl_dep d "+"where u.depId=d.depId and d.depId like ?";PreparedStatement pstmt = conn.prepareStatement(sql);pstmt.setString(1, depId+"%");ResultSet rs = pstmt.executeQuery();while(rs.next()){UserModel um = new UserModel();um.setUserId(rs.getString("userId"));um.setName(rs.getString("name"));um.setDepId(rs.getString("depId"));um.setSex(rs.getString("sex"));col.add(um);}rs.close();pstmt.close();}}
2. 代理模式簡介
public interface Subject{/*** 示意方法:一個抽象的請求方法*/public void request();}public class RealSubject implements Subject{public void request(){//執行具體的功能處理}}public class Proxy implements Subject{/*** 持有被代理的具體的目標對象*/private RealSubject realSubject=null;/*** 構造方法,傳入被代理的具體的目標對象* @param realSubject 被代理的具體的目標對象*/public Proxy(RealSubject realSubject){this.realSubject = realSubject;}public void request(){//在轉調具體的目標對象前,可以執行一些功能處理 //轉調具體的目標對象的方法realSubject.request();}}
3. 用代理模式解決使用場景
public interface UserModelApi{public String getUserId();public void setUserId(String userId);public String getName();public void setName(String name);public String getDepId();public void setDepId(String depId);public String getSex();public void setSex(String sex);}public class UserModel implements UserModelApi{ private String userId; //用戶編號 private String name; //用戶姓名 private String depId;//部門編號...
}public class Proxy implements UserModelApi{ private UserModel realSubject=null;//持有被代理的具體的目標對象 public Proxy(UserModel realSubject){//傳入被代理的具體的目標對象this.realSubject = realSubject;} private boolean loaded = false;//標示是否已經重新裝載過數據了public String getDepId(){if(!this.loaded){ //需要判斷是否已經裝載過了reload(); this.loaded = true;}return realSubject.getDepId();}//重新查詢數據庫以獲取完整的用戶數據private void reload(){try{conn = this.getConnection();String sql = "select * from tbl_user where userId=? ";//查詢所有PreparedStatement pstmt = conn.prepareStatement(sql);pstmt.setString(1, realSubject.getUserId()); ResultSet rs = pstmt.executeQuery();if(rs.next()){//只需要重新獲取除了userId和name外的數據realSubject.setDepId(rs.getString("depId"));realSubject.setSex(rs.getString("sex"));} ...}public class UserManager{public Collection<UserModelApi> getUserByDepId(String depId)throws Exception{Collection<UserModelApi> col = new ArrayList<UserModelApi>();Connection conn = null;try{conn = this.getConnection();//只需要查詢userId和name兩個值就可以了String sql = "select u.userId,u.name "+"from tbl_user u,tbl_dep d "+"where u.depId=d.depId and d.depId like ?";PreparedStatement pstmt = conn.prepareStatement(sql);pstmt.setString(1, depId+"%");ResultSet rs = pstmt.executeQuery();while(rs.next()){//這里是創建的代理對象,而不是直接創建UserModel的對象Proxy proxy = new Proxy(new UserModel());//只是設置userId和name兩個值就可以了proxy.setUserId(rs.getString("userId"));proxy.setName(rs.getString("name"));col.add(proxy);}rs.close();pstmt.close();}}