Framework integration ideas:
1. Mapper layer: integrating mybatis and spring
Required jar package:
1. The jar package of mybatis
2. Mysql database driver
3. Database connection pool
4. The integration package of Mybatis and spring.
5. Spring's jar package
Configuration file:
1. Mybatis configuration file: SqlMapConfig.xml
2. Spring configuration file: applicationContext-dao.xml
data source
1. Database connection pool
2. Configure SqlSessionFactory (in mybatis and spring integration packages)
3. Configure the scanner for the mapper file.
Second, the Service layer:
The jar package used: spring's jar package.
Configuration file: applicationContext-service.xml
Configure a package scanner to scan all classes annotated with @Service.
Transaction configuration:
Configuration file: applicationContext-trans.xml
1. Configure a transaction manager
2. Configure tx
3. Configure the slice
Third, the performance layer
To use springmvc, you need to use springmvc and spring's jar package.
Configuration file: springmvc.xml
1. Configure the annotation driver
2. Configure a view resolver.
3. Package scanner, @Controller annotation
Web.xml
1. Configure the front controller of springmvc
2. The listener initialized by the Spring container.
Part of the configuration file:
1/web.xml file configuration:
<?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> <!-- Initialize the spring container--> <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> <!-- log configuration file --> <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> <!-- Solve post garbled characters--> <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's front controller --> <servlet> <servlet-name>yc-manager</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation is not required, if you do not configure contextConfigLocation, springmvc's configuration file defaults to: 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> <!--web.xml in core --> <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-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 file configuration
<?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> <!-- Whether to enable the mapping of underscore and camel case naming rules --> <setting name="mapUnderscoreToCamelCase" value="true" /> <!-- Set L2 cache--> <setting name="cacheEnabled" value="true" /> <!-- print query statement--> <setting name="logImpl" value="SLF4J" /> <!-- --> <setting name="defaultExecutorType" value="REUSE" /> <!-- Specify whether to call the setter (put) method of the map object when the value in the result set is null --> <setting name="callSettersOnNulls" value="true" /> <!-- lazy loading --> </settings> <typeAliases> <package name="com.yc.pojo" /> </typeAliases> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!--Pagination parameter rationalization--> <property name="reasonable" value="true" /> </plugin> </plugins> </configuration>
3/db.properties configuration file:
#**************************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 #Maximum number of concurrent connections maxActive=20 #Initialize the number of connections initialSize=1 #Configure the time for getting the connection to wait for the timeout maxWait = 60000 #Minimum number of idle connections minIdle = 10 #Configure the interval for how long to perform detection and detect idle connections that need to be closed, in milliseconds timeBetweenEvictionRunsMillis = 60000 #Configure the minimum lifetime of a connection in the pool, in milliseconds minEvictableIdleTimeMillis = 300000 #The sql used to detect whether the connection is valid, the requirement is a query statement. validationQuery=SELECT 'x' testWhileIdle=true testOnBorrow = false testOnReturn = false #Whether to cache preparedStatement, which is PSCache. PSCache greatly improves the performance of databases that support cursors, such as oracle. It is recommended to close under mysql. poolPreparedStatements = false #Open PSCache and specify the size of PSCache on each connection 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"> <!-- Load configuration file --> <context:property-placeholder location="classpath:properties/*.properties"/> <!-- Database connection pool --> <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> <!-- Let spring manage sqlsessionfactory using mybatis and spring integration package --> <!-- Configure SqlsessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- Load the configuration file of mybatis--> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/> <!-- configure data source --> <property name="dataSource" ref="dataSource"/> </bean> <!-- Configure the package scanner, scan the mapper interface to generate proxy objects and put them in the spring container --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- Specify the package to scan --> <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"> <!-- Configure the package scanner to scan the class of the @Service primary key --> <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"> <!-- Transaction Configuration --> <!-- Transaction Manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- datasource--> <property name="dataSource" ref="dataSource" /> </bean> <!-- NOTIFICATION--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- Propagation behavior --> <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> <!-- facet--> <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"> <!-- Annotation driver--> <mvc:annotation-driven /> <!-- View resolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- Package scanner, scan @Controller annotations --> <context:component-scan base-package="com.yc.controller" /> <!-- Configure resource mapping --> <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/> <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> <!-- Define file upload parser--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- set default encoding--> <property name="defaultEncoding" value="UTF-8"></property> <!-- Set the maximum file upload size to 5MB, 5*1024*1024 --> <property name="maxUploadSize" value="5242880"></property> </bean> </beans>