项目搭建结构:
父module, 持久层dao(mybatis依赖、Spring依赖、Mybatis+Spring整合)、业务层biz(事务:Aspectij依赖、AOP依赖)、表现层web(Servlet依赖、SpringMVC依赖)
表现层依赖业务层,业务层依赖持久层
持久层:dao、entity、global(数据字典类)、配置文件:数据源、Session工厂、映射器接口
业务层:biz(业务处理对象)、配置文件:事务(是一些接口方法) ,业务层一个个方法就是体现一个个功能,把业务层方法封装成一个个事务
表现层(处理发出的请求):controller(控制器)、dto(控制器和页面之间传递的信息)、global(表现层中间全局东西,如:拦截器,过滤器) 配置文件:静态资源处理、视图转换器、SpringMVC加载
*持久层(dao):
spring-dao.xml
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启自动扫描-->
<context:component-scan base-package="com.imooc.oa.dao"/>
<!--数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/oa?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--Session工厂-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.imooc.oa.entity"/>
</bean>
<!--映射器接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<property name="basePackage" value="com.imooc.oa.dao"/>
</bean>
</beans>``
业务层:
spring-biz.xml
<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"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--关联一下dao里边东西-->
<import resource="spring-dao.xml"/>
<!--自动扫描这个包com.imooc.oa.biz里面内容-->
<context:component-scan base-package="com.imooc.oa.biz"/>
<!--打开自动代理-->
<aop:aspectj-autoproxy/>
<!--声明事务管理器-->
<bean id="transationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--声明通知,指定事务管理器transationManager,切入点txAdvice-->
<tx:advice id="txAdvice" transaction-manager="transationManager">
<tx:attributes>
<!--切入点方法-->
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--把通知和切入点关联-->
<aop:config>
<aop:pointcut id="txpc" expression="execution(* com.imooc.oa.biz.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpc"/>
</aop:config>
</beans>
表现层:
编码过滤器代码:
只需把对应包名改变
package com.imooc.hosp.global;
import javax.servlet.*;
import java.io.IOException;
public class EncodingFilter implements Filter{
//初始话赋一个默认值
private String encoding ="UTF-8";
@Override
//过滤器初始化
public void init(FilterConfig filterConfig) throws ServletException {
//判断获取参数值是否为空,不为空则赋值
if(filterConfig.getInitParameter("ENCODING")!=null)
//filterConfig获取参数值赋给encoding
encoding = filterConfig.getInitParameter("ENCODING");
}
//过滤器响应
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//请求设置编码
servletRequest.setCharacterEncoding(encoding);
//响应这只编码
servletResponse.setCharacterEncoding(encoding);
//不拦截直接放行
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
//销毁为空
encoding=null;
}
}
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_3_1.xsd"
version="3.1">
<!--处理控制台中文乱码-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.imooc.oa.global.EncodingFilter</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>
<!-- 配置servlet,处理网页显示静态页面样式-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<!--按目录指定-->
<url-pattern>/assets/*</url-pattern>
<url-pattern>/js/*</url-pattern>
<url-pattern>/vendor/*</url-pattern>
<!--按后缀指定-->
<url-pattern>*.js</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.gif</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<!--SpringMVC加载-->
<servlet>
<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-web.xml</param-value>
</init-param>
<!--项目启动时自动加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-web.xml
<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">
<!--引入 spring-biz.xml-->
<import resource="spring-biz.xml"/>
<!-- 自动扫描包com.imooc.oa.controller-->
<context:component-scan base-package="com.imooc.oa.controller"/>
<mvc:annotation-driven/>
<!-- 打开静态资源设置规则,静态资源交给servlet处理-->
<mvc:default-servlet-handler/>
<!-- 视图转换器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--视图转换至少设置三个属性-->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!--配置路径的前缀-->
<property name="prefix" value="/WEB-INF/pages/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!--登录拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.imooc.oa.global.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>