关于SSM框架搭建的相关配置(三)

本文详细介绍了如何在Java项目中整合Spring MVC、MyBatis和数据库连接池,包括依赖jar包配置、web.xml文件配置、编码过滤器、前端控制器配置、视图解析器设置、基于注解的映射器和处理器、dao层、service层、mapper接口、SQL映射文件等内容。通过整合这些组件,实现了一个基于SSM框架的项目开发流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,依赖jar包:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tedu</groupId>
<artifactId>day08_ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
    <!-- spring 的依赖jar包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>
    <!-- junit测试jar包 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <!-- 数据库的连接池 -->
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <!-- mysql数据库 -->
    <dependency>
        <groupId>MySQL</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <!-- mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.5</version>
    </dependency>
    <!-- mybatis-spring整合 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- jstl -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- spring-jdbc的依赖jar包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>
</dependencies>

2,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_2_5.xsd" version="2.5">
  <display-name>day08_ssm</display-name>
<!--  1.配置Listener 读取application-*.xml  -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--  2.Filter 配置编码格式  -->
<filter>
<filter-name>filter</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>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--  3.Servlet 前端控制器  -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

3,db.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8
username=root
password=

4,spring-mvc.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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

  <!--组件扫描 -->
  <context:component-scan base-package="cn.tedu.ssm.controller"/>

  <!-- 配置视图解析器 -->
  <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/web/"/>
    <property name="suffix" value=".jsp"/>      
  </bean>
  <!-- 
    1.基于注解的映射器默认是:DefaultAnnotationHandlerMapping,映射处理器是2.5版本的;
    2.3.2版本定义一个新的映射处理器:RequestMappingHandlerMapping
    3.如果要改变默认的映射处理器,使用下面的配置
    4.默认初始化一些工具类:比如异常处理,解析json
   -->
  <mvc:annotation-driven/>
</beans>

5,application-dao.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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

  <!-- 
    1.util:properties表示读取外部的属性文件,并实例化对象
    2.id表示名称
    3.location表示属性文件的位置
   -->
  <util:properties id="dbConf" location="classpath:db.properties"/>

  <!-- 配置数据库的连接池 
        1.使用spring表达式给属性赋值 
        2.spring表达式语法格式:#{}
  -->
  <bean id="dataSource" 
  class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" 
              value="#{dbConf.driverClassName}"/>
    <property name="url" 
              value="#{dbConf.url}"/>
    <property name="username" 
              value="#{dbConf.username}"/>
    <property name="password" 
              value="#{dbConf.password}"/>
  </bean>
  <!-- ssm 整合 -->
  <!-- 持久层接口的扫描 -->
  <bean id="scannerConfigurer"
        class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.tedu.ssm.dao"/>
  </bean>

  <!-- SqlSessionFactoryBean的初始化 -->
  <bean id="factoryBean"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 依赖注入数据源(dataSource) -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 读取编写sql语句的映射文件 -->
        <property name="mapperLocations"
                  value="classpath:mappers/*.xml"/>
  </bean>     
</beans>

6,application-service.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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

  <!-- 扫描包 可以扫描到当前包和子包下的所有类   -->
  <context:component-scan base-package="cn.tedu.ssm.service"/>
</beans>

7,Mapper.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" 
        "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
    <!-- 
        1.namespace表示命名空间 
        2.必须为接口名
     -->
    <mapper namespace="cn.tedu.ssm.dao.DeptDao">
    <!-- void insertDept(Dept dept); -->
    <!-- 添加部门信息 -->
    <insert id="insertDept" parameterType="cn.tedu.ssm.bean.Dept">
        insert into t_dept(
            dept_name,dept_loc
        )values(
            #{deptName},#{deptLoc}
        )
    </insert>
    <!-- List<Dept> selectDeptAll() -->
    <!-- 查询用户信息 -->
    <select id="selectDeptAll" resultType="cn.tedu.ssm.bean.Dept">
        select
            id,
            dept_name deptName,
            dept_loc deptLoc
        from
            t_dept      
    </select>
    <!-- void deleteDeptById(Integer id); -->
    <delete id="deleteDeptById">
        delete from t_dept 
        where 
            id=#{id}    
    </delete>
    <!-- Dept selectDeptById(); -->
    <select id="selectDeptById" resultType="cn.tedu.ssm.bean.Dept">
    select
            id,
            dept_name deptName,
            dept_loc deptLoc
        from
            t_dept  
        where 
            id=#{id}
    </select>
    <!-- void editDept(Dept dept); -->
    <update id="editDept">
        update t_dept set
             dept_name=#{deptName},
             dept_loc=#{deptLoc}
        where id=#{id}  
    </update>
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值