SSM+shiro+maven整合配置

来源:互联网 发布:表白网页制作软件 编辑:程序博客网 时间:2024/05/01 15:02

整合思路

这里写图片描述

第一步:整合dao层

mybatis和spring整合,通过spring管理mapper接口。
使用mapper的扫描器自动扫描mapper接口在spring中进行注册。

第二步:整合service层

通过spring管理service接口。
使用配置方式将service接口配置在spring配置文件中。
实现事务控制。

第三步:整合springmvc

由于springmvc是spring的模块,不需要整合。

建包

包含几个子包:

controller
mapper
po
service
serviceimpl
大概样子是这样:

这里写图片描述

添加依赖

pom.xml

<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.zly</groupId>    <artifactId>zhouliyuan</artifactId>    <version>0.0.1-SNAPSHOT</version>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <!-- jar 版本设置 -->        <spring.version>4.3.3.RELEASE</spring.version>    </properties>    <dependencies>        <!-- 分页 开始 -->        <dependency>            <groupId>com.github.pagehelper</groupId>            <artifactId>pagehelper</artifactId>            <version>4.1.1</version>        </dependency>        <!-- 分页 开始 -->        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-validator</artifactId>            <version>5.2.4.Final</version>        </dependency>        <!-- shiro 开始 -->        <dependency>            <groupId>org.apache.shiro</groupId>            <artifactId>shiro-spring</artifactId>            <version>1.2.3</version>        </dependency>        <dependency>            <groupId>org.apache.shiro</groupId>            <artifactId>shiro-web</artifactId>            <version>1.2.2</version>        </dependency>        <!-- shiro 结束 -->        <dependency>            <groupId>javax</groupId>            <artifactId>javaee-api</artifactId>            <version>7.0</version>            <scope>provided</scope>        </dependency>        <!-- spring框架 -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-tx</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-oxm</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-orm</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aspects</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jdbc</artifactId>            <version>${spring.version}</version>        </dependency>        <!-- MySQL依赖 -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.38</version>        </dependency>        <!-- 加入MyBatis 依赖 -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.3.1</version>        </dependency>        <!-- 引用插件依赖:MyBatis整合Spring -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-spring</artifactId>            <version>1.3.0</version>        </dependency>        <dependency>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>            <version>1.2.17</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-api</artifactId>            <version>1.7.18</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>druid</artifactId>            <version>1.0.18</version>        </dependency>        <!-- JSP tag -->        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>jstl</artifactId>            <version>1.2</version>        </dependency>        <dependency>            <groupId>taglibs</groupId>            <artifactId>standard</artifactId>            <version>1.1.2</version>        </dependency>    </dependencies></project>

这里导了很多包,我觉得 多了也不多,但是少了就不行 所以我把平常能用到的都基本导了进来。

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    id="WebApp_ID" version="3.0">    <display-name>zhouliyuan</display-name>    <welcome-file-list>        <welcome-file>index.html</welcome-file>        <welcome-file>index.htm</welcome-file>        <welcome-file>index.jsp</welcome-file>        <welcome-file>default.html</welcome-file>        <welcome-file>default.htm</welcome-file>        <welcome-file>default.jsp</welcome-file>    </welcome-file-list>    <!-- 配置静态资源访问被核心控制拦截 -->    <!-- 需要添加springmvc所需要的核心控制器 -->    <!-- 配置springmvc的xml文件路径 -->    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.html</url-pattern>        <url-pattern>*.js</url-pattern>        <url-pattern>*.css</url-pattern>        <url-pattern>*.png</url-pattern>        <url-pattern>*.jpg</url-pattern>        <url-pattern>*.gif</url-pattern>    </servlet-mapping>    <!-- 配置SpringMVC核心控制器: -->    <servlet>        <servlet-name>springDispatcherServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!-- 配置DispatcherServlet的初始化參數:设置文件的路径和文件名称 -->        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring/springmvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springDispatcherServlet</servlet-name>        <!-- 第一种:*.action,访问以.action三结尾,由DispatcherServlet进行解析 第二种:/,所有访问的地址由DispatcherServlet进行解析,对静态文件的解析需要配置不让DispatcherServlet进行解析,             使用此种方式和实现RESTful风格的url 第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时,仍然会由DispatcherServlet解析jsp地址,             不能根据jsp页面找到handler,会报错 -->        <url-pattern>/</url-pattern>    </servlet-mapping>    <!-- 当服务器已启动就需要使用到ioc容器的功能,所以启动的时候就需要初始化ioc容器对象,我们可以通过对servletcontext的监听器来实现 -->    <!-- spring已经提供了这么监听器,我们只需要对他进行注册 -->    <!-- 监听serviletcontext对象,application启动的时候自动ioc容器初始化  -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- ContextLoaderListener中初始化ioc容器, 自动读取bean所在文件的时候默认的路径是/WEB-INF/applicationContext.xml.          我们可以通过以下修改来修改读取applicationContext.xml的路径  -->    <!-- 加载spring容器 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring/applicationContext-*.xml</param-value>    </context-param>    <!-- shiro start -->    <!-- 和单独使用shiro和web整合不一样,用的不是shiro的shirofilter -->    <filter>        <filter-name>shiroFilter</filter-name>        <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>    <!-- shiro end -->    <!-- 处理post方式乱码的过滤器 这里写了 那么 controller就不用写@ModelAttribute了 -->    <filter>        <filter-name>encodingFilter</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>    </filter>    <filter-mapping>        <filter-name>encodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping></web-app>

