简单的SSM整合

1.需要的文件

applicationContext.xml,springMvc.xml,web.xml,mybatis-config.xml,db.properties

2.开始配置

2.1编写db.properties

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_demo
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root

2.2配置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"/>
    </settings>
    
    <typeAliases>
        <!--别名-->
        <package name="com.demo.domain"/>
    </typeAliases>

</configuration>

2.3编写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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="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.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/cache/spring-cache.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

    <!-- 加载数据库连接文件 -->
    <context:property-placeholder location="classpath:properties/db.properties"></context:property-placeholder>

    <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- spring整合mybatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定mybatis全局配置文件的位置-->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
        <property name="dataSource" ref="pooledDataSource"></property>
        <!-- 指定mybatis,mapper文件的位置-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>

    <!-- 扫描dao -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描所有的dao接口的实现,加入到ioc容器中-->
        <property name="basePackage" value="com.demo.dao"></property>
    </bean>

    <!-- 配置事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 控制数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 另一种是基于 @Transactional 注解的方式 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

2.4编写springMvc.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 扫描controller -->
    <context:component-scan base-package="com.demo" />

    <!-- 配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/pages"></property>
        <!--后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 开启springmvc注解支持 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
	<!--过滤静态资源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>

2.5编写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">

	<!--指定applicationContext.xml的位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定springMvc.xml的位置 -->
        <!--
        init-param
		设置 springmvc 配置文件位置以及名称,springmvc配置文件不设置默认位置是:webapp,
		可以使用 classpath 设置文件的名称为:spring-mvc.xml ,代表 springmvc 配置文件名必须为 spring-mvc.xml,
		如不使用 classpath 设置其路径及名称,默认在 webapp下,名称为:\标签中的值 + "-servlet.xml",例如:如 标签中值为:springmvc,则默认的 springmvc 配置文件名为:springmvc-servlet.xml
		-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springMvc.xml</param-value>
        </init-param>
        <!--
		设置 servlet加载时间,如不设置默认在第一次请求访问时加载 servlet,
		若设置此标签值为正整数,会将 servlet 的加载时间提前到项目启动时,
		此标签中可以写整数,但写负整数和0和没有设置是一样的效果,只有设置为正整数才会将 servlet 的加载时间提前到项目启动时,也就是 tomcat 启动时,
		值越小,代表优先级越高。
		-->
        <!--<load-on-startup>2</load-on-startup>-->
    </servlet>
  
    <!-- 代表拦截所有请求 -->
    <servlet-mapping>
        <servlet-name>springmvc</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>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
  • 配置文件的注解都写在文件里面了
上一篇:​BeanFactory和ApplicationContext的区别


下一篇:spring-aop-事务-注解开发-代理