当前位置: 首页>后端>正文

SSM整合--->Spring+SpringMVC+Mybaits

一:导包

1.Spring:

【AOP核心包】
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar

【IOC核心包】
commons-logging-1.1.3.jar
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jarnn
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar

【JDBC核心】
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar

【测试】
spring-test-4.0.0.RELEASE.jar

2·SpringMVC:

【上传·下载】
commons-fileupload-1.2.1.jar
commons-io-2.0.jar

【jstl-->jsp标准标签库】
jstl.jar
standard.jar

【SpringMVC核心】
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

【数据校验】
hibernate-validator-5.0.0.CR2.jar
hibernate-validator-annotation-processor-5.0.0.CR2.jar
classmate-0.8.0.jar
jboss-logging-3.1.1.GA.jar
validation-api-1.1.0.CR1.jar

【Ajax】
jackson-annotations-2.1.5.jar
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar

3·mybaits :

【Mybatis核心包】
mybatis-3.4.1.jar

【ehcache整合】
mybatis-ehcache-1.0.3.jar
ehcache-core-2.6.8.jar
log4j-1.2.17.jar
slf4j-api-1.7.21.jar
slf4j-log4j12-1.7.21.jar

4.其他jar包 :

【数据库 驱动】
c3p0-0.9.1.2.jar
mysql-connector-java-5.1.37-bin.jar
mybatis-spring-1.3.0.jar

二:配置

项目—目录结构:


SSM整合--->Spring+SpringMVC+Mybaits,第1张
ssm项目结构图

1.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" id="WebApp_ID" version="3.0">
  <display-name>SSM</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置  Spring容器 -->
  <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 配置spring文件位置 -->
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  
  
  <!-- 配置springMVC的前端控制器 -->
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/applicationContext-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- 两个标准配置 -->
    <!--  字符编码 -->
    <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>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>org.springframework.web.filter.CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- 支持Rest风格的过滤器 -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- Spring 扫描除 控制器之外 的组件 包括DAO Service  -->
    <context:component-scan base-package="com.item">
    <!-- 如果设置排除规则  则无需添加 use-default-filters="false" -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 1.导入外部文件 -->
    <context:property-placeholder location="classpath:dbConfig.properties"/>
    <!-- 2.配置数据源  引入外部文件-->
    <bean id="ComboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    </bean>
    
    <!-- 3.配置jdbcTemplate 被Mybaits代替 -->
    <!-- 3.配置使用Mybatis操作数据库 -->
    <!-- 可以根据配置文件得到 sqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置配置文件位置 -->
        <property name="configLocation" value="classpath:mybaits/mybaits-config.xml"></property>
        <!-- 控制数据源 -->
        <property name="dataSource" ref="ComboPooledDataSource"></property>
        <!-- 指定 mapper的位置 路径 -->
        <property name="mapperLocations" value="classpath:mybaits/mapper/*.xml"></property>
    </bean>
    
    <!-- 把每个dao接口的实现 加入到ioc容器中 -->
    <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定dao接口所在的包 -->
        <property name="basePackage" value="com.item.dao"></property>
    </bean>
    
    <!-- 4.配置tx事务控制;配置事务管理器 让它控制数据源里面链接的关闭和提交 -->
    <bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ComboPooledDataSource"></property>
    </bean>
    
    <!-- 5.基于xml配置 事务;哪些方法切入事务还要写切入点表达式 -->
    <aop:config>
        <!-- execution(* com.item.services.*.*(..)) 
            1*:表示任意修饰符
            2*:任意类
            3*:任意方法
            4..:任意参数
         -->
         <!-- 配置切入店 表达式 -->
        <aop:pointcut expression="execution(* com.item.services.*.*(..))" id="txPoint"/>
        <aop:advisor advice-ref="txInfo" pointcut-ref="txPoint"/>
    </aop:config>
    
    <!-- 6.配置事务增强 事务属性 -->
    <tx:advice id="txInfo"  transaction-manager="tm">
        <!-- 配置事务属性 -->
        <tx:attributes>
            <!-- 如果 将所有方法配置为事务方法 -->
            <tx:method name="*" rollback-for="java.lang.Exception"/>
            <!-- 配置 具体某个事务方法 -->
            <tx:method name="insertEmp" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 注解版 配置事务管理器 -->
    <!-- 1:配制事务管理器进行事务控制-->
