spring整合springMVC、mybatis、shiro
jar包:
1 <properties> 2 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 3 <maven.compiler.source>1.7</maven.compiler.source> 4 <maven.compiler.target>1.7</maven.compiler.target> 5 <!--spring版本号--> 6 <spring.version>4.2.4.RELEASE</spring.version> 7 <!-- mybatis版本号 --> 8 <mybatis.version>3.4.1</mybatis.version> 9 <!-- log4j日志文件管理包版本 --> 10 <log4j.version>1.2.17</log4j.version> 11 <!-- jackson版本号 --> 12 <jackson.version>2.8.3</jackson.version> 13 <!--servlet 版本号--> 14 <servlet.version>4.0.1</servlet.version> 15 <!--spring-mybatis版本号--> 16 <mybatis.spring.version>1.3.0</mybatis.spring.version> 17 </properties> 18 19 <dependencies> 20 <dependency> 21 <groupId>junit</groupId> 22 <artifactId>junit</artifactId> 23 <version>4.11</version> 24 <scope>test</scope> 25 </dependency> 26 <!--servlet包--> 27 <dependency> 28 <groupId>javax.servlet</groupId> 29 <artifactId>javax.servlet-api</artifactId> 30 <version>${servlet.version}</version> 31 </dependency> 32 <!-- spring核心包 --> 33 <dependency> 34 <groupId>org.springframework</groupId> 35 <artifactId>spring-core</artifactId> 36 <version>${spring.version}</version> 37 </dependency> 38 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-web</artifactId> 42 <version>${spring.version}</version> 43 </dependency> 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-oxm</artifactId> 47 <version>${spring.version}</version> 48 </dependency> 49 <dependency> 50 <groupId>org.springframework</groupId> 51 <artifactId>spring-tx</artifactId> 52 <version>${spring.version}</version> 53 </dependency> 54 55 <dependency> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-jdbc</artifactId> 58 <version>${spring.version}</version> 59 </dependency> 60 61 <dependency> 62 <groupId>org.springframework</groupId> 63 <artifactId>spring-webmvc</artifactId> 64 <version>${spring.version}</version> 65 </dependency> 66 <dependency> 67 <groupId>org.springframework</groupId> 68 <artifactId>spring-aop</artifactId> 69 <version>${spring.version}</version> 70 </dependency> 71 72 <dependency> 73 <groupId>org.springframework</groupId> 74 <artifactId>spring-context-support</artifactId> 75 <version>${spring.version}</version> 76 </dependency> 77 78 <dependency> 79 <groupId>org.springframework</groupId> 80 <artifactId>spring-test</artifactId> 81 <version>${spring.version}</version> 82 </dependency> 83 <!-- mybatis核心包 --> 84 <dependency> 85 <groupId>org.mybatis</groupId> 86 <artifactId>mybatis</artifactId> 87 <version>${mybatis.version}</version> 88 </dependency> 89 <!-- mybatis/spring包 --> 90 <dependency> 91 <groupId>org.mybatis</groupId> 92 <artifactId>mybatis-spring</artifactId> 93 <version>${mybatis.spring.version}</version> 94 </dependency> 95 <!-- 导入java ee jar 包 --> 96 <dependency> 97 <groupId>javax</groupId> 98 <artifactId>javaee-api</artifactId> 99 <version>7.0</version> 100 </dependency> 101 102 <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 database connection pool--> 103 <dependency> 104 <groupId>commons-dbcp</groupId> 105 <artifactId>commons-dbcp</artifactId> 106 <version>1.4</version> 107 </dependency> 108 <!-- JSTL标签类 --> 109 <dependency> 110 <groupId>jstl</groupId> 111 <artifactId>jstl</artifactId> 112 <version>1.2</version> 113 </dependency> 114 <!-- 日志文件管理包 --> 115 <dependency> 116 <groupId>log4j</groupId> 117 <artifactId>log4j</artifactId> 118 <version>${log4j.version}</version> 119 </dependency> 120 <!--常用解析json包--> 121 <dependency> 122 <groupId>com.alibaba</groupId> 123 <artifactId>fastjson</artifactId> 124 <version>1.2.17</version> 125 </dependency> 126 127 <!-- 上传组件包 --> 128 <dependency> 129 <groupId>commons-fileupload</groupId> 130 <artifactId>commons-fileupload</artifactId> 131 <version>1.3.1</version> 132 </dependency> 133 <dependency> 134 <groupId>commons-io</groupId> 135 <artifactId>commons-io</artifactId> 136 <version>2.5</version> 137 </dependency> 138 <dependency> 139 <groupId>commons-codec</groupId> 140 <artifactId>commons-codec</artifactId> 141 <version>1.10</version> 142 </dependency> 143 <!--aop相关包--> 144 <dependency> 145 <groupId>org.aspectj</groupId> 146 <artifactId>aspectjweaver</artifactId> 147 <version>1.6.12</version> 148 </dependency> 149 <!-- redis 开始 --> 150 <dependency> 151 <groupId>redis.clients</groupId> 152 <artifactId>jedis</artifactId> 153 <version>2.8.0</version> 154 </dependency> 155 156 <dependency> 157 <groupId>org.springframework.data</groupId> 158 <artifactId>spring-data-redis</artifactId> 159 <version>1.6.2.RELEASE</version> 160 </dependency> 161 <dependency> 162 <groupId>org.mybatis</groupId> 163 <artifactId>mybatis-ehcache</artifactId> 164 <version>1.0.0</version> 165 </dependency> 166 <!-- redis 结束--> 167 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 168 <dependency> 169 <groupId>mysql</groupId> 170 <artifactId>mysql-connector-java</artifactId> 171 <version>5.1.10</version> 172 </dependency> 173 174 <dependency> 175 <groupId>org.apache.shiro</groupId> 176 <artifactId>shiro-core</artifactId> 177 <version>1.4.0</version> 178 </dependency> 179 <dependency> 180 <groupId>org.apache.shiro</groupId> 181 <artifactId>shiro-ehcache</artifactId> 182 <version>1.4.0</version> 183 </dependency> 184 <dependency> 185 <groupId>org.apache.shiro</groupId> 186 <artifactId>shiro-spring</artifactId> 187 <version>1.4.0</version> 188 </dependency> 189 <dependency> 190 <groupId>org.apache.shiro</groupId> 191 <artifactId>shiro-web</artifactId> 192 <version>1.4.0</version> 193 </dependency> 194 </dependencies>
jar包
spring整合mybatis配置文件的xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5 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"> 6 7 <!--开启服务层类扫描--> 8 <context:component-scan base-package="com.aaa.ssm.service"></context:component-scan> 9 <!-- 加载数据源配置文件 --> 10 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 11 <property name="location" value="classpath:dbcp.properties"></property> 12 </bean> 13 <!--使用dbcp连接池配置数据源--> 14 <!-- dbcp连接池配置 --> 15 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 16 <property name="driverClassName" value="${jdbc.driver}"></property> 17 <property name="url" value="${jdbc.url}"></property> 18 <property name="username" value="${jdbc.username}"></property> 19 <property name="password" value="${jdbc.password}"></property> 20 <property name="initialSize" value="${initialSize}"></property> 21 <property name="maxActive" value="${maxActive}"></property> 22 <property name="maxIdle" value="${maxIdle}"></property> 23 <property name="minIdle" value="${minIdle}"></property> 24 <property name="maxWait" value="${maxWait}"></property> 25 </bean> 26 27 <!-- 创建SqlSessionFactory --> 28 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 29 <!-- 加载数据源 --> 30 <property name="dataSource" ref="dataSource"></property> 31 <!-- 加载mapper文件 --> 32 <!--<property name="mapperLocations" value="classpath:mapper/*.xml"></property>--> 33 </bean> 34 <!-- 利用配置的mybatis接口和配置文件 把dao实例化 --> 35 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 36 <property name="basePackage" value="com.aaa.ssm.dao"></property> 37 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 38 </bean> 39 <!-- 定义事务管理器 --> 40 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 41 <property name="dataSource" ref="dataSource"/> 42 </bean> 43 <!-- 注解驱动,启动注解 --> 44 <tx:annotation-driven transaction-manager="transactionManager"/> 45 </beans>
spring-mybatis-config.xml
spring整合springMVC配置文件的xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xmlns:task="http://www.springframework.org/schema/task" 7 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 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> 8 <!--开启包扫描--> 9 <context:component-scan base-package="com.aaa.ssm.controller"></context:component-scan> 10 <!--配置springmvc的拦截器 这里删除了--> 11 <!--映射处理器,处理适配器不配置不代表没有,使用默认的了--> 12 <!--上传配置 --> 13 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 14 <!--上传文件的最大限制,单位为字节 --> 15 <property name="maxUploadSize" value="10240000"></property> 16 </bean> 17 <!--统一异常处理 这里删除了--> 18 <!--开启定时任务的注解驱动 扫描有定时注解的方法,根据配置进行执行 这里没有用--> 19 <task:annotation-driven></task:annotation-driven> 20 <!--配置视图解析器--> 21 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 22 <property name="prefix" value="/WEB-INF/jsp/"></property> 23 <property name="suffix" value=".jsp"></property> 24 </bean> 25 </beans>
springmvc-config.xml
spring整合shiro配置文件的xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 <!--这里的id和过滤器的名称匹配 拦截配置工厂--> 6 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 7 <!--设置注入 setter 注入--> 8 <property name="securityManager" ref="securityManager"/> 9 <!-- override these for application-specific URLs if you like: --> 10 <!--未登录的请求拦截后跳转的路径--> 11 <property name="loginUrl" value="/user/toLogin.do"/> 12 <!--登录成功后跳转的页面,但是这里无效,所以下面写什么都行 13 successUrl配置只是作为一种附加配置, 14 只有session中没有用户请求地址时才会使用sucessUrl。 15 系统默认的是认证成功后跳转到上一次请求的路径, 16 如果是首次请求,那shiro就会跳转到默认虚拟路径"/", 17 也就是跳转到index.jsp. 18 这里你写什么都不会跳转 19 --> 20 <property name="successUrl" value="/home.jsp"/> 21 <!--未授权时跳转的页面 做项目的时候最好给配上去,如果前后端分离了又作用不大了--> 22 <property name="unauthorizedUrl" value="/unauthorized.jsp"/> 23 <!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean --> 24 <!-- defined will be automatically acquired and available via its beanName in chain --> 25 <!-- definitions, but you can perform instance overrides or name aliases here if you like: --> 26 <!-- <property name="filters"> 27 <util:map> 28 <entry key="anAlias" value-ref="someFilter"/> 29 </util:map> 30 </property> --> 31 <!--过滤的连接定义,拦截不拦截--> 32 <property name="filterChainDefinitions"> 33 <value> 34 # some example chain definitions(一些例子): 35 # 要放开的请求 36 /css/** = anon 37 /js/** = anon 38 /images/** = anon 39 /user/toLogin.do = anon 40 /user/login.do = anon 41 # 注销方法 42 /logOut = logout 43 # 除了上面配置之外都要拦截 并且这里有个讲究,必须先放行后拦截,拦截必须放在最下面 44 /** = authc 45 # more URL-to-FilterChain definitions here 46 </value> 47 </property> 48 </bean> 49 <!--shiro的所有bean交给spring管理--> 50 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> 51 <!--SecurityManager 核心心脏 安全管理--> 52 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 53 <!-- Single realm app. If you have multiple realms, use the 'realms' property instead. 54 这是使用单个的,如果你要使用多个的 用realms 55 --> 56 <property name="realm" ref="myRealm"/> 57 <!-- By default the servlet container sessions will be used. Uncomment this line 58 to use shiro's native sessions (see the JavaDoc for more): --> 59 <!-- <property name="sessionMode" value="native"/> --> 60 </bean> 61 <!--安全数据桥梁 myRealm--> 62 <bean id="myRealm" class="com.aaa.ssm.util.MyShiroRealm"> 63 <!--<property name="credentialsMatcher" ref="credentialsMatcher" ></property>--> 64 </bean> 65 <!--<!–加密和加盐类–> 66 <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> 67 <property name="hashAlgorithmName" value="SHA-512"></property> 68 <property name="hashIterations" value="1"></property> 69 </bean>--> 70 </beans>
spring-shiro-config.xml
web.xml配置
1 <web-app 2 version="3.0" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 7 <display-name>Archetype Created Web Application</display-name> 8 <!--配置spring监听器 开始--> 9 <listener> 10 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 11 </listener> 12 <context-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value>classpath:spring-*.xml</param-value> 15 <!--<param-value>classpath:spring-mybatis-config.xml,classpath:spring-shiro-config.xml</param-value>--> 16 </context-param> 17 <!--配置spring监听器 结束--> 18 19 <!--shiro过滤器 开始--> 20 <!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.xml 21 当前filter名字必须和spring整合shiro的配置文件中的bean的名字一致--> 22 <filter> 23 <filter-name>shiroFilter</filter-name> 24 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 25 <!--把当前filter交给spring管理--> 26 <init-param> 27 <param-name>targetFilterLifecycle</param-name> 28 <param-value>true</param-value> 29 </init-param> 30 </filter> 31 <!-- Make sure any request you want accessible to Shiro is filtered. /* catches all 32 /* 确保所有的请求都经过shiro filter 33 --> 34 <!-- requests. Usually this filter mapping is defined first (before all others) to 35 有其他的过滤器,通常shiro过滤器放在最上面 36 --> 37 <!-- ensure that Shiro works in subsequent filters in the filter chain: --> 38 <filter-mapping> 39 <filter-name>shiroFilter</filter-name> 40 <url-pattern>/*</url-pattern> 41 </filter-mapping> 42 <!--shiro过滤器 结束--> 43 44 <!--配置过滤器,解决post提交 中文乱码 开始--> 45 <filter> 46 <filter-name>charEncode</filter-name> 47 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 48 <init-param> 49 <param-name>encoding</param-name> 50 <param-value>utf-8</param-value> 51 </init-param> 52 </filter> 53 <filter-mapping> 54 <filter-name>charEncode</filter-name> 55 <url-pattern>/*</url-pattern> 56 </filter-mapping> 57 <!--配置过滤器,解决post提交 中文乱码 结束--> 58 <!--配置总调度器 开始--> 59 <servlet> 60 <servlet-name>springmvc</servlet-name> 61 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 62 <!--加载自定义配置文件--> 63 <init-param> 64 <param-name>contextConfigLocation</param-name> 65 <param-value>classpath:springmvc-config.xml</param-value> 66 </init-param> 67 </servlet> 68 <servlet-mapping> 69 <servlet-name>springmvc</servlet-name> 70 <url-pattern>*.do</url-pattern> 71 </servlet-mapping> 72 <!--配置总调度器 结束--> 73 </web-app>
web.xml
dbcp.properties数据库连接池配置
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/qy107 3 jdbc.username=root 4 jdbc.password=123456 5 #初始连接数 6 initialSize=10 7 #最大活跃数 8 maxActive=5 9 #最大空闲连接 10 maxIdle=1 11 #最小idle数 12 minIdle=1 13 #最长等待时间(毫秒) 14 maxWait=1000
mysql数据库连接池配置