SSM框架整合

来源:互联网 发布:软件质量保证承诺书 编辑:程序博客网 时间:2024/05/20 03:39

一、导入jar包

这里写图片描述

项目目录

这里写图片描述

mybatis-config.xml配置

<?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="mapUnderscoreToCamelCase" value="true"/> -->        <!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题  -->        <setting name="cacheEnabled" value="true"/>        <setting name="lazyLoadingEnabled" value="true"/>        <setting name="aggressiveLazyLoading" value="false"/>    </settings> </configuration>

spring-servlet.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-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!-- 只扫描 控制器 -->    <context:component-scan base-package="com.mybatis" 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>    <mvc:annotation-driven></mvc:annotation-driven>    <mvc:default-servlet-handler/></beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>ssm_demo</display-name>  <!-- Spring配置     配置启动 Spring IOC 容器的 Listener -->    <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>  <!-- Spring MVC  配置 -->  <servlet>        <servlet-name>spring</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!-- <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:springmvc.xml</param-value>        </init-param> -->        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>spring</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

applicationContext.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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.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 希望控制所有的业务逻辑组件 -->    <context:component-scan base-package="com.mybatis">        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>    </context:component-scan><!-- 引入数据库的配置文件 -->    <context:property-placeholder location="classpath:dbconfig.properties" /><!-- Spring用来控制业务逻辑。数据源、事务控制、aop -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="jdbcUrl" value="${jdbc.url}"></property>        <property name="driverClass" value="${jdbc.driver}"></property>        <property name="user" value="${jdbc.username}"></property>        <property name="password" value="${jdbc.password}"></property>    </bean><!-- spring事务管理 -->    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"></property>    </bean><!-- 开启基于注解的事务 -->    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/><!--     整合mybatis         目的:1、spring管理所有组件。mapper的实现类。                service==>Dao   @Autowired:自动注入mapper;            2、spring用来管理事务,spring声明式事务    -->    <!--创建出SqlSessionFactory对象  -->        <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"></property>        <!-- configLocation指定全局配置文件的位置 -->        <property name="configLocation" value="classpath:mybatis-config.xml"></property>        <!--mapperLocations: 指定mapper文件的位置-->        <!-- <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property> -->    </bean>    <mybatis-spring:scan base-package="com.mybatis.dao"/>    <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.atguigu.mybatis.dao"></property>    </bean> --></beans>