您的位置:首页 > 其它

【SSM框架】SSM整合的配置文件

2019-02-15 09:09 204 查看

JDBC数据库配置文件

文件名:jdbc.properties

文件路径:src/main/resources/jdbc.properties

文件内容:

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql:///ssmcrud?useSSL=true

jdbc.username=root

jdbc.password=123456

web.xml核心配置文件

文件名:web.xml

路径:src/main/webapp/WEB-INF/web.xml

配置内容:

1. 文件头

<?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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd

id=“WebApp_ID” version=“2.5”>

2. 注册spring配置文件的位置

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

3. 注册servletcontext监听器

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

4. 注册字符集过滤器

<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>forceRequestEncoding</param-name>

<param-value>true</param-value>

</init-param>

<init-param>

<param-name>forceReponseEncoding</param-name>

<param-value>true</param-value>

</init-param>
<filter-name>CharacterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

5. 配置springMVC中央调度器

<servlet-name>springmvc</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-name>springmvc</servlet-name>

<url-pattern>/</url-pattern>

6. 配置Rest风格的URI(可选)

<filter-name>HiddenHttpMethodFilter</filter-name>

<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
<filter-name>HiddenHttpMethodFilter</filter-name>

<url-pattern>/*</url-pattern>

7. 解决Tomcat对PUT请求不封装数据体的问题(可选)

<filter-name>HttpPutFormContentFilter</filter-name>

<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
<filter-name>HttpPutFormContentFilter</filter-name>

<url-pattern>/*</url-pattern>

Spring核心配置文件

文件名:applicationContext.xml

路径:/src/main/resource/applicationContext.xml

配置内容:

1. 文件头

<?xml version="1.0"encoding="UTF-8"?>

<beans xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance

xmlns=“http://www.springframework.org/schema/beans

xmlns:context=“http://www.springframework.org/schema/context

xmlns:aop=“http://www.springframework.org/schema/aop

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.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

2. 数据源配置

<context:property-placeholder location=“classpath:jdbc.properties”/>

<bean name="C3P0DataSource"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>

3. 业务注册

<context:component-scan base-package=“com.crud.service” />

  1. 整合Mybatis

<bean id="sqlSessionFactory"class=“org.mybatis.spring.SqlSessionFactoryBean”>

<!-- 指定mybatis主配置文件的位置 -->

<property name="configLocation"value="classpath:mybatis.xml"/>

<!-- 连接池注入 -->

<property name="dataSource"ref="C3P0DataSource"/>

<!-- 指定mapper文件的位置 -->

<property name="mapperLocations"value="classpath:mapper/*.xml"/>
<property name="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>

<!-- 扫描所有的DAO接口加入到IOC容器中 -->

<property name="basePackage"value="com.crud.dao" />

5. 配置批量执行的sqlsession(可选)

<bean id="sqlSession"class=“org.mybatis.spring.SqlSessionTemplate”>

<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />

<constructor-arg name="executorType" value="BATCH" />

6. 事务配置

<bean id=“transactionManager”

class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>

<!-- 控制住数据源 -->

<property name="dataSource"ref="C3P0DataSource"/>

<tx:advice id="transactionAdvice"transaction-manager=“transactionManager”>

<tx:attributes>

<!-- 所有方法都是事务方法 -->

<tx:method name="*" isolation="DEFAULT"propagation="REQUIRED"/>

<!-- get查询方法为只读,优化查询效率 -->

<tx:method name="get*" read-only="true"/>

</tx:attributes>

</tx:advice>

aop:config

<aop:pointcut expression="execution(* com.crud.service..*(..))"id="myPointCut"/>

<aop:advisor advice-ref="transactionAdvice" pointcut-ref="myPointCut"/>

</aop:config>

SpringMVC核心配置文件

文件名:spring-mvc.xml

路径:/src/main/resources/spring-mvc.xml

配置内容:

1. 表头

<?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:p="http://www.springframework.org/schema/p

xmlns:context=“http://www.springframework.org/schema/context

xmlns:mvc=“http://www.springframework.org/schema/mvc

xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd">

2. 配置组件扫描器

<context:component-scan base-package=“com.crud.controller” />

3. 配置视图解析器

<!-- 前缀 -->

<property name="prefix"value="/WEB-INF/views/" />

<!-- 后缀 -->

<property name="suffix"value=".jsp" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: