1. 程式人生 > >Spring+Spring MVC+Mybatis+Maven搭建多模塊項目(二)

Spring+Spring MVC+Mybatis+Maven搭建多模塊項目(二)

自己 var user inf 接口 work 過程 cal ber

基於第一篇文章《Spring+Spring MVC+Mybatis+Maven搭建多模塊項目(一)》的基礎上,寫一個完整的示例,從頁面到Dao層的整個過程 1、先在bug.model模塊下創建com.bug.model.user包,在包中創建UserVO對象

package com.bug.model.user;

public class UserVO {
    private String userId;
    private String userName;
    private String password;

    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;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

2、在bug.dao模塊中創建com.bug.dao.user包,在包中創建IUserDao接口

package com.bug.dao.user;

import org.apache.ibatis.annotations.Param;

import com.bug.model.user.UserVO;

public interface IUserDao {
    void addUser(@Param("user")UserVO user);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

為了方便,同時在com.bug.dao.user包中創建IUserDao接口對應的Mapper文件IUserDao.xml,其實在applicationContext.xml文件中也已經指定了Mapper文件的存放位置了

<?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、在bug.service模塊中創建com.bug.service.user包,在包中創建IUserServicer接口

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

我的設計是把Service接口及其實現都放到bug.service模塊中,因此在user包下再創建impl子包,在子包中創建IUserService接口的實現UserServiceImpl,並在接口中調用Dao層接口

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
    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、在bug.web模塊下創建com.web.controller.user包,在包中創建UserController控制層,用於響應頁面發送過來的請求,並調用Service層的接口

package com.bug.controller.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bug.model.user.UserVO;
import com.bug.service.user.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    public String login() {
        UserVO user = new UserVO();
        user.setUserId("1");
        user.setUserName("test1");
        user.setPassword("123456");

        try {
            userService.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
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

這裏返回的”user/login”,渲染器會自己匹配WEB-INF/jsp/user目錄下的login.jsp, 由於在spring-servlet.xml中已經限制掃描範圍,所有的控制層都必須放在com.web.controller包下,因此後繼增加的其他控制層都需要放到controller下面

另外spring-servlet.xml中的視圖解析器已經控制了以.jsp結果的文件作為渲染頁面,並放在WEB-INF/jsp目錄下面,因此需要先在WEB-INF目錄下創建jsp文件夾,再在jsp文件夾中創建user目錄,在user目錄中創建login.jsp文件,用於渲染頁面的請求 技術分享

最後在地址欄中輸入localhost:8080/bug.web/user/login驗證結果

Spring+Spring MVC+Mybatis+Maven搭建多模塊項目(二)