SSM配置文件
一、maven配置文件(pom.xml)
坐标 编码 相关依赖 加载配置资源 编译器版本
<?xml version="1.0" encoding="UTF-8"?>
< 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.bjpowernode</ groupId>
< artifactId> ch11-ssm</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< packaging> war</ packaging>
< 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>
</ properties>
< dependencies>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> javax.servlet-api</ artifactId>
< version> 3.1.0</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> javax.servlet.jsp</ groupId>
< artifactId> jsp-api</ artifactId>
< version> 2.2.1-b03</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 4.3.16.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-tx</ artifactId>
< version> 4.3.16.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 4.3.16.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aspects</ artifactId>
< version> 4.3.16.RELEASE</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-core</ artifactId>
< version> 2.9.0</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-databind</ artifactId>
< version> 2.9.0</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 1.3.1</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.1</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.9</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.1.12</ version>
</ dependency>
</ dependencies>
< build>
< finalName> p2p</ finalName>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> false</ filtering>
</ resource>
</ resources>
< plugins>
< plugin>
< artifactId> maven-compiler-plugin</ artifactId>
< version> 3.1</ version>
< configuration>
< source> 1.8</ source>
< target> 1.8</ target>
</ configuration>
</ plugin>
< plugin>
< groupId> org.mybatis.generator</ groupId>
< artifactId> mybatis-generator-maven-plugin</ artifactId>
< version> 1.3.6</ version>
< configuration>
< configurationFile> GeneratorMapper.xml</ configurationFile>
< verbose> true</ verbose>
< overwrite> true</ overwrite>
</ configuration>
</ plugin>
</ plugins>
</ build>
</ project>
二、Tomcat配置文件(web.xml)
中央调度器(在Tomcat启动后,创建DispatcherServlet
) 编码过滤器(防止POST请求中文乱码)CharacterEncodingFilter
监听器(在全局作用域初始化完成后,将Spring容器对象加入到全局作用域中)ContextLoaderListener
<?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_4_0.xsd"
version = " 4.0" >
< servlet>
< servlet-name> dispatcherServlet</ servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< init-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:springmvc.xml</ param-value>
</ init-param>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> dispatcherServlet</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener</ listener-class>
</ listener>
< context-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:applicationContext.xml</ param-value>
</ context-param>
< 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>
< welcome-file-list>
< welcome-file> index</ welcome-file>
</ welcome-file-list>
</ web-app>
三、Mybatis参数文件(jdbc.properties)
jdbc. url= jdbc: mysql: // localhost: 3306 / p2p?useUnicode= true& characterEncoding= UTF- 8 & useJDBCCompliantTimezoneShift= true& useLegacyDatetimeCode= false& serverTimezone= CTT
jdbc. user= root
jdbc. password= 123
四、Mybatis配置文件(mybatis.xml)
配置日志<settings>
指定mapper文件位置<mappers><package>
<?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 = " logImpl" value = " STDOUT_LOGGING" />
</ settings>
< typeAliases>
< package name = " com.bjpowernode.domain" />
</ typeAliases>
< mappers>
< package name = " com.bjpowernode.dao" />
</ mappers>
</ configuration>
五、Spring配置文件(applicationContext.xml)
声明对@Service的组件扫描器<context:component-scan
创建dao对象
加载第三步中的配置文件信息<context:property-placeholder>
创建数据库连接池DruidDataSource
创建SqlSessionFactoryBean
创建mybatis扫描器MapperScannerConfigurer
声明事务 声明切面类
<?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
" >
< context: component-scan base-package = " com.bjpowernode.service" />
< context: property-placeholder location = " classpath:conf/jdbc.properties" />
< bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource"
init-method = " init" destroy-method = " close" >
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.user}" />
< property name = " password" value = " ${jdbc.password}" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " configLocation" value = " classpath:conf/mybatis.xml" />
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " sqlSessionFactoryBeanName" value = " sqlSessionFactory" />
< property name = " basePackage" value = " com.bjpowernode.dao" />
</ bean>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " myDataSource" />
</ bean>
< tx: annotation-driven transaction-manager = " transactionManager" />
< tx: advice id = " myAdvice" transaction-manager = " transactionManager" >
< tx: attributes>
< tx: method name = " save*" isolation = " DEFAULT" propagation = " REQUIRED"
rollback-for = " com.bjpowernode.exception.AjaxRequestException,
com.bjpowernode.exception.TraditionRequestException" />
< tx: method name = " update*" isolation = " DEFAULT" propagation = " REQUIRED"
rollback-for = " com.bjpowernode.exception.AjaxRequestException,
com.bjpowernode.exception.TraditionRequestException" />
< tx: method name = " delete*" isolation = " DEFAULT" propagation = " REQUIRED"
rollback-for = " com.bjpowernode.exception.AjaxRequestException,
com.bjpowernode.exception.TraditionRequestException" />
< tx: method name = " *" propagation = " SUPPORTS" read-only = " true" />
</ tx: attributes>
</ tx: advice>
< aop: config>
< aop: pointcut id = " servicePt" expression = " execution(* *..service..*.*(..))" />
< aop: advisor advice-ref = " myAdvice" pointcut-ref = " servicePt" />
</ aop: config>
</ beans>
六、SpringMVC配置文件(springmvc.xml)
声明对@Controller对象的组件扫描器 声明视图解析器InternalResourceViewResolver
声明注解驱动<mvc:innotation-driven/>
声明拦截器<interceptors>
声明上传解析器CommonsMultipartResolver
<?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" >
< context: component-scan base-package = " com.bjpowernode.controller" />
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /WEB-INF/jsp/" />
< property name = " suffix" value = " .jsp" />
</ bean>
< mvc: annotation-driven/>
< mvc: interceptors>
< mvc: interceptor>
< mvc: mapping path = " /**" />
< mvc: exclude-mapping path = " /settings/user/login.do" />
< bean class = " com.bjpowernode.interceptor.LoginInterceptor" />
</ mvc: interceptor>
</ mvc: interceptors>
< bean id = " multipartResolver" class = " org.springframework.web.multipart.commons.CommonsMultipartResolver" >
< property name = " maxUploadSize" value = " #{1024*1024*80}" />
< property name = " defaultEncoding" value = " utf-8" />
</ bean>
</ beans>
七、Mybatis映射配置文件(接口名.xml)
映射接口名mapper
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
< mapper namespace = " com.bjpowernode.dao.StudentDao" >
< insert id = " insertStudent" >
insert into student(name,email,age) values(#{name},#{email},#{age})
</ insert>
< select id = " selectStudents" resultType = " Student" >
select * from student order by id desc
</ select>
</ mapper>
八、MyBatis逆向工程
<?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 = " H:\PowerNode\jar\MySql\mysql-connector-java-5.1.47.jar" />
< context id = " tables" targetRuntime = " MyBatis3" >
< commentGenerator>
< property name = " suppressAllComments" value = " true" />
</ commentGenerator>
< jdbcConnection driverClass = " com.mysql.jdbc.Driver"
connectionURL = " jdbc:mysql://localhost:3306/p2p"
userId = " root"
password = " 123" >
</ jdbcConnection>
< javaModelGenerator targetPackage = " com.wkcto.p2p.model"
targetProject = " H:\07-SpringBoot\projcet\p2p-springboot\p2p-exterface\src\main\java" >
< property name = " enableSubPackages" value = " false" />
< property name = " trimStrings" value = " false" />
</ javaModelGenerator>
< sqlMapGenerator targetPackage = " com.wkcto.p2p.mapper"
targetProject = " src/main/java" >
< property name = " enableSubPackages" value = " false" />
</ sqlMapGenerator>
< javaClientGenerator type = " XMLMAPPER"
targetPackage = " com.wkcto.p2p.mapper"
targetProject = " src/main/java" >
< property name = " enableSubPackages" value = " false" />
</ javaClientGenerator>
< table tableName = " b_bid_info" domainObjectName = " BidInfo"
enableCountByExample = " false"
enableUpdateByExample = " false"
enableDeleteByExample = " false"
enableSelectByExample = " false"
selectByExampleQueryId = " false" />
</ context>
</ generatorConfiguration>
九、log4j2配置
<?xml version="1.0" encoding="UTF-8"?>
< configuration status = " info" >
< appenders>
< Console name = " Console" target = " SYSTEM_OUT" >
< PatternLayout pattern = " [p2p] %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" />
</ Console>
< RollingFile name = " RollingFile" fileName = " /opt/tomcat_p2p/logs/app.log"
filePattern = " /opt/tomcat_p2p/logs/app_%d{yyyy-MM-dd}_%i.log" >
< PatternLayout pattern = " [p2p] %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" />
< SizeBasedTriggeringPolicy size = " 300MB" />
</ RollingFile>
</ appenders>
< loggers>
< root level = " info" >
< appender-ref ref = " Console" />
< appender-ref ref = " RollingFile" />
</ root>
</ loggers>
</ configuration>
十、redis配置
1. redis.properties
redis.usePool= true //指定是否使用连接池
redis.hostName= 192.168.226.128 //ip
redis.port= 6379 //端口
redis.password= 123456 //密码
redis.timeout= 15000 //超时时间
2. redis配置
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: context= " http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " >
< bean id = " jedisConnectionFactory" class = " org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
< property name = " usePool" value = " ${redis.usePool}" />
< property name = " hostName" value = " ${redis.hostName}" />
< property name = " port" value = " ${redis.port}" />
< property name = " timeout" value = " ${redis.timeout}" />
< property name = " password" value = " ${redis.password}" />
</ bean>
< bean id = " redisTemplate" class = " org.springframework.data.redis.core.RedisTemplate" >
< property name = " connectionFactory" ref = " jedisConnectionFactory" />
</ bean>
</ beans>
十一、配置Dubbo
1. 服务提供者
<?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: dubbo= " http://code.alibabatech.com/schema/dubbo"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd" >
< dubbo: application name = " dataservice" />
< dubbo: registry protocol = " zookeeper" address = " 192.168.226.128:2181" />
< dubbo: service interface = " com.bjpowernode.p2p.service.UserInfoService"
ref = " userInfoServiceImpl"
version = " 1.0.0"
timeout = " 15000" />
</ beans>
2. 服务消费者
<?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: dubbo= " http://code.alibabatech.com/schema/dubbo"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd" >
< dubbo: application name = " p2p" />
< dubbo: registry protocol = " zookeeper" address = " 192.168.226.128:2181" />
< dubbo: reference id = " userInfoService" interface = " com.bjpowernode.p2p.service.UserInfoService" check = " false" version = " 1.0.0" />
</ beans>