多對多【Java實現多表操作筆記四】
阿新 • • 發佈:2021-01-04
技術標籤:Java實現多表操作其他
多對多
原則:一方存多方的集合,多方存一方的物件
但現在雙方都是多方資料,此時原則為:多方存另一方的集合
多對多的關係需要建立第三張表來實現,但中間表沒有實體類
例項:選單表和角色表模擬實現多對多關係:一個選單項對應多個角色項,一個角色項對應多個選單項
實現過程:
1、javabean
資料庫中使用外來鍵列保證兩表關係,實體類中使用屬性保證兩表關係
[src.bean.Menu.java] public class Menu { private int menuId; private String menuName; //多方存另一方的集合 private List<Role> roleList; public List<Role> getRoleList() {return roleList;} public void setRoleList(List<Role> roleList) {this.roleList = roleList;} public int getMenuId() {return menuId;} public void setMenuId(int menuId) {this.menuId = menuId;} public String getMenuName() {return menuName;} public void setMenuName(String menuName) {this.menuName = menuName;} } [src.bean.Role.java] public class Role { private int roleId; private String roleName; //多方存另一方的集合 private List<Menu> menuList; public List<Menu> getMenuList() {return menuList;} public void setMenuList(List<Menu> menuList) {this.menuList = menuList;} public int getRoleId() {return roleId;} public void setRoleId(int roleId) {this.roleId = roleId;} public String getRoleName() {return roleName;} public void setRoleName(String roleName) {this.roleName = roleName;} }
2、dao 實現對應資料關係
/*介面:[src.dao.RoleDao.java(interface)]*/ public interface RoleDao { public interface RoleDao { //查詢某個選單資訊(包含角色) public Menu findByMenuId(int mid); //查詢某個角色資訊(要求包含選單) public Role findByRoleId(int roleid); } /*實現介面:[src.dao.impl.RoleDaoImpl.java]*/ @Override public Menu findByMenuId(int mid) { Menu menu = new Menu(); ArrayList<Role> roles = new ArrayList<>(); Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { connection = getConnection(); preparedStatement = connection.prepareStatement("select * from menu m ,role r, middle where m.menuid=middle.mid and r.roleid=middle.rid and m.menuid=?"); preparedStatement.setInt(1,mid); resultSet = preparedStatement.executeQuery(); while(resultSet.next()){ //1.先各自存資料 menu.setMenuName(resultSet.getString("menuname")); Role role = new Role(); role.setRoleName(resultSet.getString("rolename")); //2.建立二者關係 roles.add(role); } menu.setRoleList(roles); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { close(connection,preparedStatement,resultSet); } return menu; } @Override public Role findByRoleId(int roleid) { Role role = new Role(); ArrayList<Menu> menuArrayList = new ArrayList<Menu>(); Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { connection = getConnection(); preparedStatement = connection.prepareStatement("select * from menu m ,role r, middle where m.menuid=middle.mid and r.roleid=middle.rid and r.roleid=?"); preparedStatement.setInt(1,roleid); resultSet = preparedStatement.executeQuery(); while(resultSet.next()){ //1.先各自存資料 Menu menu = new Menu(); menu.setMenuName(resultSet.getString("menuname")); role.setRoleName(resultSet.getString("rolename")); //2.建立二者關係 menuArrayList.add(menu); } role.setMenuList(menuArrayList); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { close(connection,preparedStatement,resultSet); } return role; }
3、Demo4.java 測試關係
public class Demo4 { public static void main(String[] args) { RoleDaoImpl roleDao = new RoleDaoImpl(); /*Menu menu = roleDao.findByMenuId(2); System.out.println(menu.getMenuName()); List<Role> roleList = menu.getRoleList(); for (Role role : roleList) { System.out.println(role.getRoleName()); }*/ Role role = roleDao.findByRoleId(2); System.out.println(role.getRoleName()); List<Menu> menuList = role.getMenuList(); for (Menu menu : menuList) { System.out.println(menu.getMenuName()); } } }