写点什么

SpringDataJPA 之关联关系

  • 2022 年 5 月 16 日
  • 本文字数:2380 字

    阅读完需:约 8 分钟

Users users = this.usersDao.findOne(13);


System.out.println("用户信息:"+users);


Roles roles = users.getRoles();


System.out.println(roles);


}


}


[](()2.一对多




从角色到用户的一对多的关联关系


角色:一方


用户:多方

[](()pojo

@Entity


@Table(name="t_users")


public class Users implements Serializable{


@Id


//strategy=GenerationType.IDENTITY 自增长


@GeneratedValue(strategy=GenerationType.IDENTITY)


@Column(name="userid")


private Integer userid;


@Column(name="username")


private String username;


@Column(name="userage")


private Integer userage;


@ManyToOne()


@JoinColumn(name="roles_id")


private Roles roles;


public Integer getUserid() {


return userid;


}


public void setUserid(Integer userid) {


this.userid = userid;


}


public String getUsername() {


return username;


}


public void setUsername(String username) {


this.username = username;


}


public Integer getUserage() {


return userage;


}


public void setUserage(Integer userage) {


this.userage = userage;


}


@Override


public String toString() {


return "Users [userid=" + userid + ", username=" + username + ",userage=" + userage + "]";


}


}


@Entity


@Table(name="t_roles")


public class Roles {


@Id


@GeneratedValue(strategy=GenerationType.IDENTITY)


@Column(name="roleid")


private Integer roleid;


@Column(name="rolename")


private String rolename;


@OneToMany(mappedBy="roles")


private Set<Users> users = new HashSet<>();


public Integer getRoleid() {


return roleid;


}


public void setRoleid(Integer roleid) {


this.roleid = roleid;


}


public String getRolename() {


return rolename;


}


public void setRolename(String rolename) {


this.rolename = rolename;


}


@Override


public String toString() {


return "Roles [roleid=" + roleid + ", rolename=" + rolename + "]";


}


}

[](()操作

/**


  • 一对多的关联关系测试

  • @author Administrator


*/


@RunWith(SpringJUnit4ClassRunner.class)


@ContextConfiguration("classpath:applicationContext.xml")


public class OneToManyTest {


@Autowired


private UsersDao usersDao;


/**


  • 添加用户同时添加角色


*/


@Test


public void test1(){


//创建角色


Roles roles = new Roles();


roles.setRolename("管理员");


//创建用户


Users users =new Users();


users.setUserage(30);


users.setUsername("小王");


//建立关系


roles.getUsers().add(users);


users.setRoles(roles);


//保存数据


this.usersDao.save(users);


}


/**


  • 根据用户 ID 查询用户信息,同时查询角色


*/


@Test


public void test2(){


Users users = this.usersDao.findOne(14);


System.out.println("用户姓名:"+users.getUsername());


Roles roles = users.getRoles();


System.out.println(roles);


}


}


[](()3.多对多




一个角色可以拥有多个菜单,一个菜单可以分配多个角色。多对多的关联关系


角色:多方


菜单:多方

[](()Pojo

@Entity


@Table(name="t_roles")


public class Roles {


@Id


@GeneratedValue(strategy=GenerationType.IDENTITY)


@Column(name="roleid")


private Integer roleid;


@Column(name="rolename")


private String rolename;


@ManyToMany


//@JoinTable:配置中间表信息


//joinColumns:建立当前表在中间表中的外键字段


@JoinTable(name="t_roles_menus",joinColumns=@JoinColumn(name="role_id"),inverseJoinColumns=@JoinColumn(name="menu_id"))


private Set<Menus> menus = new HashSet<>();


public Set<Menus> getMenus() {


return menus;


}


public void setMenus(Set<Menus> menus) {


this.menus = menus;


}


public Integer getRoleid() {


return roleid;


}


public void setRoleid(Integer roleid) {


this.roleid = roleid;


}


public String getRolename() {


return rolename;


}


public void setRolename(String rolename) {


this.rolename = rolename;


}


@Override


public String toString() {


return "Roles [roleid=" + roleid + ", rolename=" + rolename + "]";


}


}


@Entity


@Table(name="t_menus")


public class Menus {


@Id


@GeneratedValue(strategy=GenerationType.IDENTITY)


@Column(name="menusid")


private Integer menusid;


@Column(name="menusname")


private String menusname;


@Column(name="menusurl")


private String menusurl;


@Column(name="fatherid")


private Integer fatherid;


@ManyToMany(mappedBy="menus")


private Set<Roles> roles = new HashSet<>();


public Set<Roles> getRoles() {


return roles;


}


public void setRoles(Set<Roles> roles) {


this.roles = roles;


}


public Integer getMenusid() {


return menusid;


}


public void setMenusid(Integer menusid) {


this.menusid = menusid;


}


public String getMenusname() {


return menusname;


}


public void setMenusname(String menusname) {


this.menusname = menusname;


}


public String getMenusurl() {


return menusurl;


}


public void setMenusurl(String menusurl) {


this.menusurl = menusurl;


}


public Integer getFatherid() {


return fatherid;


}


public void setFatherid(Integer fatherid) {


this.fat 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 herid = fatherid;


}


@Override


public String toString() {


return "Menus [menusid=" + menusid + ", menusname=" + menusname+ ", menusurl=" + menusurl + ", fatherid="+ fatherid + "]";


}


}

[](()操作

/**


  • 多对多关联关系测试

  • @author Administrator


*/


@RunWith(SpringJUnit4ClassRunner.class)


@ContextConfiguration("classpath:applicationContext.xml")


public class ManyToManyTest {


@Autowired


private RolesDao rolesDao;


/**


  • 添加角色同时添加菜单

用户头像

还未添加个人签名 2022.04.13 加入

还未添加个人简介

评论

发布
暂无评论
SpringDataJPA之关联关系_Java_爱好编程进阶_InfoQ写作社区