ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

简单的SSM整合

2021-04-13 11:00:02  阅读:159  来源: 互联网

标签:xml web jdbc applicationContext springMvc SSM 整合 简单 org


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>
  • 配置文件的注解都写在文件里面了

标签:xml,web,jdbc,applicationContext,springMvc,SSM,整合,简单,org
来源: https://blog.csdn.net/c17852093445/article/details/115655333

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有