1.pom.xml配置
1.1<build>标签中配置<plugins>和<resources>,即插件和资源文件
1.2 <properties>标签中配置自定义的标签,可以在<dependency>中取出
1.3<dependencies>中配置各依赖包,统一管理
2.创建项目用到的包
1.1web层一般用到model,dao,controller,service,utils,security
1.2创建webapp下面的静态资源存放目录,如images,js,WEB-INF目录下的views
3.resources下创建资源文件
1.1先创建数据库的配置文件
##JDBC Global Setting jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=root ##DataSource Global Setting #配置初始化大小、最小、最大 ds.initialSize=1 ds.minIdle=1 ds.maxActive=20 #配置获取连接等待超时的时间 ds.maxWait=60000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 ds.timeBetweenEvictionRunsMillis=60000 #配置一个连接在池中最小生存的时间,单位是毫秒 ds.minEvictableIdleTimeMillis=300000
1.2log4j日志的配置文件
# DEBUG,INFO,WARN,ERROR,FATAL LOG_LEVEL=INFO log4j.rootLogger=${LOG_LEVEL},CONSOLE,FILE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Encoding=utf-8 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout #log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{8}@(%F:%L):%m%n log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{1}@(%F:%L):%m%n log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender log4j.appender.FILE.File=${catalina.base}/logs/ssm.log log4j.appender.FILE.Encoding=utf-8 log4j.appender.FILE.DatePattern=‘.‘yyyy-MM-dd log4j.appender.FILE.layout=org.apache.log4j.PatternLayout #log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout log4j.appender.FILE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} %C{8}@(%F\:%L)\:%m%n
1.3创建applicationContext.xml文件(spring的配置文件)
1.3.1 创建过程:(1)管理包下的注解(2)引入数据库配置文件(3)配置数据源dataSource(4)mybatis文件配置,扫描所有mapper接口,sqlsessionFactory需要配置属性dataSource,mybatis配置文件地址,mapper接口地址
(5)spring和mybatis整合,扫描所有dao,需要属性配置,dao地址,sqlsessionFactory(6)事务管理器,需要dataSource(7)事务通知,对哪些方法进行了哪些事务管理(8)事务的aop管理,需要切点与通知
(9)配置spring的cglib代理(10)开启事务的注解(11)配置spring的cache注解,需要缓存管理器,缓存管理器引用缓存管理器工厂,工厂指向ehcache.xml文件
1.3.2代码:
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--自动扫描sj包,将带有注解的类纳入spring管理 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件, 如果扫描到有@Component @[email protected]等这些注解的类,则把这些类注册为bean--> <context:component-scan base-package="com.demo"></context:component-scan> <!--引入配置文件--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:application.properties</value> </list> </property> </bean> <!--配置数据源--> <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.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${ds.initialSize}"/> <property name="minIdle" value="${ds.minIdle}"/> <property name="maxActive" value="${ds.maxActive}"/> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="${ds.maxWait}"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${ds.minEvictableIdleTimeMillis}"/> <property name="validationQuery" value="SELECT ‘x‘"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="false"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat"/> </bean> <!--mybatis扫描mapper接口--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml" p:mapperLocations="classpath:com/demo/web/dao/*.xml"> </bean> <!--spring与mybatis整合配置,扫描所有dao--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.demo.web.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory"></bean> <!--事务配置--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> <!--事务通知--> <tx:advice id="txActive" transaction-manager="transactionManager"> <tx:attributes> <!-- 对insert,update,delete 开头的方法进行事务管理,只要有异常就回滚 --> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/> <!-- select,count开头的方法,开启只读,提高数据库访问性能 --> <tx:method name="select*" read-only="true"/> <tx:method name="count*" read-only="true"/> <!-- 对其他方法 使用默认的事务管理 --> <tx:method name="*"/> </tx:attributes> </tx:advice> <!--aop--> <aop:config> <aop:pointcut id="serviceMethods" expression="execution(* com.demo.web.service..*(..))"/> <aop:advisor advice-ref="txActive" pointcut-ref="serviceMethods"></aop:advisor> </aop:config> <!-- 配置使Spring采用CGLIB代理 --> <aop:aspectj-autoproxy proxy-target-class="true"/> <!--事务的注解支持--> <tx:annotation-driven transaction-manager="transactionManager"/> <!--Cache配置--> <cache:annotation-driven cache-manager="cacheManager"/> <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml"/> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManagerFactory"/> </beans>
1.4创建mybatis-config.xml文件,并配置
1.4.1基本需要:与spring整合后只需一个空框架即可,不用加任何配置,也对mybatis有其他配置,如typeAliases别名,settings设置等
1.4.2代码:
<?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> <properties> <property name="dialectClass" value="com.demo.core.feature.orm.dialect.MySql5Dialect"/> </properties> <!-- 配置mybatis的缓存,延迟加载等等一系列属性 --> <settings> <!-- 全局映射器启用缓存 --> <setting name="cacheEnabled" value="true"/> <!-- 查询时,关闭关联对象即时加载以提高性能 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 --> <setting name="multipleResultSetsEnabled" value="true"/> <!-- 允许使用列标签代替列名 --> <setting name="useColumnLabel" value="true"/> <!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 --> <setting name="useGeneratedKeys" value="false"/> <!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL --> <setting name="autoMappingBehavior" value="PARTIAL"/> <!-- 对于批量更新操作缓存SQL以提高性能 BATCH,SIMPLE --> <!-- <setting name="defaultExecutorType" value="BATCH" /> --> <!-- 数据库超过25000秒仍未响应则超时 --> <!-- <setting name="defaultStatementTimeout" value="25000" /> --> <!-- Allows using RowBounds on nested statements --> <setting name="safeRowBoundsEnabled" value="false"/> <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. --> <setting name="localCacheScope" value="SESSION"/> <!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values like NULL, VARCHAR or OTHER. --> <setting name="jdbcTypeForNull" value="OTHER"/> <!-- Specifies which Object‘s methods trigger a lazy load --> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 --> <setting name="aggressiveLazyLoading" value="false"/> </settings> <!--写了别名就可以在mapper.xml文件中直接写类名--> <typeAliases> <package name="com.demo.web.model"></package> </typeAliases> <plugins> <plugin interceptor="com.demo.core.feature.orm.mybatis.PaginationResultSetHandlerInterceptor"/> <plugin interceptor="com.demo.core.feature.orm.mybatis.PaginationStatementHandlerInterceptor"/> </plugins> </configuration>
1.5配置ehcache.xml文件,缓存管理配置
<?xml version="1.0" encoding="UTF-8"?> <ehcache updateCheck="false" name="txswx-ehcache"> <!-- 磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存 path:指定在硬盘上存储对象的路径 --> <diskStore path="java.io.tmpdir"/> <!-- defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理 maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象 eternal:代表对象是否永不过期 timeToIdleSeconds:最大的发呆时间 timeToLiveSeconds:最大的存活时间 overflowToDisk:是否允许对象被写入到磁盘 --> <defaultCache maxEntriesLocalHeap="10000" eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" maxEntriesLocalDisk="100000"/> </ehcache>
1.6创建springmvc.xml
1.6.1基础配置创建过程:(1)把controller层的注解纳入管理(2)模型视图前后缀管理(3)springmvc上传文件配置(4)整合shiro框架配置
1.6.2代码:(较为复杂的配置,不是基础配置)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--controller层注入--> <context:component-scan base-package="com.demo.web.controller"/> <!-- 指定自己定义的validator --> <mvc:annotation-driven validator="validator"/> <!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 会 自动注册 --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties --> <property name="validationMessageSource" ref="messageSource"/> </bean> <!-- 国际化的消息资源文件(本系统中主要用于显示/错误消息定制) --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <!-- 在web环境中一定要定位到classpath 否则默认到当前web应用下找 --> <value>classpath:messages</value> <value>classpath:org/hibernate/validator/ValidationMessages</value> </list> </property> <property name="useCodeAsDefaultMessage" value="false"/> <property name="defaultEncoding" value="UTF-8"/> <property name="cacheSeconds" value="60"/> </bean> <!--配置拦截器--> <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="defaultLocale" value="zh_CN"/> </bean> <!--支持返回json(避免IE在ajax请求时,返回json出现下载 )--> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!--模型视图增加前后缀--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp"/> <!--springMvc上传文件--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"/> <property name="maxUploadSize" value="10485760000"/> <property name="maxInMemorySize" value="40960"/> </bean> <!--启用shiro授权注解方式--> <aop:config proxy-target-class="true"></aop:config> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean> </beans>
1.7application_shiro.xml文件配置
1.7.1创建过程:(1)shiro过滤器配置(包括安全管理器,登录页面,登录成功页面,没有访问权限页面,权限设置)(2)会话DAO(3)会话管理器,需要会话DAO(4)缓存管理器,需要缓存管理配置文件路径(5)reamls配置,需要自定义的realm类(6)安全管理器,需要reamls配置(7)shiro生命周期处理器
1.7.2代码:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util.xsd"> <description>apache shiro配置</description> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!--配置securityManager 安全管理器--> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/page/login.shtml"/> <property name="successUrl" value="/index.shtml"/> <property name="unauthorizedUrl" value="/page/401.shtml"/> <property name="filterChainDefinitions"> <value> <!-- 静态资源允许访问 --> /app/** = anon /assets/** = anon <!-- 登录页允许访问 --> /user/login = anon <!-- 其他资源需要认证 --> /** = authc </value> </property> </bean> <!--缓存管理器--> <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/> </bean> <!--会话DAO--> <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.MemorySessionDAO"/> <!--会话管理器--> <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <property name="sessionDAO" ref="sessionDAO"/> </bean> <!--登录规则--> <bean id="securityRealm" class="com.cjw.web.security.SecurityRealm"/> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realms"> <list> <ref bean = "securityRealm"/> </list> </property> </bean> <!-- Shiro生命周期处理器 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> </beans>
1.8ehcache-shiro.xml配置
<ehcache updateCheck="false" name="shiroCache"> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> </ehcache>
1.9 web.xml配置
1.9.1创建过程:(1)spring文件初始化配置(2)spring上下文监听器配置(3)spring编码过滤器配置(4)log4j初始化配置(5)log4j监听器配置(6)shiro过滤器配置(7)前端控制器配置(8)错误页面配置
原文地址:https://www.cnblogs.com/future-eye/p/8341394.html