SSM项目基础配置
基础Spring配置
一个SSM项目的文件格式
基本流程:controller控制类使用@Autowired自动注入service文件夹下的接口,controller控制类里调用接口里面的方法,去到service文件夹下创建对应的接口,去到service-imp下的类创建实现接口方法并且自动注入mapper文件夹下的mapper接口,在service-imp下调用mapper文件夹下的接口,去掉mapper文件夹的接口中创建对应的接口方法,去到resources-com.xxx.mapper下对应的mapper.xml实现SQ L的增删改查
- src
- main
- java(主要java文件)
- com.xxx(包)
- constant(存放静态熟悉类)
- controller(存放控制类)
- dto (存放数据传输对象,多用于多表连接查询创建的特定类)
- interceptors(存放拦截器)
- mapper(存放mapper接口文件)
- pojo(存放一些简单无规则 java 对象)
- service(存放业务接口)
- imp(存放业务的对应实现类继承service下的接口)
- servlet(存放Web Servlet)
- com.xxx(包)
- resources(资源文件)
- com.xxx.mapper(存放mapper.xml文件)
- spring(存放spring文件)
- applicationContext.xml(spring配置执行文件)
- spring-context.xml(spring 基础配置文件)
- spring-mybatis.xml(spring mybatis配置文件)
- spring-servlet.xml(spring 服务器配置文件)
- db.properties(存放连接数据库的 url driver username password)
- generatorConfig.xml(mybatis逆向工程文件)
- log4j.properties(日志文件)
- webapp
- jsp(存放跳转的页面)
- static(存放一些静态资源比如j query)
- WEB-INF
- web.xml(web配置文件)
- index.jsp
- java(主要java文件)
- main
applicationContext.xml
放于spring文件夹下
<?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">
<!--
引入spring和其它整合配置文件
健壮性-->
<import resource="classpath:spring/spring-*.xml"/>
</beans>
spring-context.xml
放于spring文件夹下
<?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"
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">
<!--spring基础配置-->
<!--1、spring容器注册-->
<context:annotation-config/>
<!--2、自动扫描配置-->
<context:component-scan base-package="com.lzj"/>
<!--3、激活aop注解方式的代理-->
<aop:aspectj-autoproxy/>
<!--消息格式转换-->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="registerDefaultFormatters" value="false"/>
<property name="formatters">
<set>
<bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory"></bean>
</set>
</property>
<property name="formatterRegistrars">
<set>
<bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar">
<property name="dateFormatter">
<bean class="org.springframework.format.datetime.joda.DateTimeFormatterFactoryBean">
<property name="pattern" value="yyyyMMdd"/>
</bean>
</property>
</bean>
</set>
</property>
</bean>
</beans>
spring-mybatis.xml
放于spring文件夹下
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<!--扫描mapper包下的接口-->
<context:component-scan base-package="com.lzj.mapper">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--引入数据库相关信息的配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--如果有需要,请把所有的属性都写到db.properties文件里面-->
<!--c3p0连接池的私有属性-->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="100000"/>
<!--当获取连接失败重试次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--最后关键一步,如何整合mybatis-->
<!--1.注入一般mybatis的sqlSessionFactory这是我们所要做的关键步骤 2.声明式的事务管理-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--引入mappers文件-->
<!--<property name="configLocation" value="classpath:mybtis-config.xml"/>-->
<!--这就要求所有mapper文件必须放到com/lzj/mapper下-->
<property name="mapperLocations" value="classpath:com/lzj/mapper/**/*.xml"/>
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<!--可以加入驼峰命名,其它mybatis的配置也就是mybatis.cfg.xml的相关配置都会转移到这里-->
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</property>
<!--插件配置-->
<property name="plugins">
<array>
<!--分页插件的配置 拦截器实现分页功能-->
<bean class="com.github.pagehelper.PageInterceptor">
<!--这里几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置-->
<property name="properties">
<value>
helperDialect=mysql
reasonable=true
supportMethodsArguments=true
params=count=countSql
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--持久层接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lzj.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
</bean>
<!--事务管理,使用数据源事务管理类进行管理-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--确定事务管理的策略transaction-manager:指向上面的transactionManager-->
<tx:advice transaction-manager="transactionManager" id="transactionAdvice">
<!--事务处理相关值以及它的传播性-->
<tx:attributes>
<!--查询相关配置只读 select开头或者get 或者query-->
<!--开启只读-->
<tx:method name="select*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<!--一旦有异常开启事务回滚-->
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!--使用aop对事务管理范围进行植入 明确几个点
1、对那些地方需要进行事务的管理 execution书写,明确边界
2、使用什么策略去管理策略我们使用了tx:advice全部手写于其中,在我们的aop的advier当中只需要去引用这个事务管理的交易即可-->
<aop:config>
<aop:pointcut id="txCut" expression="execution(* com.lzj.service..*.*(..))"/>
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="txCut"/>
</aop:config>
<!--采用注解进行事务管理,请在Service的实现类上加上@Transanctional注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
spring-servlet.xml
放于spring文件夹下
<?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">
<!--启动注解 排除service注解-->
<context:component-scan base-package="com.lzj">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<!--配置一个视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--加上MVC驱动-->
<mvc:annotation-driven>
<!--配置消息转换器以支持JSON使用 可加可不加-->
<!--<mvc:message-converters>-->
<!--<bean class="org.springframework.http.converter.StringHttpMessageConverter">-->
<!--<property name="supportedMediaTypes">-->
<!--<list>-->
<!--<value>application/json:charset=UTF-8</value>-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
<!--</mvc:message-converters>-->
</mvc:annotation-driven>
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">-->
<!--<property name="messageConverters">-->
<!--<list>-->
<!--<bean class="org.springframework.http.converter.StringHttpMessageConverter">-->
<!--<property name="supportedMediaTypes">-->
<!--<list>-->
<!--<value>text/html:charset=UTF-8</value>-->
<!--<value>application/json:charset=UTF-8</value>-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
<!--<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">-->
<!--<property name="supportedMediaTypes">-->
<!--<list>-->
<!--<value>text/html:charset=UTF-8</value>-->
<!--<value>application/json:charset=UTF-8</value>-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
<!--文件上传 id 必须取名multipartResolver,注册我们的文件上传解析器-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--最大上传的文件你的大小 单位 byte-->
<property name="maxInMemorySize" value="54000000"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<!--静态资源处理-->
<mvc:default-servlet-handler/>
</beans>
web.xml
放于WEB-INF文件夹下
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!--指定整个程序上下文信息-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>spring-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--上下文配置的位置的定制-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-servlet.xml</param-value>
</init-param>
</servlet>
<!--servlet配置映射配置-->
<servlet-mapping>
<servlet-name>spring-servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--字符编码以及全HTTP请求支持过滤器添加-->
<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>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--注册一个支持所有http请求类型的过滤去-->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.8.RELEASE</spring.version>
<jackson.version>2.9.3</jackson.version>
</properties>
<dependencies>
<!--Spring依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--json依赖-->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!--添加jsonwei为javabean-->
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<!--文件上传依赖-->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!--mybatis依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--日志依赖-->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--引入数据源-->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
<!--mybatis与spring整合所需要依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--servlet jsp jstL等依赖start-->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--处理时间格式-->
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
<!--mybatis分页依赖-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<!--用于MD5加密-->
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-maven-plugin -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
<build>
<finalName>AppManagement</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</plugin>
</plugins>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
generatorConfig.xml
mybatis创建逆向工程生成pojo文件mapper文件等 放于resources下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry location="D:\maven-work\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/appinfodb"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.lzj.pojo" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.lzj.mapper" targetProject="src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.lzj.mapper" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--tableName对应的表名,domainObjectName创建Class的名称-->
<table tableName="data_dictionary" domainObjectName="DataDictionary" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!--<property name="useActualColumnNames" value="true"/>-->
<!--<generatedKey column="ID" sqlStatement="DB2" identity="true" />-->
<!--<columnOverride column="DATE_FIELD" property="startDate" />-->
<!--<ignoreColumn column="FRED" />-->
<!--<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />-->
</table>
</context>
</generatorConfiguration>
db.properties
数据库连接文件 放于resources下
jdbc.url=jdbc:mysql://localhost:3306/appinfodb
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456
log4j.properties
在控制台输出日志记录 放于resources下
# 定义LOG输出级别
log4j.rootLogger=INFO,Console,File
# 定义日志输出目的为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活指定日志输出格式,下面一行指定具体的格式
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]-%m%n
#mybaits显示SQL语句日志配置
log4j.logger.com.lzj.mapper=DEBUG
#文件大小到达指定尺寸的时候产生一个新文件
log4j.appender.File=org.apache.log4j.RollingFileAppender
#肯定输出目录,这里会放tomcat之下
log4j.appender.File.File=D:/idea/log.log
#定义文件最大大小
log4j.appender.File.MaxFileSize=10MB
#输出所有日志,如果换成DEBUG表示输出DEBUG以上级别的日志
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm|\:ss}][%c]%m%n
存储一个值引用上下文
WebPathInitServlet页面跳转前加给ctx 放于servlet下
/**
* @Author: LZJ
* @Date: 2019/2/10 20:46
* @Version 1.0
*/
//urlPatterns为空谁都不匹配只是注册为WebServlet,loadOnStartup = 2让他跟随容器启动
@WebServlet(urlPatterns = {},loadOnStartup = 2)
public class WebPathInitServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
//在整体的应用上下文当中存储一个ctx值用它来引用上下文路径。
config.getServletContext().setAttribute("ctx",config.getServletContext().getContextPath());
super.init(config);
}
}
会话拦截器
MethodTimerInterceptor多用于登陆处理 放于interceptors下
/**
* @Author: LZJ
* @Date: 2019/2/15 22:42
* @Version 1.0
* 会话拦截器
*/
public class SessionInterceptor implements HandlerInterceptor {
private static final Logger LOGGER = Logger.getLogger(SessionInterceptor.class);
//检查当前会话是否有User有放行,没有拦截
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object user = request.getSession().getAttribute("SESSION_USER");
if (user==null){
LOGGER.warn("您不具备权限,请登陆");
return false;
}
if (user instanceof User){
//再去数据库检查一次身份
User u =(User) user;
u.setUsername(null);
request.getSession().setAttribute("SESSION_USER",u);
LOGGER.info(u.getUsername()+"正在处于登陆状态");
return true;
}else {
LOGGER.warn("请不要搞事,请先登陆");
return false;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
方法耗时统计拦截器
SessionInterceptor用于测试方法耗时可以查看方法是否需要优化 放于interceptors下
/**
* @Author: LZJ
* @Date: 2019/2/15 22:42
* @Version 1.0
* 方法耗时统计拦截器
*/
public class MethodTimerInterceptor implements HandlerInterceptor {
private static final Logger LOGGER = Logger.getLogger(MethodTimerInterceptor.class);
//前置功能 开始到结束,2个点计算时间
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//定义开始时间
long start = System.currentTimeMillis();
//将其存到请求域当中
request.setAttribute("start",start);
//返回true才会找下一个拦截器,如果没有下一个拦截器,则去Controller
//记录请求日志
LOGGER.info(request.getRequestURI()+",请求到达");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//取出start
long start =(long)request.getAttribute("start");
//得到end
long end = System.currentTimeMillis();
//记录一下耗时
long spendTime = end - start;
if (spendTime>=1000){
LOGGER.warn("方法耗时严重,请及时处理,耗时"+spendTime+"毫秒");
}else {
LOGGER.info("方法耗时"+spendTime+"毫秒,速度正常");
}
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}