web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- http://localhost:8080/shopping_ssm -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<display-name>Archetype Created Web Application</display-name>
<!-- 1.自定义spring核心配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自定义SpringMVC核心配置文件 局部 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:springMVC.xml
</param-value>
</init-param>
<!-- 让tomcat在启动时就初始化DispatcherServlet实例 -->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 5. 作为 Shiro整合的Spring的入口 -->
<!--<filter>
<!– 这个名字不要随便改, 后面要跟IoC容器中的bean对相应 –>
<filter-name>shiroFilter</filter-name>
<!– 默认这个filter的生命周期由IoC容器管理 (false), 现在我们需要将DelegatingFilterProxy的生命周期交由web容器tomcat管理 –>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>-->
<!--3. spring 的 listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 4. Post请求字符编码过滤器 -->
<filter>
<filter-name>character</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!-- encoding是CharacterEncodingFilter类中的属性,不能写错 -->
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>character</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
复制代码
* applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
">
<context:property-placeholder location="classpath:*.properties"/>
<context:component-scan base-package="com.zte.shopping.service.impl"/>
<!-- 3. 配置DataSource -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
</bean>
<!--mybatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.zte.shopping.entity"/>
<property name="mapperLocations">
<list>
<value>classpath:mapper/*Mapper.xml</value>
</list>
</property>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zte.shopping.dao"/>
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>-->
</bean>
` <!--配置通知-->`
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="regist" propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.zte.shopping.service.impl.*ServiceImpl.*(..))" id="pc"/>
<aop:advisor advice-ref="transactionInterceptor" pointcut-ref="pc" />
</aop:config>
</beans>
复制代码
* springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
">
<!-- Spring工作流程的配置 扫描到包名 -->
<context:component-scan base-package="com.zte.shopping.controller" />
<!--<context:component-scan base-package="java.com.zte.shopping.global" />-->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 访问静态资源 default or WebRoot/js/jquery-1.9.1.min.js -->
<!--<mvc:resources location="" mapping="js/**" />-->
<mvc:default-servlet-handler/>
<!--<mvc:resources mapping="/images/**" location="/images/**"/>-->
<!-- <mvc:resources mapping="/js/**" location="/js/**" />
<mvc:resources mapping="/css/**" location="/css/**" />-->
<!--<mvc:view-controller path="/showMain" view-name="backend/main" />-->
<!-- http://localhost:8080/shopping_ssm/showLogin 跳转到 /WEB-INF/pages/backend/login.jsp 后台登录首页-->
<!--<mvc:view-controller path="/showLogin" view-name="backend/login" />-->
<mvc:view-controller path="/showMain" view-name="main" />
</beans>
复制代码
* applicationContext_shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
">
<!-- Shiro的配置 -->
<!-- Shiro的配置 -->
<!--<context:component-scan base-package="service.impl" />-->
<!--????扫描-->
<!-- 1.配置Shiro核心 SecurityManager -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 注入多个jdbcRealm -->
<property name="realms">
<list>
<ref bean="usernameRealm" />
<ref bean="phoneRealm" />
<ref bean="emailRealm" />
</list>
</property>
</bean>
<!-- 2. 用于处理Shiro中登录验证 角色验证 权限验证:ShiroFilterFactoryBean -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 2.1 注入securityManager -->
<property name="securityManager" ref="securityManager" />
<!-- <property name="loginUrl" value="index.jsp" /> -->
<!-- 没有验证统一进入/showLogin指定的页面 -->
<property name="loginUrl" value="/showLogin" />
<!-- 未授权 跳转的页面 -->
<property name="unauthorizedUrl" value="/unauthorizedUrl.jsp" />
<!-- 2.2 注入验证规则(做 登录验证 or 角色验证 or 权限验证) -->
<property name="filterChainDefinitions">
<value>
<!-- 配置登录验证 登录页面 和 登录功能 不需要验证 首页/main需要验证-->
/showLogin=anon
/login=anon
/main=authc
/student/view=authc,roles[student],perms[student:view]
/student/delete=authc,roles[student],perms[student:delete]
/student/**=authc,roles[student]
/teacher/**=authc,roles[teacher]
</value>
</property>
</bean>
<!-- 3. 配置jdbcRealm -->
<!-- 支持 用户名 登录 -->
<bean id="usernameRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<property name="dataSource" ref="dataSource" />
<!-- 登录验证 -->
<property name="authenticationQuery">
<value>select password from t_user where username = ?</value>
</property>
<!-- 角色验证 -->
<property name="userRolesQuery">
<value>
select
r.rolename
from
t_user u
left join
t_user_role ur
on
u.id = ur.user_id
left join
t_roles r
on
r.id = ur.role_id
where
username = ?
</value>
</property>
<!-- 权限验证 -->
<!-- 开启权限验证 -->
<property name="permissionsLookupEnabled" value="true" />
<!-- 自定义权限验证 -->
<property name="permissionsQuery">
<value>
select
*
from
t_roles r
left join
t_role_permission rp
on
r.id = rp.role_id
left join
t_permissions p
on
p.id = rp.permisson_id
where
r.rolename = ?
</value>
</property>
</bean>
<!-- 支持 手机号 登录 -->
<bean id="phoneRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<!-- <property name="dataSource" ref="dataSource" />-->
<property name="authenticationQuery">
<value>select password from t_user where phone = ?</value>
</property>
<!-- 角色验证 -->
<property name="userRolesQuery">
<value>
select
r.rolename
from
t_user u
left join
t_user_role ur
on
u.id = ur.user_id
left join
t_roles r
on
r.id = ur.role_id
where
username = ?
</value>
</property>
<!-- 权限验证 -->
<!-- 开启权限验证 -->
<property name="permissionsLookupEnabled" value="true" />
<!-- 自定义权限验证 -->
<property name="permissionsQuery">
<value>
select
*
from
t_roles r
left join
t_role_permission rp
on
r.id = rp.role_id
left join
t_permissions p
on
p.id = rp.permisson_id
where
r.rolename = ?
</value>
</property>
</bean>
<!-- 支持 邮箱 登录 -->
<bean id="emailRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<property name="dataSource" ref="dataSource" />
<property name="authenticationQuery">
<value>select password from t_user where email = ?</value>
</property>
<!-- 角色验证 -->
<property name="userRolesQuery">
<value>
select
r.rolename
from
t_user u
left join
t_user_role ur
on
u.id = ur.user_id
left join
t_roles r
on
r.id = ur.role_id
where
username = ?
</value>
</property>
<!-- 权限验证 -->
<!-- 开启权限验证 -->
<property name="permissionsLookupEnabled" value="true" />
<!-- 自定义权限验证 -->
<property name="permissionsQuery">
<value>
select
*
from
t_roles r
left join
t_role_permission rp
on
r.id = rp.role_id
left join
t_permissions p
on
p.id = rp.permisson_id
where
r.rolename = ?
</value>
</property>
</bean>
</beans>
复制代码
* dataSource.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/shoppingssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
复制代码
* log4j.properties
log4j.appender.myconsole=org.apache.log4j.ConsoleAppender
log4j.appender.myconsole.layout=org.apache.log4j.PatternLayout
log4j.appender.myconsole.layout.ConversionPattern=%-5p %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.PreparedStatemant=DEBUG
log4j.rootLogger=debug,myconsole
复制代码
* mybatis-config.xml
<?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>
<plugins>
<!-- 配置mybatis分页插件 -->
<!-- com.github.pagehelper为PageHelper类所在包名 只能是4.0版本???-->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 4.0.0以后版本可以不设置该参数 -->
<property name="dialect" value="mysql"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
<!-- 和startPage中的pageNum效果一样-->
<property name="offsetAsPageNum" value="true"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true"/>
<!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
<!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
<property name="pageSizeZero" value="true"/>
<!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
<!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
<!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
<property name="reasonable" value="true"/>
<!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
<!-- 增加了一个params参数来配置参数映射,用于从Map或ServletRequest中取值 -->
<!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 -->
<!-- 不理解该含义的前提下,不要随便复制该配置 -->
<property name="params" value="pageNum=pageHelperStart;pageSize=pageHelperRows;"/>
<!-- 支持通过Mapper接口参数来传递分页参数 -->
<property name="supportMethodsArguments" value="true"/>
<!-- 总是返回PageInfo类型-->
<property name="returnPageInfo" value="true"/>
</plugin>
</plugins>
</configuration>
复制代码