pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.woniu</groupId> <artifactId>bkeeping</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <srping.version>4.2.1.RELEASE</srping.version> <mybatis.version>3.3.0</mybatis.version> <slf4j.version>1.7.12</slf4j.version> <log4j.version>1.2.17</log4j.version> <jackson.version>2.7.0</jackson.version> </properties> <dependencies> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>javaee-api</artifactId> <version>5.0-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- unit testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <!-- Indicates that the package was introduced during development and not loaded when published --> <scope>test</scope> </dependency> <!-- spring Framework Package start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${srping.version}</version> </dependency> <!-- spring Framework Package end --> <!-- mybatis Framework Package start --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- mybatis Framework Package end --> <!-- Database Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <!-- C3P0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-all</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.8</version> </dependency> </dependencies> <build> <plugins> <!-- To configure jdk Version 1.8 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- To configure tomcat7 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </build> </project>
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>bkeeping</display-name> <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> <!--1. Configure the listener to read the xml file in the contextConfigLocation, if not specified, Read the application.xml file in the WEB-INF path by default--> <!--Understand: The listener reads the xml file in the location path and generates a WebApplicationContext object, which is placed in the properties of the ServletContext. This allows us to get the WebApplicationContext object and use it to access spring container managed beans --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 2. Specify the path to the spring profile--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </context-param> <!--3. Configure Dispatcher Servlet --> <!--Duties of Dispatcher Servlet: 1. File upload resolution. If the request type is multipart, file upload resolution will be done through MultipartResolver. 2. Map the request to the processor through HandlerMapping (returns a HandlerExecutionChain that includes a processor and multiple HandlerInterceptor interceptors); 3. Support multiple types of processors (processors in Handler Execution Chain) through Handler Adapter; 4. Resolve the logical view name to the specific view through ViewResolver; 5. Localization analysis; 6. Render specific views, etc. 7. If an exception is encountered during execution, it will be handled to HandlerExceptionResolver for resolution. --> <servlet> <!--3.1 Configured Dispatcher Servlet Name and Class Class--> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--3.2 If init-param is not configured, Dispatcher Servlet defaults to WebApplicationContext as the context, Spring default profile is'/WEB-INF/[servlet name]-servlet.xml'--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <! -- Initialize the Servlet when starting the container at 3.3 --> <load-on-startup>1</load-on-startup> </servlet> <!--3.4 intercepts matching requests, Servlet intercepts matching rules a: *.do [Requests ending with A.do are resolved by Dispatcher Servlet on behalf of access] b: /[Represents that all access is parsed by Dispatcher Servlet, and for static files that do not need to be parsed such as: js css needs to be configured not to allow Dispatcher Servlet Resolve, for RESTFUL-style url s, which configures the Internal ResourceViewResolver view resolver, defining prefix suffix) --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--4.CharacterEncoding Filter Character Encoding Filter--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--4.1 To use the character set, we usually use UTF-8 (UTF-8 is best for insurance)--> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <!--4.2 Whether to force the encoding of request to be encoding, default false, not recommended for change--> <param-name>forceRequestEncoding</param-name> <param-value>false</param-value> </init-param> <init-param> <!--4.3 Whether to force the encoding of response to be encoding is recommended to be true--> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <!--4.4 Don't leave an empty space or write'/ 'directly here, otherwise it won't work--> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
spring-mvc.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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!--1. The component scan element scans the specified package and all its subpackages and finds classes that can be automatically registered as Spring Bean s Class must have @Component @Controller @Service @Repository--> <context:component-scan base-package="com.woniu.controller"/> <!--2. Annotation drivers automatically register RequestMappingHandlerMapping, RequestMappingHandlerAdapter, ExceptionHandlerExceptionResolver And support Support for type conversion of form parameters using ConversionService instances Support for formatting data types using @NumberFormat annotation, @DateTimeFormat annotation Support JSR303 validation of JavaBean instances using @Valid annotation Support for @RequestBody and @ResponseBody annotations --> <mvc:annotation-driven/> <!--3. Internal Resource View Resolver is a subclass of UrlBasedViewResolver--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="suffix" value=".jsp"></property> <property name="prefix" value="/WEB-INF/page/"></property> </bean> </beans>
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmfour jdbc.username=root jdbc.password=123
spring-db.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--1.Appoint properties Path to configuration file --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- Configure Data Source,Use Spring Built-in data source connection pool --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> </beans>
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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 1.Under test dao Use when getBean When you need to use spring Injection unnecessary --> <!-- <import resource="spring-db.xml"/> --> <!--2. To configure SqlSessionFactoryBean --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 2.1 Appoint MyBatis Path to master profile --> <property name="configLocation" value="classpath:mybatisconfig.xml" /> <!-- 2.2 Configure data source properties in spring-db Always configured data source --> <property name="dataSource" ref="dataSource" /> <!--2.3 Appoint mapper.xml Location, default and dao.class Under the same package --> <property name="mapperLocations" value="classpath:com/woniu/mapper/*.xml"/> <!-- 2.4 Easy to use aliases for entity classes sql Sentence --> <property name="typeAliasesPackage" value="com.woniu.pojo"/> <!-- 2.5 Use mybatis Plug-ins for pagehelper,Remember to guide dependencies --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helperDialect=mysql supportMethodsArguments=true <!-- Define the current page and page size, multiple separated by commas --> params=pageNum=currentPage,pageSize=pageSize </value> </property> </bean> </array> </property> </bean> <!-- 3.Specify package bulk scan from dao Scan Out in Package Mapper Interface, automatically create proxy object and spring Register in Container //Follow the specifications: Keep mapper.java and mapper.xml mapping file names consistent, and namespace in mapper is the fully qualified name of dao in a directory--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.woniu.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" /> </bean> </beans>
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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--1.Component Scan @Service Perform automatic registration Spring Bean --> <context:component-scan base-package="com.woniu.service" /> </beans>
log4j.properties
#USE THIS SETTING FOR OUTPUT MYBATIS`s SQL ON THE CONSOLE ## this need write where you want to use log4j log4j.logger.com.woniuxy.dao=DEBUG, Console #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%p] %m [%t] %c [%l]%n #log4j.logger.java.sql.ResultSet=INFO #log4j.logger.org.apache=INFO #log4j.logger.java.sql.Connection=DEBUG #log4j.logger.java.sql.Statement=DEBUG #log4j.logger.java.sql.PreparedStatement=DEBUG