SSM framework integration Ideas & amp; function implementation, SSM framework integration ideas

Source: Internet
Author: User

SSM framework integration Ideas & functions, and SSM framework integration ideas

This is my first blog about the integrated idea of SSM framework and simple function implementation.

First of all, I recently learned Spring + SpringMVC + Mybatis and encountered various problems during development. People around me have provided me with some ideas. I will sort them out a little bit and send them up for my own consideration, you can also share it with us. I hope to have a comprehensive understanding and help you better.

Requirement: to develop your own blog system, you must have various functions such as logon and registration, addition, deletion, modification, and query of blogs.

Development Environment: IDEA, MySQL, and JDK1.8 (not to be repeated)

First, we have a corresponding understanding of the SSM framework.

SpringMVC: mainly for page request acceptance and response.

Components include: front-end controller, processor er, processor adapter, View parser, processor Handler, and View. Only the processor Handler and View need to be developed by the programmer.

View is an interface that supports different View types (jsp, freemarker, pdf, etc)

In addition, the processor Handler is the Controller.

--------------------------- I am a split line ------------------------

Service layer function: inject dao and call dao method

---------------------- I am a split line, and I am another ------------------------

Mybatis: Remember the ultimate goal when starting Mybatis. We need to operate the data in the database, so you only need to remember that it is convenient, convenient, and convenient to use. (It takes a whole day to describe the configuration of Myabtis in detail)

So, in the end, our development idea is very clear. We get data from the page to the Controller layer, and then pass the data to the service layer for relevant operations (call methods, etc ), then, upload the data to the persistent layer to add, delete, modify, and query the database. In this case, is it clear a lot?

The following code only intercepts fragments to sort out my ideas.

The first thing to prepare is the page. I made a simple login page. The page snippets are as follows:

<Form action = "/login" class = "form-signin" method = "post"> 

 

The page is available, and the database should also have fields such as id, username, and password. And get/set the relevant field of model (pojo. The snippets are as follows:

public class User {    private BigInteger id;    private String username;    private String pass_word;    private String email;    public BigInteger getId() {        return id;    }    public void setId(BigInteger id) {        this.id = id;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPass_word() {        return pass_word;    }    public void setPass_word(String pass_word) {        this.pass_word = pass_word;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }}

At the same time, prepare SQL statements (which are essential for database operations) in xml files and corresponding java files.

Take logon as an example. xml and corresponding er. java are as follows:

<! -- User Logon --> <select id = "login" parameterType = "com. myblogs. model. user "resultType =" int "> SELECT count (*) FROM user WHERE username =#{ username} AND pass_word =#{ pass_word} </select>
/*** User Logon */int login (user User user );

Note that the input/output types and names must be consistent. At the same time, the Directory of xml and related mapper files must be consistent, for example, under the com. myblogs. mapper directory.

Let me talk about my SQL statement ideas. Count (*) is a statistical number. When the account and password match the database, there is at least one record. Then I will take the number of records obtained to determine whether the logon is successful!

In this way, the persistence layer is ready, especially fast. What is the order below? Business layer. That is, call the method of the previously written er file.

You need a service interface and implementation class, that is, serviceimpl. java. Because the service interface and the mapper file content are consistent, you can directly write the interface implementation class without writing. My implementation class is as follows:

@ Autowired private UserMapper userMapper; // the User logs on to int login (user) {int count = userMapper. login (User); if (count! = 0) {return 1;} return 0 ;}

Here, I want to make judgments. The number of records obtained by the database. Do you remember the previous count? If the returned int value is not 0, the data is obtained, that is, the logon is successful (the user name and password entered on the page are correct to the database). Otherwise, the logon fails, the user name or password is incorrect. So I made a judgment on this layer.

 

Yes, the service layer has been written. What about next? Write the Controller layer, which is the previously mentioned Handler (reiterate: both mean one)

 @Autowired    UserMapper userMapper;    @RequestMapping("/login")    public String loginPageShow(){        return "login";    }    @RequestMapping(value = "/login",method = RequestMethod.POST)    public String loginPage(User user, HttpSession session){        int count=userMapper.login(user);        if (count==1){            session.setAttribute("username",user.getUsername());            return "loginsuccess";        }        return "login";    }

If the logon succeeds, the loginsuccess page is returned. If the logon fails, the login page is returned and the user name and password are re-entered. These fields must also be included on the corresponding page, with the jsp file in front.

Then, run and modify the bug! A simple login is achieved !!!!!!

The effect is as follows:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.