@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(“classpath:applicationContext.xml”)
public class OneToOneTest {
@Autowired
private UsersDao usersDao;
/**
- 添加用户同时添加角色
*/
@Test
public void test1(){
//创建角色
Roles roles = new Roles();
roles.setRolename(“管理员”);
//创建用户
Users users = new Users();
users.setUserage(30);
users.setUsername(“赵小刚”);
//建立关系
users.setRoles(roles);
roles.setUsers(users);
//保存数据
this.usersDao.save(users);
}
/**
- 根据用户 ID 查询用户,同时查询用户角色
*/
@Test
public void test2(){
Users users = this.usersDao.findOne(13);
System.out.println(“用户信息:”+users);
Roles roles = users.getRoles();
System.out.println(roles);
}
}
从角色到用户的一对多的关联关系
角色:一方
用户:多方
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 = 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);
}
}
一个角色可以拥有多个菜单,一个菜单可以分配多个角色。多对多的关联关系
角色:多方
菜单:多方
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 = new HashSet<>();
public Set getMenus() {
return menus;
}
public void setMenus(Set 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 = new HashSet<>();
public Set getRoles() {
return roles;
}
public void setRoles(Set 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;
}