Practical records of campus second-hand trading platform based on SSM framework (2)

persistent object layer

First, let’s put down a directory structure:
Insert image description here
You can see from the directory structure that it is basically the addition, deletion, modification, and search of users, goods, and bills. I will only give an example of a user management operation. The rest of the goods and bills can be copied. .
First create a User class in the pojo folder, and then write the class attributes and toString, get, and set methods.

public class Yzq_User {
 private String userId;
 private String username;
 private String password;
 private int role;
 private String phone;
 private String gender;
 private int age;
 private Date createtime;
 private String address;
 
 @Override
 public String toString() {
  return "Yzq_User [userId=" + userId + ", username=" + username
    + ", password=" + password + ", role=" + role + ", phone="
    + phone + ", gender=" + gender + ", age=" + age
    + ", createtime=" + createtime + ", address=" + address + "]";
 }
 
 public Yzq_User(String userId, String username, String password, int role,
   String phone, String gender, int age, Date createtime,
   String address) {
  super();
  this.userId = userId;
  this.username = username;
  this.password = password;
  this.role = role;
  this.phone = phone;
  this.gender = gender;
  this.age = age;
  this.createtime = createtime;
  this.address = address;
 }
 
  public Yzq_User() {
  super();
 }
 
  public String getUserId() {
  return userId;
 }
  public void setUserId(String userId) {
  this.userId = userId;
 }
  public String getUsername() {
  return username;
 }
  public void setUsername(String username) {
  this.username = username;
 }
  public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public int getRole() {
  return role;
 }
 public void setRole(int role) {
  this.role = role;
 }
 public String getPhone() {
  return phone;
 }
 public void setPhone(String phone) {
  this.phone = phone;
 }
 public String getGender() {
  return gender;
 }
 public void setGender(String gender) {
  this.gender = gender;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public Date getCreatetime() {
  return createtime;
 }
 public void setCreatetime(Date createtime) {
  this.createtime = createtime;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
}

Web presentation layer

First look at the JSP page. Let's take the registration page as an example. Fill in the registration information in the form and click the registration button to achieve the registration effect. In fact, it is to implement the operation of adding users. If you learn this, then other deletions, modifications, and queries are all the same idea. Bills and goods are the same. reason.

<form id="useraddForm" name="useraddForm" class="register" action="${pageContext.request.contextPath }/addUser.html" method="post">

I will skip the input fields of the form and focus on the submission path action, which is required.
When deleting, modifying, or querying, you must pay attention to the path. To delete a product, you only need to bind the product ID to the path, like this:

<a href="${pageContext.request.contextPath }/goUpGoods.html?goodsId=${goods.goodsId}">

Next, create a UserController class under the controller package according to the directory structure.

@Controller
public class Yzq_UserController {
    @Autowired
    private Yzq_UserService userService;

    //添加user
    @RequestMapping("/addUser.html")
    public String addUser(Yzq_User user) throws Exception{
     userService.addUser(user);
        return "yzq_login";
    }
}
  • Pay attention to /addUser.html filled in the @RequestMapping annotation. This is the action filled in the JSP page in the previous step. The two must be consistent, so the action filled in the form at that time was submitted here.
  • Return "yzq_login" is to jump to the login page. If the user registration is successful, it will jump to the login page.
  • Pay attention to the @Controller and @Autowired annotations.
  • At this time, Yzq_UserService userService and userService.addUser(user) must be popular, because we haven't written UserService yet, and it will be OK when we write it in the next step.

business logic layer

Create a UserService interface under the service package according to the directory structure. Note that it is an interface.

public interface Yzq_UserService {
	//添加user
 	public void addUser(Yzq_User user) throws Exception;
 }

Then create the UserService implementation class UserServiceImpl under this package to implement the UserService interface.

@Service
public class Yzq_UserServiceImpl implements Yzq_UserService{
	@Autowired
	private Yzq_UserMapper userMapper;

	//添加user
	@Override
	public void addUser(Yzq_User user) throws Exception {
	// TODO Auto-generated method stub
	userMapper.addUser(user);
	}
}
  • Don’t miss the annotations @Service and @Autowired.
  • Yzq_UserMapper userMapper and userMapper.addUser(user) are definitely popular for the same reason as before.

data access layer

Create a UserMapper interface under the dao package according to the directory structure. Note that it is an interface.

public interface Yzq_UserMapper {
	//添加user
	public void addUser(Yzq_User user) throws Exception;
}

Then create the UserMapper mapping file UserMapper.xml under this package.

<mapper namespace="com.kmushop.dao.user.Yzq_UserMapper">
 <resultMap id="BaseResultMap" type="com.kmushop.pojo.Yzq_User">
  <id column="user_id" property="userId" />
  <result column="user_name" property="username" />
  <result column="password" property="password" />
  <result column="user_role" property="role" />
  <result column="phone" property="phone" />
  <result column="gender" property="gender" />
  <result column="age" property="age" />
  <result column="user_address" property="address" />
 </resultMap>
 <sql id="dataFields">
  user_id, user_name, password, user_role, phone, gender, user_address
 </sql>

<!--
 添加user
 -->
 <insert id="addUser" parameterType="com.kmushop.pojo.Yzq_User">
  insert into user(user_id, user_name, password, user_role, phone, gender, user_address)
  values (#{userId}, #{username}, #{password}, '2', #{phone}, #{gender}, #{address})
 </insert>
 </mapper>
  • Column is a field in the database table, and property is an attribute in the user class. This realizes the mapping between fields in the database table and attributes in the entity class.
  • The insert section is the sql statement of insert. Pay attention to fill in the parameterType and remember to add # to the values ​​value.

Now the function of adding user is implemented, and the remaining functions of deletion, modification, and query are all a process, pojo -> JSP -> Controller -> Service -> Mapper.
The main thing is to write different SQL statements according to different functions.

Guess you like

Origin blog.csdn.net/qq_45050480/article/details/108521439