SSM配置文件详情
1.springmvc框架
springmvc.xml 配置文件
- 包扫描(扫描的是ioc注解和依赖注入的注解)
- 加载mvc注解驱动
- 配置springmvc的各种组件:视图解析器、文件解析器、异常处理器、类型转换器、拦截器等等
- 配置静态资源由DefaultServlet处理 <mvc:default-servlet-handler />
- 导入其它的配置文件(spring-service)
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.itheima.controller"/>
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 静态资源过滤 -->
<mvc:default-servlet-handler/>
<!-- 导入service -->
<import resource="classpath:spring-service.xml"/>
</beans>
2.web.xml
1. web.xml,前端控制器,里面是配置Servlet和Filter、Listener
2. 配置 DispatcherServlet
3. 配置解决乱码的过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- 乱码过滤器 -->
<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>
<init-param>
<!-- 不管是请求还是响应都为utf-8 -->
<param-name>forceEncoding</param-name>
<param-value>true</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>
<!-- / 与 *.do
/ 返回页面 jsp default-servlet-handler才有效
*.do ajax json 不需要default-servlet-handler
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.spring框架
spring的配置文件
1. aop的配置
2. 声明式事务的配置(xml配置和注解方式配置)
2.1xml配置方式:
配置事务管理器
配置事务建议
配置事务的AOP
2.2 注解方式配置:
配置事务管理器
开启事务注解支持
在代码中使用 Transactional注解来标 注,哪个方法需要使用事务
3. 其它的一些ioc和依赖注入的配置
4. 导入其它的配置文件(spring-mybatis.xml整合的配置文件)
spring-service.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- xml配置文件配置声明式事务和注解方式配置声明式事务 -->
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- advice -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception" />
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception" />
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:pointcut id="myPoint" expression="execution(* com.itheima.service..*.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="myPoint"/>
</aop:config>
<!-- 注解事务支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 扫service包 -->
<context:component-scan base-package="com.itheima.service"/>
<!-- 导入dao的配置文件 -->
<import resource="classpath:spring-mybatis.xml"/>
</beans>
3.Mybatis框架
spring-mybatis.xml
1. 配置dataSource数据源(引入properties配置文件)
2. spring整合mybatis的SqlSessionFactory工厂
2.1 注入数据源
2.2 批量别名配置
3. 包扫描(扫描的是dao包下的所有接口,将代理对象交给spring容器管理)
spring整合mybatis配置文件
spring-mybatis.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">
<!--引入properties配置文件-->
<context:property-placeholder location="classpath:druid.properties"/>
<!--1. 配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
</bean>
<!--2. 配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--1.注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--2.批量配置别名-->
<property name="typeAliasesPackage" value="com.itheima.pojo"></property>
</bean>
<!--3. 配置MapperScannerConfigurer-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.dao"></property>
</bean>
</beans>
mybatis映射文件
映射文件步骤:
1.@映射配置文件存储的路径在resources里面
@要和对应的Dao接口的路径保持一致(且以/创建文件)
@映射配置文件的文件名必须和Dao接口名保持一致
2.mapper根标签,namespace属性的值,要和对应的Dao接口的全限定名一致
3. 执行操作sql语句:
查询方法就对应select标签
添加方法就对应insert标签
删除方法就对应delete标签
修改方法就对应update标签
AccountDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
//引入包的全限定名
<mapper namespace="com.itheima.dao.AccountDao">
//增加
<insert id="add" parameterType="Account">
insert into account(name,money) values (#{name},#{money})
</insert>
//修改
<update id="update" parameterType="Account">
update account set name=#{name},money=#{money} where id=#{id}
</update>
//删除
<delete id="deleteById" parameterType="int">
delete from account where id=#{id}
</delete>
//查询所有
<select id="findAll" resultType="Account">
select * from account
</select>
//id查询
<select id="findById" resultType="Account" parameterType="int">
select * from account where id=#{id}
</select>
</mapper>