db.properties

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/1713ssmzhouliyuan?characterEncoding=utf-8jdbc.username=rootjdbc.password=123456

log4j.properties

# Global logging configurationlog4j.rootLogger=DEBUG, stdout# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

mybatis–>sqlMapConfig.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>    <!-- 指定别名的 只需要指定pojo类所存在的包名,spring会自动读取 包下所有类 并且 以类的名字定义别名。 也就是在这儿定义pojo的包,那么mapper包下的.xml         写的时候 只需要写pojo的类名就行了。 -->    <!-- 全局setting配置,根据需要添加 -->    <!-- 配置别名 -->    <typeAliases>        <!-- 批量扫描别名 -->        <package name="com.zly.pojo" />    </typeAliases>    <!-- 配置mapper 由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。 必须遵循:mapper.xml和mapper.java文件同名且在一个目录 -->    <!-- <mappers> </mappers> -->    <!-- 分页 -->    <plugins>        <!-- 引用pagerhelper插件 -->        <plugin interceptor="com.github.pagehelper.PageHelper">            <property name="dialect" value="mysql" />            <!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->            <property name="returnPageInfo" value="always" />            <!-- 设置为true时,使用RowBounds分页会进行count查询 -->            <property name="rowBoundsWithCount" value="true" />            <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->            <property name="pageSizeZero" value="true" />            <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->            <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->            <property name="reasonable" value="true" />            <!-- 支持通过Mapper接口参数来传递分页参数 -->            <property name="supportMethodsArguments" value="true" />        </plugin>    </plugins></configuration>

shiro–>springmvc.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc" 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.3.xsd    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">    <!-- 对于注解的Handler 可以单个配置 实际开发中加你使用组件扫描 -->    <!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 -->    <context:component-scan base-package="com.zly.controller"></context:component-scan>    <!-- 使用mvc:anno tation-driven代替上面两个注解映射器和注解适配的配置 mvc:annotation-driven默认加载很多的参数绑定方法,         比如json转换解析器默认加载了,如果使用mvc:annotation-driven则不用配置上面的RequestMappingHandlerMapping和RequestMappingHandlerAdapter         实际开发时使用mvc:annotation-driven -->    <mvc:annotation-driven></mvc:annotation-driven>    <!-- 视图解析器 解析jsp,默认使用jstl,classpath下要有jstl的包 -->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!-- 配置jsp路径的前缀 -->        <property name="prefix" value="/" />        <!-- 配置jsp路径的后缀 -->        <property name="suffix" value=".jsp" />    </bean></beans>

spring–>shiro-spring.xml

