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

}
  • 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验证结果

原文地址:https://www.cnblogs.com/zftxdh/p/7213805.html