当前位置:网站首页>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 !
边栏推荐
- Knowing these commands allows you to master shell's own tools
- Basic requirements: 7 problems in singleton mode
- Notes on key review of software engineering at the end of the term
- Interpretation of new plug-ins | how to enhance authentication capability with forward auth
- Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
- 7 views on NFT market prospect
- 链游系统开发技术方案设计丨NFT链游系统开发流程及源码
- Teach you to learn dapr - 4 Service invocation
- Pybullet robot simulation environment construction 5 Robot pose visualization
- JS tutorial electron JS is a good tool for designing powerful multi platform desktop applications
猜你喜欢

Byte interview: two array interview questions, please accept

Fire evacuation and self rescue... This safety production and fire training is full!

Interpretation of cloud native microservice technology trend

Interpretation of new plug-ins | how to enhance authentication capability with forward auth

对NFT市场前景的7个看法

Can Luo Yonghao succeed in entering the AR field this time?

I regard it as a dry product with a monthly income of more than 30000 yuan for sidelines and more than 10000 yuan for novices!

Screenshot of the answers to C language exercises

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

Wechat app mall, review products, upload commodity pictures, and score Commodity Services
随机推荐
Can Luo Yonghao succeed in entering the AR field this time?
Codeforces Round #802 (Div. 2)
防火 疏散 自救…这场安全生产暨消防培训干货满满!
Toupper function
Redis overview
Calculate the average of N numbers in the group indexed by the formal parameter x, move the data less than the average in the group indexed to the front of the array, and move the data greater than or
Leetcode 1169. 查询无效交易(如果数据量不大,这种题还是得暴力枚举解决)
Classical synchronization problem
Web3去中心化存储生态图景
[Error] ld returned 1 exit status
Teach you to learn dapr - 4 Service invocation
Call the random function to generate 20 different integers and put them in the index group of institute a
Deployment and operation of mongodb partitioned cluster
Some explanations for latex CJK
How can I get the stock account opening discount link? Is online account opening safe?
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
链游系统开发技术方案设计丨NFT链游系统开发流程及源码
量化合约系统开发分析案例丨合约量化系统开发方案详解
No manual prior is required! HKU & Tongji & lunarai & Kuangshi proposed self supervised visual representation learning based on semantic grouping, which significantly improved the tasks of target dete
Memory partition model