<?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:p="http://www.springframework.org/schema/p"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">    <!-- beagin  shiro自身运行需要的bean -->    <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>    <!-- 数据库保存的密码是使用MD5算法加密的,所以这里需要配置一个密码匹配对象 -->    <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.Md5CredentialsMatcher"></bean>    <!-- 缓存管理 -->    <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>    <!-- end shiro自身运行需要的bean -->    <!-- 使用shiro提供的jbdcrealm.把他定义成一个bean -->    <bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">        <!--添加加密需要的环境变量 -->        <!-- <property name="credentialsMatcher" ref="credentialsMatcher"></property> --><!-- 开始权限查询 -->        <property name="permissionsLookupEnabled" value="true"></property>        <property name="dataSource" ref="dataSource"></property>        <property name="authenticationQuery" value="SELECT upassword FROM users WHERE uname = ?"></property>        <property name="userRolesQuery"            value="select rolename from  u_r  left join roles using(rid)  left join users using(uid) where uname=?"></property>        <property name="permissionsQuery"            value="select pname from r_p  left join roles using(rid) left join permissons using(pid) where rolename=?"></property>    </bean>    <!-- Shiro安全管理器 -->    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">        <property name="realm" ref="jdbcRealm"></property>        <!-- shiro运行需要的缓存管理器 -->        <property name="cacheManager" ref="cacheManager"></property>        <!-- 配置多个Realm,单个real不需要 -->        <!-- <property name="authenticator" ref="authenticator"></property> -->    </bean>    <!-- id的值一定要与web.xml中定义的过滤器名字一样 -->    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">        <!-- Shiro的核心安全接口,这个属性是必须的 -->        <property name="securityManager" ref="securityManager"></property>        <!-- 要求登录时的链接(登录页面地址),非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面 -->        <property name="loginUrl" value="/login.html"></property>        <!-- 登录成功后要跳转的连接(本例中此属性用不到,因为登录成功后的处理逻辑在LoginController里硬编码) -->        <!-- <property name="successUrl" value="/" ></property> -->        <!-- 用户访问未对其授权的资源时,所显示的连接 -->        <property name="unauthorizedUrl" value="/500.html"></property>        <property name="filterChainDefinitions">            <!-- 相当于[urls]中的配置 -->            <value>                /login.html=anon                /login=anon                /main/vip/**=roles["vip3","vip1","vip2"]                /main/**=authc                /deleteById=roles["vip3"]            </value>        </property>    </bean>    <!-- 定义异常处理的页面 -->    <bean        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">        <property name="exceptionMappings">            <props>                <!--登录 -->                <prop key="org.apache.shiro.authz.UnauthenticatedException">                    redirect:/login.html                </prop>                <!--授权 -->                <prop key="org.apache.shiro.authz.UnauthorizedException">                    redirect:/500.html                </prop>            </props>        </property>        <property name="defaultErrorView" value="redirect:/500.html" />    </bean>    <!-- 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类,         并在必要时进行安全逻辑验证 -->    <bean        class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>    <bean        class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">        <property name="securityManager" ref="securityManager"></property>    </bean></beans>

spring–>applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    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.3.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">    <!--spring中 用于开始 注解的配置,必须有这句话才会使注解生效 -->    <context:component-scan base-package="com.zly"></context:component-scan>    <!-- 配置 读取properties文件 db.properties -->    <context:property-placeholder location="classpath:db.properties" />    <!-- 配置 数据源 阿里巴巴的Druid -->    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">        <property name="driverClassName" value="${jdbc.driver}" />        <property name="url" value="${jdbc.url}" />        <property name="username" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />    </bean>    <!-- 配置SqlSessionFactory -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 设置MyBatis核心全局配置文件 -->        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />        <!-- 设置数据源 -->        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 配置Mapper扫描器 -->    <!-- MapperScannerConfigurer 是插件包中的类其目的是接收指定的dao 类所在的路径 然后把包下所有的j接口都当作mapper对象。         也就是mapper包下 的...Dao.java等文件 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <!-- 设置Mapper扫描包 -->        <property name="basePackage" value="com.zly.mapper" />        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />    </bean>    <!-- 应用shiro的配置 -->    <import resource="shiro-spring.xml" /></beans>

spring–>applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    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.3.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">    <!-- 配置Service扫描 -->    <bean id="userService" class="com.zly.service.impl.UserServiceImpl"/></beans>

spring–>applicationContext-transaction.xml

<?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: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/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">    <!-- 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类 -->    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <!-- 数据源 dataSource在applicationContext-dao.xml中配置了 -->        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 通知 -->    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <!-- 传播行为 -->            <tx:method name="save*" propagation="REQUIRED" />            <tx:method name="delete*" propagation="REQUIRED" />            <tx:method name="insert*" propagation="REQUIRED" />            <tx:method name="update*" propagation="REQUIRED" />            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />        </tx:attributes>    </tx:advice>    <!-- aop -->    <aop:config>        <aop:advisor advice-ref="txAdvice"            pointcut="execution(* com.zly.service.impl.*.*(..))" />    </aop:config></beans>

其他的是逆向工程 各种层之间的关联