<!--    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> -->
    <!-- 控制数据源  -->
<!--        <property name="dataSource" ref="dataSource" ></property> -->
<!--    </bean> -->
    <!-- 2:开启基于注解的事务控制模式:依赖tx名称空间 -->
<!--    <tx:annotation-driven transaction-manager="transactionManager"/> -->
    <!-- 3:给事务方法加注解  @Transactionl-->
</beans>

3.SpringMVC配置:

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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-4.0.xsd">
    
    <!-- SpringMVC只扫描控制器:禁用默认规则 -->
    <!-- 扫描包名  按实际项目填写 -->
    <context:component-scan base-package="com.item" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 返回值页面 前缀 和 后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property> 
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 配置文件上传 -->
    <!--  id: DispatcherServlet——>MULTIPART_RESOLVER_BEAN_NAME——>multipartResolver  -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>    
        <!-- 指定所上传文件的总大小不能超过1G。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->    
        <property name="maxUploadSize" value="1073741824"/>  
        <!--resolveLazily属性启用是为了推迟文件解析,以便在UploadController 中捕获文件大小异常-->    
        <property name="resolveLazily" value="true"/>   
        <property name="maxInMemorySize" value="4096"></property>
    </bean>
    
    <!-- 处理静态资源 -->
    <mvc:default-servlet-handler/> <!-- 将静态资源的处理经由Spring MVC框架交回Web应用服务器处理 -->
    <mvc:annotation-driven></mvc:annotation-driven> <!-- 表示:注解驱动开发 -->
</beans>

4.Mybaits配置:

(1).Spring整合-Mybaits: 添加在Spring 配置文件中

    <!-- 3.配置jdbcTemplate 被Mybaits代替 -->
    <!-- 3.配置使用Mybatis操作数据库 -->
    <!-- 可以根据配置文件得到 sqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置配置文件位置 -->
        <property name="configLocation" value="classpath:mybaits/mybaits-config.xml"></property>
        <!-- 控制数据源 -->
        <property name="dataSource" ref="ComboPooledDataSource"></property>
        <!-- 指定 mapper的位置 路径 -->
        <property name="mapperLocations" value="mybaits/mapper/*.xml"></property>
    </bean>
    
    <!-- 把每个dao接口的实现 加入到ioc容器中 -->
    <bean id="" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定dao接口所在的包 -->
        <property name="basePackage" value="com.item.dao"></property>
    </bean>

(2).Mybaits配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings> 
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
        <setting name="cacheEnabled" value="true"/>
    </settings>
</configuration> 

(3).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">
  <!-- namespace:接口的全类名 ,相当于告诉配置文件是实现哪个接口的;
    id:方法名,相当于配置是对某个方法的实现
    resultType:返回值类型(如果为查询操作 必须指定)
    #{属性名}:代表取出传递过来的某个参数的值
  -->
<mapper namespace="com.item.dao.ItemDao">
    <resultMap type="com.item.bean.Item" id="itemMap">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="password" column="password" jdbcType="VARCHAR"/>
    </resultMap>
    
<!--举例方法:  public Item getItemById(String id); -->
  <select id="getItemById" resultType="com.item.bean.Item">
    select * from M_TEST where id = #{id}
  </select>
</mapper>

三:测试

(1).index.jsp页面发送get请求 跳转success 获取参数
<a href="getItemById?id=001">查询数据</a>

(2).查看success是否成功显示

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>成功</h1>
    ${item}
</body>
</html>

https://www.xamrdz.com/backend/3qq1944804.html

相关文章: