ssm框架搭建(springMvc+spring+mybatis)

一、springMvc

1、新建空的maven项目

  1. 打包方式
<packaging>war</packaging> 
  1. 在 ProjectStructure -Modules下点击缺失的WEB-INF文件夹,系统将自动添加,之后在WEB-INF文件夹下新建web.xml文件

2、导包

  1. 使用统一的加包版本管理
<properties>
        <servlet.version>3.1.0</servlet.version>
        <spring.version>5.2.2.RELEASE</spring.version>
        <jsp.version>2.2.1</jsp.version>
        <jstl.version>1.2</jstl.version>
        <lombok.version>1.18.12</lombok.version>
        <mybatis.version>3.4.6</mybatis.version>
        <mybatis-spring.version>1.3.2</mybatis-spring.version>
        <druid.version>1.1.20</druid.version>
        <common.version>4.4.6</common.version>
        <mysql.version>5.1.47</mysql.version>
        <pagehelper.version>5.1.10</pagehelper.version>
    </properties>
  1. spring-webmvc、javax.servlet-api、javax.servlet.jsp-api(jsp)、jstl (jsp)、projectlombok.lombok(插件,自动添加setget方法等)、fastJson(前后端分离)

3、配置核心拦截器DispatcherServlet

在web.xml下配置它,需要同时配置spring配置文件的位置、加载时启动、拦截地址为‘/’(必须为‘/’,这样就不会拦截jsp,防止出现死循环)、utf-8过滤器。
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--配置核心拦截器  这里指定了springmvc.xml的位置   load-on-startup 必须要在最后才行-->
    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--上面必须是“/,如果用“ * ”就会连jsp都拦截,最后导致循环-->

    <!--配置监听器,让一运行时就读一下applicationContext.xml-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 编码过滤器  解决乱码(设置utf-8-->
    <filter>
        <filter-name>charsetFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>charsetFilter</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping>
</web-app>

4、 对应位置新建 springmvc.xml

注解驱动、配置controller扫描包、默认servlet、fastjson消息转化器(前后端分离)、view视图解析
jsp

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.0.xsd 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">
    <!--注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--配置默认servlet 防止核心拦截器拦截静态资源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <!--扫描java下的包,告诉它我将要写·的controller都在这个包下,他只看java包中有controller注解的类,然后看方法-->
    <context:component-scan base-package="com.nuedu.controller"></context:component-scan>
    <!--view视图解析区-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <!--后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

前后端分离(解决跨域)

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.0.xsd 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">
    <!--注解驱动-->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="false">
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=utf-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!--配置默认servlet 防止核心拦截器拦截静态资源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <!--解决跨域问题   必须是/**  表示可以无限层-->
    <mvc:cors>
        <mvc:mapping path="/**"/>
    </mvc:cors>
    <!--扫描java下的包,告诉它我将要写·的controller都在这个包下,他只看java包中有controller注解的类,然后看方法-->
    <context:component-scan base-package="com.neuedu.controller"></context:component-scan>


</beans>

二、springIOC

1、resources下新建applicationContex.xml

在其中配置扫描包,哪些包下类对象放入ioc容器中
applicationContex.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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.0.xsd 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">

    <!--扫描java下的包,,他只看java包中有对应注解的,然后将该类加入到IOC中-->
    <context:component-scan base-package="com.nuedu.controller"></context:component-scan>
    <context:component-scan base-package="com.nuedu.dao"></context:component-scan>
    <context:component-scan base-package="com.nuedu.service"></context:component-scan>
    <!-- datasource 连接池    id就是这个,别瞎写;注意转义;minIdle是池最小限制,maxActive是池最大;注意property -->
    <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url"
                  value="jdbc:mysql://localhost:3306/mybase?useUnicode=true&amp;characterEncoding=utf-8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
        <property name="minIdle" value="8"></property>
        <property name="maxActive" value="20"></property>
    </bean>
    <!-- sqlSessionFactory    第一个参数就是上面配置的连接池的id,表明该工厂是基于druid建立的 -->
    <!--参数typeAliasesPackage 配置包的别名  Mapper中pojo中的类不用写前缀了-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource"></property>
        <property name="typeAliasesPackage" value="com.nuedu.pojo"></property>
        <property name="plugins">
            <array>
            <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                        <props>
                            <prop key="reasonable">true</prop>
                            <prop key="helperDialect">mysql</prop>
                        </props>
                </property>
            </bean>
            </array>
        </property>
    </bean>
    <!-- 管理 mapper 代理对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.nuedu.dao"></property>
    </bean>

</beans>

2、web.xml下配置文件监听器,在启动时读取applicationContex.xml,并配置其位置。

具体代码见上面web.xml

3、对应包中类(接口不能,实现类才行)放注解,表示加入到ioc容器中

    @Service   //服务层
    @Component   //未命名
    @Controller  //控制层
    @Configuration  //配置层
    @Repository    //仓库

三、mybatis

1、导包

  • mybatis
  • mybatis-plus
  • springORM(与spring版本一致)
  • mysql (与自己数据库版本匹配)
  • druid (德鲁伊连接池)

2、在applicationContext.xml下配置数据库连接相关的东西

  • 配置数据库连接
  • 配置SqlSessionFactory
  • 配置mapperScanner (之后要在resources下建立与java中mapper/dao层同路径的映射xml文件)
  • 具体代码在上面applicationContext.xml

四、编写代码

  • 新建包 controller、mapper、service/impl、pojo
  • 在resources下建立与java中mapper/dao层同路径的映射xml文件
    XXXMapper.xml / XXXDao.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文件的路径和文件名  必须和 dao接口 路径和文件名一致
namespace 必须是dao接口的完全限定名
id 必须对应dao接口中的方法名-->
<!--resultType是泛型T的全限定名,由于前面applicationContext配置了别名,所以只用写类名;例如List<User> 则为User-->
<mapper namespace="com.nuedu.dao.UserDao">
    <select id="list" resultType="User" parameterType="User">
        select id,username,password from user
        <where>
            <if test="username != null">
                username like concat('%',#{username},'%')
            </if>
        </where>
    </select>
    <insert id="add" parameterType="User">
        insert into user(username,password) values(#{username},#{password})
    </insert>
    <delete id="del" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    </delete>
    <delete id="deee" parameterType="java.lang.Integer">
        delete from user where id in
        <foreach collection="array" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </delete>
    <select id="getById" parameterType="java.lang.Integer" resultType="User">
        select id,username,password from user where id=#{id}
    </select>
    <update id="update" parameterType="User">
        update user
            set
            username=#{username},
            password=#{password}
            where id = #{id}
    </update>
</mapper>

结构
XXXController.java

package com.nuedu.controller;

import com.nuedu.pojo.User;
import com.nuedu.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("/list")
    String list(User user, ModelMap mo){
        if(StringUtils.isBlank(user.getUsername())){
            user.setUsername(null);
        }else{
            mo.addAttribute("username",user.getUsername());
        }
        List<User> list=userService.list(user);
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        for(User users:list){
            System.out.println(users);
        }
        mo.addAttribute("users",list);

        return "user/list";
    }
    @GetMapping("/add")
    String add(){
        return "user/edit";
    }
    @PostMapping("/add")
    String add2(User user){
        userService.add(user);
        return "redirect:list";
    }
    @GetMapping("/del")
    String dele(Integer id){
        userService.del(id);
        return "redirect:list";
    }
    @PostMapping("/batchdel")
    String deee(Integer[] ids){
        userService.deee(ids);
        return "redirect:list";
    }
    @GetMapping("/update")
    String goUpdate(Integer id,ModelMap modelMap) {
        modelMap.addAttribute("user",userService.getById(id));
        return "user/edit";
    }
    @PostMapping("/update")
    String doUpdate(User user) {
        userService.update(user);
        return "redirect:list";
    }

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值