SSM初步整合+简单的CRUD

本文介绍了SSM(Spring、SpringMVC、Mybatis)的初步整合过程,重点在于配置web.xml和Spring配置文件。分析了Spring作为整合者的作用,以及SpringMVC的执行流程。内容包括Controller、Service和DAO层的CRUD操作。
摘要由CSDN通过智能技术生成

1.配置web.xml

//注册Spring的全局配置文件
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/application.xml</param-value>
    </context-param>
    
    //ContextLoaderListener监听器注册ApplicationContext
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <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>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>DispacherServelt</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:conf/dispacherServlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispacherServelt</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

2.配置Spring配置文件

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
//数据库连接池
    <context:property-placeholder location="classpath:conf/jdbc.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
<!--    SqlsessionFactory-->
  //Session工厂
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:conf/mybatis.xml"/>
    </bean>
  //Mapper映射文件
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.bdj.dao"/>
    </bean>
//注解扫描
    <context:component-scan base-package="com.bdj.service"/>
</beans>
3.配置DispacherServlet调度器
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
//扫描Controller注解
    <context:component-scan base-package="com.bdj.controller"/>
//视图解析器
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
//开启注解驱动
    <mvc:annotation-driven/>
</beans>

SSM整合分析: Spring负责整合Mybatis Springmvc 感觉就像个大树干或者像个箱体 将其他散件整合到一起形成特定的功能 Spring和SpringMVC是父子容器 不需要整合就可以直接用Spring中的东西 Spring只用整合Mybatis即可.
SpringMVC执行流程:
从页面发来请求交给DispacherServlet处理 DispacherServlet将请求先交给HandleMapping处理器映射器,找到能处理该请求的处理器并将其封装为处理器执行链也就是拦截器和Controller的执行链又return给DispacherServlet,DispacherServlet根据处理器执行链中的处理器找到能执行该处理器的处理器适配器.HandleAdaptor处理适配器器执行Controller的方法并返回一个视图,中央调度器又将返回的视图交给视图解析器赋值路径 最终中央调度器调用视图完成响应.

下面是简单的CRUD

Controller层

public class MyController {

    @Resource
    StudentService ss;
    @RequestMapping("/search.do")
    @ResponseBody
    public List search(){
        List<Student> list=ss.search();
        return list;
    }

    @RequestMapping("/update.do")
    public ModelAndView update(Student student){
       int result=ss.update(student);
       ModelAndView mv=new ModelAndView();
       if(result>=1){
           mv.setViewName("success");
       }else{
           mv.setViewName("failed");
       }
       return  mv;

    }
    @RequestMapping("/delete.do")
    public ModelAndView delete(String name){
        System.out.println(name);
        int result=ss.deleteByName(name);
        ModelAndView mv=new ModelAndView();
        if(result>=1){
            mv.setViewName("success");
        }else{
            mv.setViewName("failed");
        }
        return  mv;

    }
    @RequestMapping("/insert.do")
    public ModelAndView  insert(Student student){

            int result=ss.insert(student);
        ModelAndView mv=new ModelAndView();
        if(result>=1){
            mv.setViewName("success");
        }else{
            mv.setViewName("failed");
        }
        return  mv;

    }
}

service层

import com.bdj.dao.StudentDao;
import com.bdj.domain.Student;
import com.bdj.service.StudentService;
import org.springframework.stereotype.Service;

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

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    StudentDao studentDao;

    public int insert(Student student) {
        int result=studentDao.insert(student);
        return result;
    }

    public List<Student> search() {
        List<Student> list=new ArrayList<>();
        list=studentDao.search();
        return list ;
    }

    public int deleteByName(String name) {
        int result=studentDao.deleteByName(name);
        return result;
    }

    public int update(Student student) {
        int result=studentDao.update(student);
        return result;
    }
}

dao层

<?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.bdj.dao.StudentDao">
    <select id="search" resultType="Student">
        select id,name,age from student
    </select>
    <delete id="deleteByName">
        delete from student where name=#{name}
    </delete>
    <insert id="insert">
        insert into student(id,name,age)values (
        #{id},#{name},#{age})
    </insert>
    <update id="update">
        update student set name=#{name},age=#{age} where id=#{id}
    </update>
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值