forked from xkcoding/spring-boot-demo
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jpa增加部门上下级自关联,用户部门多对多关联 (xkcoding#59)
* 添加部门之间多对一关联,部门和用户多对多关联。
- Loading branch information
Showing
6 changed files
with
227 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
spring-boot-demo-orm-jpa/src/main/java/com/xkcoding/orm/jpa/entity/Department.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
spring-boot-demo-orm-jpa/src/main/java/com/xkcoding/orm/jpa/repository/DepartmentDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...ng-boot-demo-orm-jpa/src/test/java/com/xkcoding/orm/jpa/repository/DepartmentDaoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
|
||
|
||
} | ||
|
||
|
||
} |