Spring+SpingMVC+Mybatis 多模块项目搭建

项目模块创建部分

1、项目模块

                                     图1 项目模块

(1)ssm-demo:为项目名称 ,是一个maven项目(模块),ssm-demo-common、ssm-demo-dao、ssm-demo-servier是一个子maven模块,而lssm-demot-web是一个maven web模块。

注:common模块:放置工具类、静态变量等。

       dao模块:sql映射文件、mapper接口。

       service模块:service接口和实现类,处理器类(controller)。

       web模块:放置spring-context.xml、springMVC.xml、web.xml等文件和html/jsp页面。

 

(2)模块的创建过程 

 1)首先,创建项目,  file->new-project...->maven

 2)在项目中创建maven模块,例如创建ssm-demo-dao

剩下的ssm-demo-service、ssm-demo-commom的创建同上。

 3)  在项目中创建ssm-demo-service模块创建也类似,只需在下图中选中webapp,其他步骤相同

 

到这里项目各模块创建完毕,接下来进行框架配置。

 

 

框架配置部分

配置文件在web模块的如下位置

2、spring-context.xml 核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 加载数据库连接信息配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--druid连接池  -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>


    <!-- 配置Mybatis工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath*:dbmap/UserMapper.xml"/>
    </bean>

    <!-- Mapper-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ssm.demo.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

</beans>

3、springMVC.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 这个包下所有使用@controller   @service @repository等标识的类,都在spring 容器中生成一个bean   -->
    <context:component-scan base-package="com.ssm.demo">
    </context:component-scan>
    <!--    注解式处理器和映射器-->
    <mvc:annotation-driven/>

    <!--视图解析器-->
    <!-- 假设逻辑视图名为:hello,则查找视图  /views/hello.jsp  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

4、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         metadata-complete="true" version="3.0">
  <display-name>webapp</display-name>

  <!-- spring监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 指定spring核心配置文件 -->
  <!--  切面  数据持久层-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/spring-context.xml</param-value>
  </context-param>

  <!-- 配置前端控制器 -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:spring/*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern><!--/表示拦截所有请求 -->
  </servlet-mapping>

</web-app>

 

框架使用

5、数据库查询功能实现(使用例子)

ssm-demo的pom.xml中添加依赖

<dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlets</artifactId>
            <version>9.4.11.v20180605</version>
        </dependency>
        <!-- 链接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.7</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.liao</groupId>
            <artifactId>ssm-demo-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

 

ssm-demo-web 的pom.xml 添加依赖

<dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlets</artifactId>
            <version>9.4.11.v20180605</version>
        </dependency>
        <!-- 链接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!--引用ssm-demo-service模块-->
        <dependency>
            <groupId>com.liao</groupId>
            <artifactId>ssm-demo-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

 

 

(1)数据库配置文件和表

jdbc.propertis(位于web模块的resource)

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=false
jdbc.username=你的用户名
jdbc.password=你的密码

表user

 

(2)dao模块代码

 

UserEntity.java

package com.ssm.demo.entitis;

public class UserEntity {
    private int id;
    private String name;
    private String city;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "UserEntity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}

UserMapper接口


package com.ssm.demo.mapper;


import com.ssm.demo.entitis.UserEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper {

    List<UserEntity> getAllUser();

}

UserMapper.xml

<?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.ssm.demo.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.ssm.demo.entitis.UserEntity">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="city" property="city" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="getAllUser" resultMap="BaseResultMap">
        select * from user
    </select>

</mapper>

 

(3)service模块代码

 

UserService接口

package com.ssm.demo.service.ServiceInterface;


import com.ssm.demo.entitis.UserEntity;

import java.util.List;

public interface UserService {
    List<UserEntity> getAll();


}

UserServiceImpl.java

package com.ssm.demo.service;


import com.ssm.demo.entitis.UserEntity;
import com.ssm.demo.mapper.UserMapper;
import com.ssm.demo.service.ServiceInterface.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    public List<UserEntity> getAll() {
        return userMapper.getAllUser();

    }
}

 

TestController.java
package com.ssm.demo.controller;


import com.ssm.demo.entitis.UserEntity;
import com.ssm.demo.service.ServiceInterface.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class TestController {
    @Autowired
    private UserService userService;

    /*
     * 查询user表的所有信息
     *通过mode将查询结果返回,
     * index为逻辑视图名,将解析为/views/index.jsp
     * */
    @RequestMapping("all")
    public String showUser(Map mode) {
        List<UserEntity> userEntityList = userService.getAll();
        mode.put("userEntityList", userEntityList);
        return "index";
    }

}

 

 

(4)index.jsp位置

 

  index.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>${requestScope.userEntityList}</h3>
</body>
</html>

 

 

在浏览器中输入:http://localhost:8080/user/all      即可显示数据库返回的数据

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值