@@ -59,6 +59,12 @@ | |||||
<artifactId>lombok</artifactId> | <artifactId>lombok</artifactId> | ||||
<optional>true</optional> | <optional>true</optional> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>com.alibaba</groupId> | |||||
<artifactId>fastjson</artifactId> | |||||
<version>1.2.58</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
</dependencies> | </dependencies> | ||||
<build> | <build> | ||||
@@ -0,0 +1,66 @@ | |||||
package com.xkcoding.orm.jpa.entity; | |||||
import com.xkcoding.orm.jpa.entity.base.AbstractAuditModel; | |||||
import lombok.*; | |||||
import javax.persistence.*; | |||||
import java.util.Collection; | |||||
/** | |||||
* <p> | |||||
* 部门实体类 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jpa.entity | |||||
* @description: 部门实体类 | |||||
* @author: 76peter | |||||
* @date: Created in 2019/10/1 18:07 | |||||
* @copyright: Copyright (c) 2019 | |||||
* @version: V1.0 | |||||
* @modified: 76peter | |||||
*/ | |||||
@EqualsAndHashCode(callSuper = true) | |||||
@Data | |||||
@NoArgsConstructor | |||||
@AllArgsConstructor | |||||
@Builder | |||||
@Entity | |||||
@Table(name = "orm_department") | |||||
@ToString(callSuper = true) | |||||
public class Department extends AbstractAuditModel { | |||||
/** | |||||
* 部门名 | |||||
*/ | |||||
@Column(name = "name", columnDefinition = "varchar(255) not null") | |||||
private String name; | |||||
/** | |||||
* 上级部门id | |||||
*/ | |||||
@ManyToOne(cascade = {CascadeType.REFRESH},optional = true) | |||||
@JoinColumn(name = "superior",referencedColumnName = "id") | |||||
private Department superior; | |||||
/** | |||||
* 所属层级 | |||||
*/ | |||||
@Column(name = "levels", columnDefinition = "int not null default 0") | |||||
private Integer levels; | |||||
/** | |||||
* 排序 | |||||
*/ | |||||
@Column(name = "orderno", columnDefinition = "int not null default 0") | |||||
private Integer orderno; | |||||
/** | |||||
* 子部门集合 | |||||
*/ | |||||
@OneToMany(cascade={CascadeType.REFRESH, CascadeType.REMOVE}, fetch = FetchType.EAGER,mappedBy="superior") | |||||
private Collection<Department> children; | |||||
/** | |||||
* 部门下用户集合 | |||||
*/ | |||||
@ManyToMany(mappedBy = "departmentList") | |||||
private Collection<User> userList; | |||||
} |
@@ -3,9 +3,8 @@ package com.xkcoding.orm.jpa.entity; | |||||
import com.xkcoding.orm.jpa.entity.base.AbstractAuditModel; | import com.xkcoding.orm.jpa.entity.base.AbstractAuditModel; | ||||
import lombok.*; | import lombok.*; | ||||
import javax.persistence.Column; | |||||
import javax.persistence.Entity; | |||||
import javax.persistence.Table; | |||||
import javax.persistence.*; | |||||
import java.util.Collection; | |||||
import java.util.Date; | import java.util.Date; | ||||
/** | /** | ||||
@@ -19,12 +18,12 @@ import java.util.Date; | |||||
* @date: Created in 2018/11/7 14:06 | * @date: Created in 2018/11/7 14:06 | ||||
* @copyright: Copyright (c) | * @copyright: Copyright (c) | ||||
* @version: V1.0 | * @version: V1.0 | ||||
* @modified: yangkai.shen | |||||
* @modified: 76peter | |||||
*/ | */ | ||||
@EqualsAndHashCode(callSuper = true) | @EqualsAndHashCode(callSuper = true) | ||||
@Data | |||||
@NoArgsConstructor | @NoArgsConstructor | ||||
@AllArgsConstructor | @AllArgsConstructor | ||||
@Data | |||||
@Builder | @Builder | ||||
@Entity | @Entity | ||||
@Table(name = "orm_user") | @Table(name = "orm_user") | ||||
@@ -66,4 +65,22 @@ public class User extends AbstractAuditModel { | |||||
*/ | */ | ||||
@Column(name = "last_login_time") | @Column(name = "last_login_time") | ||||
private Date lastLoginTime; | private Date lastLoginTime; | ||||
/** | |||||
* 关联部门表 | |||||
*/ | |||||
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) | |||||
@JoinTable(name = "orm_usersetdept",joinColumns = @JoinColumn(name = "userid",referencedColumnName="id"), | |||||
inverseJoinColumns = @JoinColumn(name = "deptid",referencedColumnName="id")) | |||||
//1、关系维护端,负责多对多关系的绑定和解除 | |||||
//2、@JoinTable注解的name属性指定关联表的名字,joinColumns指定外键的名字,关联到关系维护端(User) | |||||
//3、inverseJoinColumns指定外键的名字,要关联的关系被维护端(Department) | |||||
//4、其实可以不使用@JoinTable注解,默认生成的关联表名称为主表表名+下划线+从表表名, | |||||
//即表名为user_department | |||||
//关联到主表的外键名:主表名+下划线+主表中的主键列名,即user_id,这里使用referencedColumnName指定 | |||||
//关联到从表的外键名:主表中用于关联的属性名+下划线+从表的主键列名,department_id | |||||
//主表就是关系维护端对应的表,从表就是关系被维护端对应的表 | |||||
private Collection<Department> departmentList; | |||||
} | } |
@@ -0,0 +1,26 @@ | |||||
package com.xkcoding.orm.jpa.repository; | |||||
import com.xkcoding.orm.jpa.entity.Department; | |||||
import org.springframework.data.jpa.repository.JpaRepository; | |||||
import org.springframework.stereotype.Repository; | |||||
import java.util.Collection; | |||||
/** | |||||
* <p> | |||||
* User Dao | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jpa.repository | |||||
* @description: Department Dao | |||||
* @author: 76peter | |||||
* @date: Created in 2019/10/1 18:07 | |||||
* @copyright: Copyright (c) 2019 | |||||
* @version: V1.0 | |||||
* @modified: 76peter | |||||
*/ | |||||
@Repository | |||||
public interface DepartmentDao extends JpaRepository<Department, Long> { | |||||
public Collection<Department> findDepartmentsByLevels(Integer level); | |||||
} |
@@ -11,3 +11,24 @@ CREATE TABLE `orm_user` ( | |||||
`last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间', | `last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间', | ||||
`last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间' | `last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间' | ||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; | ||||
DROP TABLE IF EXISTS `orm_department`; | |||||
CREATE TABLE `orm_department` ( | |||||
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键', | |||||
`name` VARCHAR(32) NOT NULL COMMENT '部门名称', | |||||
`superior` INT(11) COMMENT '上级id', | |||||
`levels` INT(11) NOT NULL COMMENT '层级', | |||||
`orderno` INT(11) NOT NULL DEFAULT 0 COMMENT '排序', | |||||
`create_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '创建时间', | |||||
`last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间' | |||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; | |||||
--DROP TABLE IF EXISTS `orm_usersetdept`; | |||||
CREATE TABLE `orm_usersetdept` ( | |||||
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键', | |||||
`userid` INT(11) NOT NULL COMMENT '用户id', | |||||
`deptid` INT(11) NOT NULL COMMENT '部门id', | |||||
`create_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '创建时间', | |||||
`last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间' | |||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; |
@@ -0,0 +1,86 @@ | |||||
package com.xkcoding.orm.jpa.repository; | |||||
import com.xkcoding.orm.jpa.SpringBootDemoOrmJpaApplicationTests; | |||||
import com.xkcoding.orm.jpa.entity.Department; | |||||
import com.xkcoding.orm.jpa.entity.User; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import net.minidev.json.JSONArray; | |||||
import org.junit.Test; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import javax.transaction.Transactional; | |||||
import java.util.Collection; | |||||
import java.util.List; | |||||
/** | |||||
* <p> | |||||
* jpa 测试类 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jpa.repository | |||||
* @description: jpa 测试类 | |||||
* @author: 76peter | |||||
* @date: Created in 2018/11/7 14:09 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: 76peter | |||||
*/ | |||||
@Slf4j | |||||
public class DepartmentDaoTest extends SpringBootDemoOrmJpaApplicationTests { | |||||
@Autowired | |||||
private DepartmentDao departmentDao; | |||||
@Autowired | |||||
private UserDao userDao; | |||||
/** | |||||
* 测试保存 ,根节点 | |||||
*/ | |||||
@Test | |||||
@Transactional | |||||
public void testSave() { | |||||
Collection<Department> departmentList = departmentDao.findDepartmentsByLevels(0); | |||||
if(departmentList.size()==0){ | |||||
Department testSave1 = Department.builder().name("testSave1").orderno(0).levels(0).superior(null).build(); | |||||
Department testSave1_1 = Department.builder().name("testSave1_1").orderno(0).levels(1).superior(testSave1).build(); | |||||
Department testSave1_2 = Department.builder().name("testSave1_2").orderno(0).levels(1).superior(testSave1).build(); | |||||
Department testSave1_1_1 = Department.builder().name("testSave1_1_1").orderno(0).levels(2).superior(testSave1_1).build(); | |||||
departmentList.add(testSave1); | |||||
departmentList.add(testSave1_1); | |||||
departmentList.add(testSave1_2); | |||||
departmentList.add(testSave1_1_1); | |||||
departmentDao.saveAll(departmentList); | |||||
Collection<Department> deptall = departmentDao.findAll(); | |||||
log.debug("【部门】= {}", JSONArray.toJSONString((List)deptall)); | |||||
} | |||||
userDao.findById(1L).ifPresent(user -> { | |||||
user.setName("添加部门"); | |||||
Department dept = departmentDao.findById(2L).get(); | |||||
user.setDepartmentList(departmentList); | |||||
userDao.save(user); | |||||
}); | |||||
User users = userDao.findById(1L).get(); | |||||
log.debug("用户部门={}", JSONArray.toJSONString((List)userDao.findById(1L).get().getDepartmentList())); | |||||
departmentDao.findById(2L).ifPresent(dept -> { | |||||
Collection<User> userlist = dept.getUserList(); | |||||
//关联关系由user维护中间表,department userlist不会发生变化,可以增加查询方法来处理 重写getUserList方法 | |||||
log.debug("部门下用户={}", JSONArray.toJSONString((List)userlist)); | |||||
}); | |||||
userDao.findById(1L).ifPresent(user -> { | |||||
user.setName("清空部门"); | |||||
user.setDepartmentList(null); | |||||
userDao.save(user); | |||||
}); | |||||
log.debug("用户部门={}", userDao.findById(1L).get().getDepartmentList()); | |||||
} | |||||
} |