SSM配置文件
web.xml配置
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatchServlet绑定配置spring文件 默认是扫描WEB-INF包下的文件
classpath的原路径是resource
classpath*: 是到所有路径下找配置文件
建议使用classpath-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<!--/与/*的区别
/:只匹配所有的请求,不会匹配jsp页面(个人总结:只拦截路径不拦截文件(带有.jsp,.html,.css,等等))
/*:匹配所有请求,包括jsp界面,这样就会导致返回的结果jsp界面(拦截包括文件和路径)
会再去匹配servlet,应为没有做结束处理所以会一直一层一层的去匹配
形成循环结果报错!-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--过滤器 CharacterEncodingFilter 中有一个encoding 参数有get方法但是参数用@Nullable标记可以为空不是必给值
着个配置可以在springmvc中配置-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
SSM配置
- SpringDao配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--关联数据库文件-->
<context:property-placeholder location="classpath:db.properties" />
<!--连接池 mybatis和spring结合之前是写在mybatis中的-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.className}" />
<property name="jdbcUrl" value="${db.url}" />
<property name="password" value="${db.pwd}" />
<property name="user" value="${db.username}" />
<!--连接池的私有属性-->
<!--连接池最大连接数-->
<property name="maxPoolSize" value="30" />
<!--连接池最小连接数-->
<property name="minPoolSize" value="10" />
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false" />
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="10000" />
<!--当获取连接失败重试次数-->
<property name="acquireRetryAttempts" value="2" />
</bean>
<!--配置sqlSessionFactory 通过set注入-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!--声明映射文件的可以写在mybatis文件中-->
<!--<property name="mapperLocations" value="classpath:com/tv189/mapper/BookMapper.xml" />-->
</bean>
<!--配置mapper 接口扫描包,动态的实现了mapper接口 可以注入到spring容器中 接口是不能直接注入到容器中的通过这种方式可以注入
这里注入sqlSessionFactory的方式与使用SqlSessionDaoSupport获取SQLSession的方式相似(spring mybatis 配置中有)
因为MapperScannerConfigurer直接实现了InitializingBean而SqlSessionDaoSupport通过DaoSupport间接实现InitializingBean
实现原理通过basePackage找到接口 MapperScannerConfigurer 动态生成实现类 然后注入sqlSessionFactory
这里注入sqlSessionFactory与当时手写 继承SqlSessionDaoSupport的mapper接口实现类注入sqlSessionFactory手法相同-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!--要扫描的dao包-->
<property name="basePackage" value="com.tv189.mapper" />
</bean>
</beans>
- SpringService配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描service下面的包-->
<context:component-scan base-package="com.tv189.service" />
<!--项目中使用注解配置-->
<bean id="bookService" class="com.tv189.service.impl.BookServiceImpl">
<property name="bookMapper" ref="bookMapper" />
</bean>
<!--声明事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--aop横切事务-->
</beans>
3.SpringMVC配置
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.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">
<!--扫描包下的注解-->
<context:component-scan base-package="com.tv189.controller"/>
<!--注解驱动 相当于配置了映射器 处理适配器
着个配置相当于
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> -->
<mvc:annotation-driven />
<!--让springMVC不去处理静态文件 如:.css .js .html .mp3 .mp4 让这些文件不走下面的过滤-->
<mvc:default-servlet-handler />
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
4.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml" />
<import resource="spring-service.xml" />
<import resource="spring-mvc.xml" />
</beans>
5.mybatis 配置文件(在与spring结合之后mybatis的配置文件可以省略去,将mybatis中的配置全部写到spring中)
<?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>
<!--<properties resource="db.properties" />-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.tv189.pojo"/>
</typeAliases>
<!--配饰数据源 指定mapper.xml文件 与spring结合后由spring管理
在environments中可以有多个environment的它们用id区别
在写测试类是mybatis.xml文件加载时使用 default="development"指明使用的是哪个数据源-->
<!--<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${db.className}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.pwd}"/>
</dataSource>
</environment>
</environments>-->
<mappers>
<mapper resource="com/tv189/mapper/BookMapper.xml" />
</mappers>

浙公网安备 33010602011771号