1. 程式人生 > >Spring+Spring MVC+Mybatis+Maven搭建多模組專案(二)

Spring+Spring MVC+Mybatis+Maven搭建多模組專案(二)

基於第一篇文章《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; } }

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

為了方便,同時在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>

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

我的設計是把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);
    }
}

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

這裡返回的”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驗證結果