当前位置:网站首页>Platform management background and merchant menu resource management: Design of platform management background data service
Platform management background and merchant menu resource management: Design of platform management background data service
2022-06-26 16:58:00 【Seconds to write code】
Platform management background and business menu resource management
Platform management background is to provide services for operators of e-commerce platform , It mainly includes business management and some public configuration management functions . In the design of business management , Including business registration 、 to examine 、 Business user's authority management and menu resource management and other functions . In addition to the design of some public management functions , Platform management background itself also has the design of security control management .
The project engineering of platform management background is manage-microservice, Complete source code can be downloaded from the source code of this book .
The branch of the example code in this chapter is V2.1, Please pay attention to update . The development of platform management background mainly includes two parts , One part is the access control management design of the management background itself , The other part is the business and its menu resource management . The functions of these two parts are in the module manage-web To realize .

Platform management background data service design
Platform management background is an independent application system , It has its own user system and independent authority management design . The access control and authority management of the platform management background are mainly controlled by the operator 、 It's made up of roles and departments , What is the relationship between these entities : An operator can only be subordinate to one department , At the same time, an operator can have multiple roles .
Entity modeling
To achieve access control and simple rights management design , We define three entity objects , They are the operators 、 Roles and departments .
Operator's entity object Operators Mainly by name 、 mailbox 、 Gender 、 It is composed of password, department and other attributes , The implementation code is as follows :
@Entity
@Table (name = "toperator")
public class Operators extends IdEntity{
private String name;
private string email;private Integer sex;
@DateTimeFormat (pattern = "yyyy-MM-dd HH:mm :ss")
ecolumn(name = "created",columnDefinition = "timestamp defaultCurrent timestamp ")
@Temporal (TemporalType.TIMESTAMP)private Date created;
private String password;
@ManyToOne
@JoinColumn (name = "did")@JsonBackReference
private Department department;
@ManyToMany(cascade ={},fetch = FetchType.EAGER)@JoinTable(name = "operator part",
joinColumns = {@JoinColumn(name - "operator_id")},inverseJoinColumns = {@JoinColumn(name = "part_id")})
private List<Part> parts = new ArrayList<>();
public Operators() {
}
...
}In this design , It mainly realizes the operator's association relationship . On the one hand, it connects the Department entities in a many to one relationship , That is, an operator can only belong to one department ; On the other hand, role entities are associated with many to many relationships , That is, an operator can have multiple roles .
The entity object of the character Part It is mainly composed of attributes such as name and creation time , The implementation code is as follows :
@Entity
@Table(name = "t part")
public class Part extends IdEntity {
private String name;
@DateTimeFormat (pattern = "yyyy-MM-dd HH:mm:ss")
@Column (name = "created",columnDefinition = "timestamp defaultcurrenttimestamp")
@Temporal(TemporalType.TIMESTAMP)private Date created;
public Part(){
}
...
}This character design has no associated resources , Its access design will be implemented in a simpler way later in this chapter .
The entity object of the Department Department It is mainly composed of attributes such as name and creation time , The implementation code is as follows :
@Entity
@Table(name = "t department")
public class Department extends IdEntity {
private String name;
@DateTimeFormat (pattern = "yyyy-MM-dd HH:mm :ss")
eColumn (name = "created",columnDefinition = "timestamp defaultcurrent timestamp ")
@Temporal (TemporalType.TIMESTAMP)private Date created;
public Department (){
}
...
}In the design of association between entity objects , Using the design principle of one-way Association , That is, in the Department entity design , No longer associated with the operator . If you need to query the operator from the Department , You can use SQL Query statement implementation
In the entity object design above , All inherit an abstract class Baseld, It's used to implement ID Definition of primary key , The code is as follows :
@MappedSuperclass
public abstract class Baseld implements Serializable{
private static final long serialVersionUID =1L;
@Id
@Generatedvalue (strategy = GenerationType. IDENTITY)private Long id;
public Long getId() {
return id;
public void setId (Long id){
this.id= id;
}
}Give an action to an entity
Define a repository interface for the entity , By binding JPA The repository , You can give it some basic behavior , Realize the persistent design of entity . In the definition of the repository interface , You can also add other operation methods of an entity through declaration method .

For the operator entity object in the previous section , Its operation can be designed in the following ways :
@Repository
public interface OperatorRepository extends JpaRepository<Operators,Long>,JpaSpecificationExecutor<0perators> {
CQuery ("select t from Operators t where t.name =?1 and t.email =?2")Operators findByNameAndEmail(String name, String email);
GQuery ("select distinct u from Operators u where u.name= :name ")Operators findByName (RParam( "name") String name);
cQuery ("select distinct u from Operators u where u.id= :id")Operators findById(@Param("id") Long id);
@Query("select o from Operators o "+
"left join o.parts p"+
"where p.id= :id")
List<0perators> findByPartId(@Param ("id") Long id);
}Here are a few methods declared , It's all through SQL Query statements extend the behavior of operator entity objects . among findByPartld Realized through the Department ID Query operator list function .
in addition , In the persistence design of role entities and department entities , Just create a simple repository interface , By binding JPA Interface , The basic operation can be realized , I won't repeat .
Data access service design
Data access service is a service layer encapsulation design to call the repository interface , Through the development of service layer , It can provide unified transaction management for the call of repository interface , Implement other extension designs .
The development of operator entity data access service is taken as an example to illustrate .
For the general operation of adding, deleting, modifying and querying , You can use the design shown below :
@Service
@Transactional
public class OperatorService
CAutowired
private operatorRepository operatorRepository;
public String insert (Operators operators) {
try{
Operators old = findByName (operators .getName());
if(old == null) {
operatorRepository.save (operators) ;
return operators.getId() .toString();
}else{
return " user name '"+ old.getName()+” Already exist ! ";
}catch (Exception e) {
e.printStackTrace() ;
return e.getMessage() ;
}
}
public string update (Operators operators) {
try{
operatorRepository. save (operators) ;
return operators.getId() . toString() ;
}catch (Exception e){
e.printStackTrace() ;
return e.getMessage() ;
}}
public String delete (Long id) {
try{
operatorRepository .deleteById(id) ;
return id. toString() ;
}catch (Exception e) {
e.printStackTrace() ;
return e.getMessage() ;
}
}
public List<0perators>findA11(){
return operatorRepository.findAl1();
}
public Operators find0ne (Long id){
return operatorRepository.findByOperatorId(id);
}
public Operators findByName (string name){
return operatorRepository.findByName (name) ;
public List<Operators> findByPartId (Long partId){
return operatorRepository.findByPartId(partId);
}
}These methods all realize data access operation by calling the repository interface of operator entity . among , For the operator , Because you need to use a user name for login verification , Therefore, de duplication check is used when adding new data .

For paged queries , By defining a Specification Implement complex queries , The code is as follows :
@service
@Transactional
public class OperatorService {
@Autowired
private OperatorRepository operatorRepository;
public Page<0perators> findAll (0peratorsVo operatorsVo){
Sort sort = Sort.by (Sort.Direction.DESC, "created");
Pageable pageable = PageRequest.of(operatorsVo.getPage(),
operatorsVo.getsize(), sort);
return operatorRepository.findAll (new Specification<Operators>(){
@override
public Predicate toPredicate (Root<Operators> root, CriteriaQuery<
query, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicatesList = new ArrayList<Predicate>();
if(CommonUtils.isNotNull (operatorsVo.getName())){
predicatesList.add (criteriaBuilder.like (root.get ("name"),
"g"+operatorsVo.getName() +"o"));
}
if (CommonUtils.isNotNull(operatorsVo.getCreated())){
predicatesList.add (criteriaBuilder.greaterhan (root.get ("created"),operatorsVo. getCreated()));
query.where(predicatesList.toArray (new
Predicate[predicatesList.size()]));
return query.getRestriction();
},pageable);
}
}among , The parameters of paging query can be designed as needed , Only operator name and creation date are provided for query .
among , The parameters of paging query can be designed as needed , Only operator name and creation date are provided for query .
unit testing
After completing the design of data access service , You can do a unit test , To verify that the design is correct .
Here's a test case for inserting data , Through this test case, we can create an administrator user who logs in to the system :
@Runwith (SpringRunner.class)
@ContextConfiguration (classes ={JpaConfiguration.class,ManageRestApiApplication.class})
@SpringBootTest
public class BbServiceTest {
private static Logger logger =
LoggerFactory.getLogger (BbServiceTest.class);
@Autowired
private OperatorService operatorService;@Autowired
private PartService partService;@Autowired
private DepartmentService departmentService;
@Test
public void insertData(){
Partpart=new Part(;part.setName ( "admins");
partService.save(part);
Department department = new Department ();department.setName(" Technology Department ");
departmentService.save(department);
Operators operators =new Operators();operators.setName ("admin");
operators.setSex(1);
operators.set Department (department);
List<Part> partList = operators.getParts();partList.add(part);
operators.setParts(partList);
BCryptPasswordEncoder bc = new BCryptPasswordEncoder();operators.setPassword(bc.encode ( "123456"));
operatorService.insert(operators);
assert operators.getId() >0: "create error";
}
}This test case does the following :
(1) Created a character , The name is admins( You can also think of it as a user group ).
(2) Created a department , It's called technology department .
(3) A user is created , The name is admin, And set the password to 123456.
If the test is successful , Then the generated data can be used for later development . We can use admin This user logs in as a system administrator .
Other test cases can be designed according to this method .
The content of this article is platform management background and business menu resource management : Platform management background data service design
- The next article is to explain the platform management background and business menu resource management : Platform management background access control design ;
- Friends who think the article is good can forward this article and pay attention to Xiaobian ;
- Thank you for your support !
边栏推荐
- Cloud platform monitoring system based on stm32+ Huawei cloud IOT design
- COMP5216 Mobile Computing Assignment 1 - Extending ToDoList app
- What is flush software? Is it safe to open an account online?
- Incomplete line spacing adjustment of formula display in word
- Scala Foundation (2): variables et types de données
- Teach you to learn dapr - 4 Service invocation
- [latex bearer] use tables in \title (error \begin doesn't match its definition.)
- 合约量化系统开发方案详细,量化合约系统开发技术说明
- 5g is not flat and 6G is restarted. China leads wireless communication. What is the biggest advantage of 6G?
- LeetCode Algorithm 24. Exchange the nodes in the linked list in pairs
猜你喜欢

20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)

Redis' 43 serial cannons, try how many you can carry

Pybullet robot simulation environment construction 5 Robot pose visualization

Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)

Interpretation of cloud native microservice technology trend

Junit单元测试

Teach you to learn dapr - 4 Service invocation

Environment setup mongodb
![[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity](/img/e8/9c5f1658a252c3c80503b5021917f6.jpg)
[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity

Over the weekend: 20000 words! Summary of JVM core knowledge, 18 serial cannons as a gift
随机推荐
7 views on NFT market prospect
20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)
板卡的分级调试经验
Several forms of buffer in circuit
Community ownership of NFT trading market is unstoppable
Summary of all knowledge points of C language
Implementation of MySQL master-slave architecture
STM32F103C8T6实现呼吸灯代码
Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)
QT 5.9.8 installation tutorial
day10每日3题(3):数组中的字符串匹配
Greenplum database fault analysis - semop (id=2000421076, num=11) failed: invalid argument
数字藏品与NFT到底有何区别
[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap
Day10 daily 3 questions (3): String Matching in array
Teach you to learn dapr - 6 Publish subscription
JS tutorial electron JS is a good tool for designing powerful multi platform desktop applications
Teach you to learn dapr - 1 The era of net developers
Leetcode 1170. 比较字符串最小字母出现频次(可以,已解决)
Necessary decorator mode for 3 years' work