Spring+spring Mvc+mybatis+maven Building Multi-module project (II.)

Source: Internet
Author: User

Based on the first article "Spring+spring Mvc+mybatis+maven Building Multi-module project (i)", write a complete example, from the page to the DAO layer of the entire process 1, Create the Com.bug.model.user package under the Bug.model module and create the Uservo object in the package

Package com.bug.model.user;PublicClass Uservo {Private String userId;Private String UserName;private String password;Public StringGetUserId () {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;}}        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

2. Create the Com.bug.dao.user package in the Bug.dao module and create the Iuserdao interface in the package

package com.bug.dao.user;import org.apache.ibatis< Span class= "Hljs-preprocessor" >.annotations;import com.bug.model.user;p ublic interface Iuserdao {void AddUser (@Param ( "user") Uservo user)            
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

In order to facilitate, at the same time in the Com.bug.dao.user package to create the Iuserdao interface corresponding mapper file Iuserdao.xml, in fact, in the Applicationcontext.xml file has also specified the location of the mapper file

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace=  "Com.bug.dao.user.IUserDao" > < Insert id= "AddUser"  Parametertype= "Com.bug.model.user.UserVO" > INSERT into user_t (user_id, user_name, Password) VALUES (#{user.userid,jdbctype=varchar}, #{user.username,jdbctype=varchar}, #{user.password,jdbctype= VARCHAR}) </insert></MAPPER>             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

3. Create the Com.bug.service.user package in the Bug.service module and create the Iuserservicer interface in the package

com.bug.service.user;import com.bug.model.user.UserVO;public interface IUserService { void addUser(UserVO user) throws Exception;}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

My design is to put the service interface and its implementation into the Bug.service module, so under the user package to create the Impl child package, create a Iuserservice interface implementation Userserviceimpl in the child package, and invoke the DAO layer interface in the interface

Package Com.bug.service.user.impl;Import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import Com.bug.dao.user.IUserDao; import Com.bug.model.user.UserVO; import com.bug.service.user.IUserService;  @Service ( "UserService")  Public class userserviceimpl implements iuserservice { @Autowired Span class= "Hljs-keyword" >private Iuserdao Userdao;  @Override public void adduser (Uservo user) throws exception{userdao.adduser (user);}}    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

4. Create the Com.web.controller.user package under the Bug.web module, create the Usercontroller control layer in the package, respond to requests sent by the page, and invoke the interface of the service layer

PackageCom. Bug. controller. user; Import org. springframework. Beans. Factory. annotation. Autowired; Import org. springframework. stereotype. Controller; Import org. springframework. Web. bind. annotation. Requestmapping; importCom. Bug. Model. user. Uservo; importCom. Bug. Service. user. Iuserservice; @Controller @requestmapping ("/USER") public class Usercontroller {@Autowired private iuserservice UserService "/login") public string login () {Uservo user = New Uservo () .setuserid (  "1") .setusername ( "test1" ) .setpassword (.adduser (user) ; } catch (Exception e) {e.printstacktrace () ;} return  "User/login" ;}         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

The "User/login" returned here, the renderer will itself match the login.jsp in the Web-inf/jsp/user directory, Since the scanning range has been limited in the spring-servlet.xml, all control layers must be placed under the Com.web.controller package, so the subsequent additional control layers need to be placed under the controller

In addition, the view parser in Spring-servlet.xml has already controlled the files with the. JSP results as render pages and is placed under the web-inf/jsp directory, so you need to create the JSP folder in the Web-inf directory and create the user directory in the JSP folder. , create a login.jsp file in the user directory that is used to render a page request

Finally, enter Localhost:8080/bug.web/user/login validation results in the Address bar

Spring+spring Mvc+mybatis+maven Building Multi-module project (II.)

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.