SpringMvc+Mybatis+Redis框架Word文档下载推荐.docx
《SpringMvc+Mybatis+Redis框架Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《SpringMvc+Mybatis+Redis框架Word文档下载推荐.docx(24页珍藏版)》请在冰豆网上搜索。
redis:
这里重写了spring-data-redis里面的RedisCache、RedisCachemanager两个类和封装了一些其他的类目的就是为了实现redis的自动续期和单用户登录功能(一个用户同时只能在一个地方登录)如果需要实现不同平台的单用户登录需要自己小改一下在缓存中多加个平台标识就行了
butler:
这是web项目名字不必纠结介绍下包的作用
org.service:
系统的一些服务类这里只有定时任务(SpringTask)和SpringMail邮件推送服务
org.system:
这个看里面的包就知道干嘛用的了就说说exception和interception这两个
org.system.exception:
全局异常处理这里把所有的程序异常捕捉并用JSON返回,以免其他客户端在服务器报错时出现一些数据无法解析造成的其他问题。
至于项目的异常自己也做了配置和封装,在报错的时候可以异步发送邮件给管理员,具体实现请继续往下看
框架的大致结构就介绍到这里下面说一下配置的xml有人说多其实还还好,不喜欢多的可以合一下,两个就能解决了,我分开是为了更明显的区分
web.xml
[html]viewplaincopy在CODE上查看代码片派生到我的代码片
<
?
xmlversion="
1.0"
encoding="
UTF-8"
>
web-appxmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
xmlns="
//xmlns.jcp.org/xml/ns/javaee"
xsi:
schemaLocation="
//xmlns.jcp.org/xml/ns/javaeehttp:
//xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="
3.1"
<
display-name>
butler<
/display-name>
!
--声明Spring配置文件所在目录-->
context-param>
param-name>
contextConfigLocation<
/param-name>
param-value>
classpath*:
applicationContext.xml,
spring-*.xml
/param-value>
/context-param>
--声明IntrospectorCleanupListener监听器防止反复加载对象造成内存泄漏-->
listener>
listener-class>
org.springframework.web.util.IntrospectorCleanupListener<
/listener-class>
/listener>
--声明ContextLoaderListener监听器自动装配Spring配置文件信息-->
org.springframework.web.context.ContextLoaderListener<
--全局UTF-8编码过滤器-->
filter>
filter-name>
CharacterEncoding<
/filter-name>
filter-class>
org.springframework.web.filter.CharacterEncodingFilter<
/filter-class>
init-param>
encoding<
UTF-8<
/init-param>
/filter>
filter-mapping>
url-pattern>
/*<
/url-pattern>
/filter-mapping>
--配置Spring控制器-->
servlet>
servlet-name>
spring<
/servlet-name>
servlet-class>
org.springframework.web.servlet.DispatcherServlet<
/servlet-class>
classpath*:
spring-mvc.xml<
load-on-startup>
1<
/load-on-startup>
/servlet>
--HttpPutFormContentFilter使put方法是也可以获取表单内的参数这里我有特殊需求标准的Rest可以去掉-->
HttpMethodFilter<
org.springframework.web.filter.HttpPutFormContentFilter<
servlet-mapping>
/servlet-mapping>
/web-app>
接下来是Spring的主配置文件applicationContext.xml
utf-8"
beansxmlns="
//www.springframework.org/schema/beans"
xmlns:
context="
//www.springframework.org/schema/context"
aop="
//www.springframework.org/schema/aop"
tx="
//www.springframework.org/schema/tx"
cache="
//www.springframework.org/schema/cache"
c="
//www.springframework.org/schema/c"
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans.xsd
//www.springframework.org/schema/context
//www.springframework.org/schema/context/spring-context.xsd
//www.springframework.org/schema/aop
//www.springframework.org/schema/aop/spring-aop.xsd
//www.springframework.org/schema/tx
//www.springframework.org/schema/tx/spring-tx.xsd
//www.springframework.org/schema/cache
//www.springframework.org/schema/cache/spring-cache-3.2.xsd"
--配置需要交给spring扫描管理的包,一般是包括整个项目的java文件的父包(由context提供)-->
context:
component-scanbase-package="
org.system,org.service.task"
/>
--属性文件读入,用于加密数据库配置文件-->
beanid="
propertyConfigurer"
class="
org.system.encrypt.DBConfigurer"
propertyname="
locations"
list>
value>
classpath:
conf.properties<
/value>
/list>
/property>
/bean>
--配置需要交给spring扫描管理的文件,一般是项目的配置文件(由context提供)-->
property-placeholderlocation="
conf.properties"
--配置数据源-->
beanname="
dataSource"
com.alibaba.druid.pool.DruidDataSource"
destroy-method="
close"
--基本配置-->
driverClassName"
value="
${db.driverClassName}"
url"
${db.url}"
username"
${db.user}"
password"
${db.password}"
--初始化时建立物理连接的个数-->
initialSize"
${db.initialSize}"
--最小连接池数-->
minIdle"
${db.minIdle}"
--最大连接池数量-->
maxActive"
${db.maxActive}"
--配置获取连接等待超时的时间-->
maxWait"
${db.maxWait}"
--配置sqlSessionFactory(由mybatis-spring.jar提供支持)-->
sqlSessionFactory"
org.mybatis.spring.SqlSessionFactoryBean"
ref="
--配置mybatis的默认选项-->
configuration"
--配置拦截器用于Mybatis分页和总数查询,只实现Mysql其他的可以参照-->
plugins"
PaginationInterceptor"
--mybatis默认选项配置这里是因为我返回全是用Map所以如果数据库有字段为空就会使这个属性不显示为了解决这个问题才需要配置这个-->
org.apache.ibatis.session.Configuration"
callSettersOnNulls"
true"
/>
--Mybatis分页拦截器-->
org.system.intercept.PaginationInterceptor"
--Mapper接口所在包名,Spring会自动查找其下的Mapper-->
beanclass="
org.mybatis.spring.mapper.MapperScannerConfigurer"
basePackage"
org.system.mapper..*"
sqlSessionFactoryBeanName"
--支持注解事务模式-->
tx:
annotation-driventransaction-manager="
transactionManager"
proxy-target-class="
--事务管理-->
org.springframework.jdbc.datasource.DataSourceTransactionManager"
--配置声明式事务-->
adviceid="
txAdvice"
transaction-manager="
attributes>
methodname="
insert*"
propagation="
REQUIRED"
read-only="
false"
delete*"
update*"
get*"
SUPPORTS"
/tx:
advice>
--配置该声明式事务规则用于的切入点步骤:
1.配置切入点2.应用该声明式事务规则-->
aop:
configproxy-target-class="
expose-proxy="
--配置切入点-->
pointcutid="
transaction_pointcut"
expression="
execution(*org.system.service.impl..*.*(..))"
--应用该声明式事务规则-->
advisoradvice-ref="
pointcut-ref="
/aop:
config>
--异常拦截器-->
exceptionHandler"
org.system.exception.ExceptionResolver"
--Spring上下文工具类-->
springContextUtil"
org.service.utils.spring.SpringContextUtil"
/beans>
这里说一说有个数据库加密的东西在这里
意思就是用DBConfigurer这个类来解密conf.properties这个文件内的几个配置就是采用了DES加密解密主要为了防止配置文件泄露数据库的信息暴露如果不用直接注释掉就可以使用明文了稍后会把properties相关文件贴出来
接下来是spring-mvc.xml
mvc="
//www.springframework.org/schema/mvc"
task="
//www.springframework.org/schema/task"
//www.springframework.org/schema/beanshttp:
//www.springframework.org/schema/mvchttp:
//www.springframework.org/schema/mvc/spring-mvc.xsd
//www.springframework.org/schema/aophttp:
//www.springframework.org/schema/contexthttp:
//www.springframework.org/schema/taskhttp:
//www.springframework.org/schema/task/spring-task-3.1.xsd"
--扫描要自动管理的包-->
org.system.controller.impl"
--静态资源文件路径设置-->
mvc:
resourceslocation="
/api/"
mapping="
/api/**"
--对模型视图名称的解析,在请求时模型视图名称添加前后缀系统内没有涉及到页面跳转所以基本无用-->
org.springframework.web.servlet.view.InternalResourceViewResolver"
prefix"
/"
suffix"
va