框架整合思路:
一、Mapper层:整合mybatis和spring
需要的jar包:
1、mybatis的jar包
2、Mysql数据库驱动
3、数据库连接池
4、Mybatis和spring的整合包。
5、Spring的jar包
配置文件:
1、mybatis的配置文件:SqlMapConfig.xml
2、Spring的配置文件:applicationContext-dao.xml
数据源
1、数据库连接池
2、配置SqlSessionFactory(mybatis和spring整合包中的)
3、配置mapper文件的扫描器。
二、Service层:
使用的jar包:spring的jar包。
配置文件:applicationContext-service.xml
配置一个包扫描器,扫描所有带@Service注解的类。
事务配置:
配置文件:applicationContext-trans.xml
1、配置一个事务管理器
2、配置tx
3、配置切面
三、表现层
使用springmvc,需要使用springmvc和spring的jar包。
配置文件:springmvc.xml
1、配置注解驱动
2、配置一个视图解析器。
3、包扫描器,@Controller注解
Web.xml
1、配置springmvc的前端控制器
2、Spring容器初始化的listener。
部分配置文件:
1/web.xml文件配置:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="yc" version="2.5"> <display-name>yc-manager</display-name> <!-- 初始化spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 日志配置文件 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:properties/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 解决post乱码 --> <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> <!-- springmvc的前端控制器 --> <servlet> <servlet-name>yc-manager</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>yc-manager</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--core中的web.xml --> <context-param> <param-name>webAppRootKey</param-name> <param-value>manager.root</param-value> </context-param> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 错误页面 --> <error-page> <error-code>403</error-code> <location>/WEB-INF/jsp/403.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/WEB-INF/jsp/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/jsp/500.jsp</location> </error-page> <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/WEB-INF/jsp/error.jsp</location> </error-page> </web-app>
2/SqlMapConfig.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 name="mapUnderscoreToCamelCase" value="true" /> <!-- 设置二级缓存 --> <setting name="cacheEnabled" value="true" /> <!-- 打印查询语句 --> <setting name="logImpl" value="SLF4J" /> <!-- --> <setting name="defaultExecutorType" value="REUSE" /> <!-- 指定当结果集中值为 null 的时候是否调用映射对象的 setter(map 对象时为 put)方法 --> <setting name="callSettersOnNulls" value="true" /> <!-- 延迟加载 --> </settings> <typeAliases> <package name="com.yc.pojo" /> </typeAliases> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!--分页参数合理化 --> <property name="reasonable" value="true" /> </plugin> </plugins> </configuration>
3/db.properties配置文件:
#**************************Oracle********************************** jdbc.driver=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc:oracle:thin:@10.22.1.245:1521:orcl jdbc.username=zsyc jdbc.password=zsyc filters=stat #最大并发连接数 maxActive=20 #初始化连接数量 initialSize=1 #配置获取连接等待超时的时间 maxWait=60000 #最小空闲连接数 minIdle=10 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis=60000 #配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis=300000 #用来检测连接是否有效的sql,要求是一个查询语句。 validationQuery=SELECT 'x' testWhileIdle=true testOnBorrow=false testOnReturn=false #是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。 poolPreparedStatements=false #打开PSCache,并且指定每个连接上PSCache的大小 maxOpenPreparedStatements=20
4/log4j.properties
#off/fatal/error/warn/info/debug/all log4j.debug=false log4j.rootLogger=info,stdout,log,errorlog # Console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n ### Log ### log4j.appender.log = org.apache.log4j.DailyRollingFileAppender log4j.appender.log.File = ../logs/yc-manager-web.log log4j.appender.log.Append = true log4j.appender.log.Threshold = DEBUG log4j.appender.log.DatePattern='.'yyyy-MM-dd log4j.appender.log.layout = org.apache.log4j.PatternLayout log4j.appender.log.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %C{1}.java :%L(%M)] %m%n ### Error ### log4j.appender.errorlog = org.apache.log4j.DailyRollingFileAppender log4j.appender.errorlog.File = ../logs/yc-manager-web.error.log log4j.appender.errorlog.Append = true log4j.appender.errorlog.Threshold = ERROR log4j.appender.errorlog.DatePattern='.'yyyy-MM-dd log4j.appender.errorlog.layout = org.apache.log4j.PatternLayout log4j.appender.errorlog.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %C{1}.java :%L(%M)] %m%n #Spring logging configuration log4j.category.org.springframework = warn
5/applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:properties/*.properties"/> <!-- 数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="filters" value="${filters}" /> <property name="maxActive" value="${maxActive}" /> <property name="initialSize" value="${initialSize}" /> <property name="maxWait" value="${maxWait}" /> <property name="minIdle" value="${minIdle}" /> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /> <property name="validationQuery" value="${validationQuery}" /> <property name="testWhileIdle" value="${testWhileIdle}" /> <property name="testOnBorrow" value="${testOnBorrow}" /> <property name="testOnReturn" value="${testOnReturn}" /> <property name="poolPreparedStatements" value="${poolPreparedStatements}" /> <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" /> </bean> <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <!-- 配置SqlsessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载mybatis的配置文件 --> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置包扫描器,扫描mapper接口生成代理对象放到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定要扫描的包 --> <property name="basePackage" value="com.yc.mapper"/> </bean> </beans>
6/applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置包扫描器,扫描@Service主键的类 --> <context:component-scan base-package="com.yc.service"/> </beans>
7/applicationContext-trans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 事务配置 --> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- 切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.yc.service.*.*(..))" /> </aop:config> </beans>
8/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: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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注解驱动 --> <mvc:annotation-driven /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 包扫描器,扫描@Controller注解 --> <context:component-scan base-package="com.yc.controller" /> <!-- 配置资源映射 --> <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/> <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> <!-- 定义文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设定默认编码 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 设定文件上传的最大值5MB,5*1024*1024 --> <property name="maxUploadSize" value="5242880"></property> </bean> </beans>