SpringDataJPA之关联关系


 本文介绍下SpringDataJPA怎么处理关联关系

关联关系处理

1.一对一

 用户和角色设置一对一关系,通过外键关联。

pojo设置

创建 Users 实体

@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;
    
    public Roles getRoles() {
        return roles;
    }
    public void setRoles(Roles roles) {
        this.roles = roles;
    }
    @OneToOne
    //@JoinColumn:就是维护一个外键
    @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 + "]";
    }
}

创建 Roles 实体

@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;
    
    @OneToOne(mappedBy="roles")
    private Users users;
    public Users getUsers() {
        return users;
    }
    public void setUsers(Users users) {
        this.users = users;
    }
    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;
    }
}

关联关系操作

/**
* 一对一关联关系测试
* @author Administrator
*
*/
@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);
    }
}

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.fatherid = 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;
    /**
    * 添加角色同时添加菜单
    */
    @Test
    public void test1(){
        //创建角色对象
        Roles roles = new Roles();
        roles.setRolename("超级管理员");
        //创建菜单对象 XXX 管理平台 --->用户管理
        Menus menus = new Menus();
        menus.setMenusname("XXX 管理平台");
        menus.setFatherid(-1);
        menus.setMenusurl(null);
        //用户管理菜单
        Menus menus1 = new Menus();
        menus1.setMenusname("用户管理");
        menus1.setFatherid(1);
        menus1.setMenusurl(null);
        //建立关系
        roles.getMenus().add(menus);
        roles.getMenus().add(menus1);
        menus.getRoles().add(roles);
        menus1.getRoles().add(roles);
        //保存数据
        this.rolesDao.save(roles);
    }
    /**
    * 查询 Roles
    */
    @Test
    public void test2(){
        Roles roles = this.rolesDao.findOne(3);
        System.out.println("角色信息:"+roles);
        Set<Menus> menus = roles.getMenus();
        for (Menus menus2 : menus) {
            System.out.println("菜单信息:"+menus2);
        }
    } 
}

搞定~


上一篇:苹果二手手机换购计划很快支持 Android 等手机


下一篇:我的Android进阶之旅------>Android颜色值(RGB)所支持的四种常见形式