SSM整合配置文件
Spring
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--开启注解扫描 该spring配置文件只处理service和dao,controller不需要spring框架去处理--> <context:component-scan base-package="com.ccunix"> <!--不扫描controller层--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <context:property-placeholder location="classpath:db.properties"/> <!--c3p0数据库连接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="password" value="${jdbc.password}"/> <property name="user" value="${jdbc.username}"/> <property name="driverClass" value="${jdbc.driver}"/> </bean> <!--sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--两种方式 可以将对应的mapper.xml配置在mybatis-config.xml 或者是在dao包内 或者是放在resources下 --> <property value="classpath:mybatis-config.xml" name="configLocation"/> <!--<property value="classpath:com/ccunix/dao/*.xml" name="mapperLocations">--> <property value="classpath:mappers/*.xml" name="mapperLocations"/> </bean> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.ccunix.dao"/> </bean> <!--配置事务通知消息类型--> <tx:advice id="txManager" transaction-manager="transactionManager"> <tx:attributes> <!-- 增删改需要的内容--> <tx:method name="*" read-only="false" propagation="REQUIRED"/> <tx:method name="find*" read-only="true" propagation="SUPPORTS"/> <tx:method name="select*" read-only="true" propagation="SUPPORTS"/> <tx:method name="query*" read-only="true" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.ccunix.service.impl.*.*(..))" id="pt1"/> <aop:advisor pointcut-ref="pt1" advice-ref="txManager"/> </aop:config> <!--开启注解支持--> <context:annotation-config/> </beans>
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test2?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=123456
Mybatis
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 value="true" name="mapUnderscoreToCamelCase"/> <!--开启二级缓存--> <setting value="true" name="cacheEnabled"/> </settings> </configuration>
因为mybatis的配置都集合在了soring中了,所以里面的内容很少。
也可以在spring中配置驼峰标识和二级缓存,那样的话mybatis.xml就可以删除了
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> <property name="configuration"> <bean class="org.apache.ibatis.session.Configuration"> <property name="mapUnderscoreToCamelCase" value="true"/> </bean> </property> </bean>
SpringMVC
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描controller--> <context:component-scan base-package="com.ccunix.controller"/> <!--视图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> <!--上传文件--> <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"> <!--10*1024*1024 10M--> <property value="10485760" name="maxUploadSize"/> </bean> <!--拦截器--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/admin/**"/> <!--需要自己配置--> <bean class="com.ccunix.interceptor.AdminLoginInterceptor"/> </mvc:interceptor> </mvc:interceptors> <!--不过滤静态资源--> <mvc:default-servlet-handler/> <!--开启springMVC框架注解的支持--> <mvc:annotation-driven/> </beans>
web.xml
<web-app> <!--字符编码过滤器--> <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> <!--前端控制器--> <servlet> <servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--spring整合springmvc 配置Spring的监听器,默认只加载/WEB-INF/目录下的applicationContext.xml配置文件 如果配置文件在resources目录下 可以配置context-param标签进行说明 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--说明配置文件在哪里--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>

浙公网安备 33010602011771号