Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of each layer under SSM Framework

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you the content of the sample analysis of each layer under the SSM framework. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

SSM framework

SSM framework is the integration of spring MVC, Spring and Mybatis framework, and it is the standard MVC mode. The whole system is divided into four layers: presentation layer (web), controller layer, service layer and dao layer. Spring MVC is used for request forwarding and view management.

Spring implements business object management, and Mybatis serves as the persistence engine for data objects.

Presentation layer (web): generally speaking, it is the interface presented to the user, that is, what the user sees when using a system.

Business logic layer (service): operations for specific problems, can also be said to be the operation of the data layer, the logical processing of data services.

Data access layer (dao): the transactions done in this layer directly operate the database, aiming at adding, deleting, modifying, updating, searching, etc.

DataBase = = > Entity = = > Mapper.xml = = > Dao.Java = = > Service.java = = > Controller.java = = > html css js (thymeleaf)

Explanation at each level

Create a maven project structure with IDEA, as shown in the figure on the left. In this project, there are five packages, namely controller, dao, entity, service, and serviceimpl:

The layer describes the classes of the physical layer database in the project, which is mainly used to define the properties that should be with the database object, and provides get/set methods, tostring methods, and parameter-free constructors. When the persistence layer interacts with the database, the dao layer will first create the dao interface, and then you can define the implementation class of the interface in the configuration file; then you can call the dao interface in the module to deal with the data business without paying attention to which class is the specific implementation class of this interface. The data source and database connection parameters of the dao layer are configured in the configuration file. The business layer controls the business, and the logical application design of the business module, like the dao layer, first designs the interface, then creates the class to be implemented, and then configures the association of its implementation in the configuration file. Then you can call the interface in the service layer to process the business logic application. The implementation layer implements all the methods of the service interface, and the integration of service and dao control layer controller layer mainly calls the interface in the Service layer to control the specific business process, and the configuration of the control should also be carried out in the configuration file. The difference between Controller and Service is that Controller is responsible for the flow control of specific business modules; Service layer is responsible for the logical application design of business modules entity layer (model layer)

The entity layer is used to store our entity classes, which is basically consistent with the attribute values in the database, and implements the methods of set and get.

1. Import lombok chili-driven dependencies to generate get/set method dependencies:

Package com.dvms.entity;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import lombok.ToString;import lombok.experimental.Accessors;import java.util.Date;@Data@ToString@AllArgsConstructor@NoArgsConstructor@Accessors (chain = true) / / chain call public class Emp {private String id; private String name; private Double salary; private Integer age; private Date bir;}

two。 Traditional way

Package com.umf.entity;import java.io.Serializable;public class UpdataSettingEntity implements Serializable {private static final long serialVersionUID = 1L; / / private Integer id = 1; / / private int updataflag; / * Settings: * / public void setId (Integer id) {this.id = id } / * get: * / public Integer getId () {return id;} / * Settings: * / public void setUpdataflag (int updataflag) {this.updataflag = updataflag } / * get: * / public int getUpdataflag () {return updataflag;}} dao layer (mapper layer)

The dao layer (interface class) carries on the data persistence operation to the database, his method statement is directly aimed at the database operation, mainly realizes some addition, deletion, modification and query operations, and the method in Mybatis is mainly mapped with xxxDao (Mapper). Xml.

Mybatis can use .xml for data manipulation, can also use annotations in the Dao layer, or can use a combination of xml and dao layer interfaces (commonly used).

Package com.dvms.dao;import com.dvms.entity.User;import org.apache.ibatis.annotations.Param;import org.springframework.stereotype.Repository;import java.util.List;@Repositorypublic interface UserDao {/ / Log in to User login (@ Param ("username") String username, @ Param ("password") String password); / / pass the binding of multiple parameters to be used as parameters in mybatis / / New user (registered) void save (User user) / / query all users List findAlluser (); / / delete user void delete (String id); / / modify user information (check first and then change) User update (String id); void add (User user);}

XxxDao (Mapper) .xml:

Key:

< mapper namespace="com.dvms.dao.UserDao">

Premise: basic database syntax

Insert into user (id,username,password,sex,phone,email,role) values (# {id}, # {username}, # {password}, # {sex}, # {phone}, # {email}, # {role}) select id,username,password,sex,phone,email,role from user where username = # {username} and password = # {password} select id,username,password,sex,phone,email Role from user delete from user where id=# {id} select id,username,password,sex,phone,email,role from user where id=# {id} update user set username=# {username}, password=# {password}, sex=# {sex}, phone=# {phone}, email=# {email}, role=# {role} where id=# {id}

Use annotations in the Dao layer:

Public interface StudentDao {@ Select ("select * from student where stu_id=# {stuId}") public Student queryById (int stuId);} service layer

The service layer (interface class) provides interfaces for the classes in the controller layer to make calls, which are generally encapsulated by self-written methods and implemented in serviceImpl.

The service layer is built on the Dao layer, and the service layer can be established only after the dao layer is established, and the service layer is under the controller layer, so the service layer should not only call the interface of the dao layer, but also provide the interface to the classes of the Controller layer to call, it happens to be in the position of an intermediate layer. Each model has a service interface, and each interface encapsulates its own business processing methods.

Package com.dvms.service;import com.dvms.entity.User;import java.util.List;public interface UserService {/ / New user void save (User user); / / Log in to User login (String username, String password); / / query all users List findAlluser (); / / Delete user void delete (String id); / / modify user information User update (String id); void add (User user);}

Servicedmpl (implementing service layer, integrating service and dao) (importing dao layer) (interface implementation class)

Package com.dvms.service.Impl;import com.dvms.dao.UserDao;import com.dvms.entity.User;import com.dvms.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;import java.util.UUID;@Servicepublic class UserServiceImpl implements UserService {@ Autowired private UserDao userDao; @ Override public void save (User user) {user.setId (UUID.randomUUID () .toString ()); userDao.save (user) @ Override public User login (String username, String password) {return userDao.login (username,password);} @ Override public List findAlluser () {return userDao.findAlluser ();} @ Override public void delete (String id) {userDao.delete (id);} @ Override public User update (String id) {return userDao.update (id) } @ Override public void add (User user) {userDao.add (user);}} Controller layer (web layer)

Responsible for the business process control of the specific module (get the parameters (passed from the front end) and return the response (front end or database or a specified path). You need to call the interface of the service logic design layer to control the business process (import the service layer).

Package com.dvms.controller;import com.dvms.entity.Emp;.@Controllerpublic class EmpController {@ Autowired private EmpService empService; @ GetMapping ("emp/findAll") public String findAll (@ RequestParam (defaultValue = "1") int pageNum, @ RequestParam (defaultValue = "5") int pageSize, Model model) {PageHelper.startPage (pageNum,pageSize) / / PageInfo pageInfo = new PageInfo (empService.findAll ()); model.addAttribute ("pageInfo", pageInfo); / / List emps = empService.findAll (); / / model.addAttribute ("emps", emps); return "ems/tables";} @ RequestMapping ("/ emp/save") public String save (Emp emp) {empService.save (emp); return "redirect:/emp/findAll";}}

The Controller layer calls the interface methods of the Service layer, and the Service layer calls the methods of the Dao layer, where the called parameters are passed using the Entity layer. The View layer and the Controller layer work together and are mainly responsible for the interface display.

Thank you for reading! This is the end of this article on "sample Analysis of each layer under the SSM Framework". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report