JavaEE权限管理系统的搭建(三)--------springmvc和mabatis整合环境搭建
本节介绍如何环境的搭建和配置:
首先要在父工程引入jar包依赖:
1 <!-- 通过属性定义指定jar的版本 --> 2 <properties> 3 <spring.version>4.2.4.RELEASE</spring.version> 4 <hibernate.version>5.0.7.Final</hibernate.version> 5 <struts2.version>2.3.24</struts2.version> 6 <slf4j.version>1.6.6</slf4j.version> 7 <log4j.version>1.2.12</log4j.version> 8 <shiro.version>1.2.3</shiro.version> 9 <jackson.version>2.7.4</jackson.version> 10 <shiro.version>1.4.0</shiro.version> 11 </properties> 12 13 <dependencies> 14 <dependency> 15 <groupId>org.springframework</groupId> 16 <artifactId>spring-context</artifactId> 17 <version>${spring.version}</version> 18 </dependency> 19 20 <dependency> 21 <groupId>org.springframework</groupId> 22 <artifactId>spring-context-support</artifactId> 23 <version>${spring.version}</version> 24 </dependency> 25 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring-webmvc</artifactId> 29 <version>${spring.version}</version> 30 </dependency> 31 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-web</artifactId> 35 <version>${spring.version}</version> 36 </dependency> 37 38 <dependency> 39 <groupId>org.springframework</groupId> 40 <artifactId>spring-orm</artifactId> 41 <version>${spring.version}</version> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-beans</artifactId> 47 <version>${spring.version}</version> 48 </dependency> 49 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-core</artifactId> 53 <version>${spring.version}</version> 54 </dependency> 55 56 <dependency> 57 <groupId>org.mybatis</groupId> 58 <artifactId>mybatis</artifactId> 59 <version>3.3.0</version> 60 </dependency> 61 <dependency> 62 <groupId>org.mybatis</groupId> 63 <artifactId>mybatis-spring</artifactId> 64 <version>1.2.3</version> 65 </dependency> 66 67 <dependency> 68 <groupId>org.aspectj</groupId> 69 <artifactId>aspectjweaver</artifactId> 70 <version>1.7.4</version> 71 </dependency> 72 73 <!-- struts2 begin --> 74 <dependency> 75 <groupId>org.apache.struts</groupId> 76 <artifactId>struts2-core</artifactId> 77 <version>${struts2.version}</version> 78 <!-- 排除传递的依赖 --> 79 <exclusions> 80 <exclusion> 81 <artifactId>javassist</artifactId> 82 <groupId>javassist</groupId> 83 </exclusion> 84 </exclusions> 85 </dependency> 86 <dependency> 87 <groupId>org.apache.struts</groupId> 88 <artifactId>struts2-spring-plugin</artifactId> 89 <version>${struts2.version}</version> 90 </dependency> 91 <dependency> 92 <groupId>org.apache.struts</groupId> 93 <artifactId>struts2-convention-plugin</artifactId> 94 <version>${struts2.version}</version> 95 </dependency> 96 <!-- struts2 end --> 97 98 <!-- hibernate begin --> 99 <dependency> 100 <groupId>org.hibernate</groupId> 101 <artifactId>hibernate-core</artifactId> 102 <version>${hibernate.version}</version> 103 </dependency> 104 <!-- JSR-303 jar包 --> 105 <dependency> 106 <groupId>org.hibernate</groupId> 107 <artifactId>hibernate-validator</artifactId> 108 <version>5.1.3.Final</version> 109 </dependency> 110 <!-- hibernate end --> 111 112 <!-- log start --> 113 <dependency> 114 <groupId>log4j</groupId> 115 <artifactId>log4j</artifactId> 116 <version>${log4j.version}</version> 117 </dependency> 118 119 <dependency> 120 <groupId>org.slf4j</groupId> 121 <artifactId>slf4j-api</artifactId> 122 <version>${slf4j.version}</version> 123 </dependency> 124 125 <dependency> 126 <groupId>org.slf4j</groupId> 127 <artifactId>slf4j-log4j12</artifactId> 128 <version>${slf4j.version}</version> 129 </dependency> 130 <!-- log end --> 131 132 <dependency> 133 <groupId>org.apache.poi</groupId> 134 <artifactId>poi</artifactId> 135 <version>3.11</version> 136 </dependency> 137 138 <dependency> 139 <groupId>org.apache.cxf</groupId> 140 <artifactId>cxf-rt-frontend-jaxws</artifactId> 141 <version>3.0.1</version> 142 </dependency> 143 <dependency> 144 <groupId>org.apache.cxf</groupId> 145 <artifactId>cxf-rt-transports-http</artifactId> 146 <version>3.0.1</version> 147 </dependency> 148 <dependency> 149 <groupId>junit</groupId> 150 <artifactId>junit</artifactId> 151 <version>4.12</version> 152 <scope>test</scope> 153 </dependency> 154 155 <!-- 加入servlet和jsp的依赖 --> 156 <dependency> 157 <groupId>javax.servlet</groupId> 158 <artifactId>servlet-api</artifactId> 159 <version>2.5</version> 160 <scope>provided</scope> 161 </dependency> 162 <dependency> 163 <groupId>javax.servlet</groupId> 164 <artifactId>jsp-api</artifactId> 165 <version>2.0</version> 166 <scope>provided</scope> 167 </dependency> 168 169 <dependency> 170 <groupId>javax.servlet</groupId> 171 <artifactId>jstl</artifactId> 172 <version>1.2</version> 173 </dependency> 174 175 <!-- 引入fastjson的依赖 --> 176 <dependency> 177 <groupId>com.alibaba</groupId> 178 <artifactId>fastjson</artifactId> 179 <version>1.2.4</version> 180 </dependency> 181 182 <!--jackson依赖--> 183 <dependency> 184 <groupId>com.fasterxml.jackson.core</groupId> 185 <artifactId>jackson-databind</artifactId> 186 <version>${jackson.version}</version> 187 </dependency> 188 <dependency> 189 <groupId>com.fasterxml.jackson.core</groupId> 190 <artifactId>jackson-core</artifactId> 191 <version>${jackson.version}</version> 192 </dependency> 193 <dependency> 194 <groupId>com.fasterxml.jackson.core</groupId> 195 <artifactId>jackson-annotations</artifactId> 196 <version>${jackson.version}</version> 197 </dependency> 198 199 <!-- 引入pinyin4J的依赖 --> 200 <dependency> 201 <groupId>com.belerweb</groupId> 202 <artifactId>pinyin4j</artifactId> 203 <version>2.5.0</version> 204 </dependency> 205 206 <!--apache shiro jar包--> 207 <dependency> 208 <groupId>org.apache.shiro</groupId> 209 <artifactId>shiro-core</artifactId> 210 <version>${shiro.version}</version> 211 </dependency> 212 213 <!-- 引入json-lib的依赖 --> 214 <dependency> 215 <groupId>net.sf.json-lib</groupId> 216 <artifactId>json-lib</artifactId> 217 <version>2.4</version> 218 <classifier>jdk15</classifier> 219 </dependency> 220 221 <!-- 引入c3p0jar包 --> 222 <dependency> 223 <groupId>c3p0</groupId> 224 <artifactId>c3p0</artifactId> 225 <version>0.9.1.2</version> 226 </dependency> 227 228 <!-- 引入ehcache的依赖 --> 229 <dependency> 230 <groupId>net.sf.ehcache</groupId> 231 <artifactId>ehcache-core</artifactId> 232 <version>2.6.6</version> 233 </dependency> 234 <!-- 引入MySQL数据库驱动依赖 --> 235 <dependency> 236 <groupId>mysql</groupId> 237 <artifactId>mysql-connector-java</artifactId> 238 <version>5.1.32</version> 239 </dependency> 240 <dependency> 241 <groupId>org.quartz-scheduler</groupId> 242 <artifactId>quartz</artifactId> 243 <version>2.2.3</version> 244 </dependency> 245 <dependency> 246 <groupId>org.quartz-scheduler</groupId> 247 <artifactId>quartz-jobs</artifactId> 248 <version>2.2.3</version> 249 </dependency> 250 <dependency> 251 <groupId>javax.mail</groupId> 252 <artifactId>mail</artifactId> 253 <version>1.4</version> 254 </dependency> 255 </dependencies> 256 257 <build> 258 <!-- 插件 --> 259 <plugins> 260 <plugin> 261 <groupId>org.apache.maven.plugins</groupId> 262 <artifactId>maven-resources-plugin</artifactId> 263 <version>2.5</version> 264 </plugin> 265 <plugin> 266 <groupId>org.apache.maven.plugins</groupId> 267 <artifactId>maven-compiler-plugin</artifactId> 268 <version>2.3.2</version> 269 <configuration> 270 <source>1.8</source> 271 <target>1.8</target> 272 </configuration> 273 </plugin> 274 <!-- 引入tomcat插件 --> 275 <plugin> 276 <groupId>org.apache.tomcat.maven</groupId> 277 <artifactId>tomcat7-maven-plugin</artifactId> 278 <version>2.2</version> 279 <configuration> 280 <path>/</path> 281 <port>8888</port> 282 </configuration> 283 </plugin> 284 </plugins> 285 <resources> 286 <resource> 287 <directory>src/main/java</directory> 288 <includes> 289 <include>**/*.properties</include> 290 <include>**/*.xml</include> 291 </includes> 292 <filtering>false</filtering> 293 </resource> 294 <resource> 295 <directory>src/main/resources</directory> 296 <includes> 297 <include>**/*.properties</include> 298 <include>**/*.xml</include> 299 </includes> 300 <filtering>false</filtering> 301 </resource> 302 </resources> 303 </build>
在子模块中的web层中添加所需的配置,所需文件如下:
encrypt.properties
algorithmName=sha-256 hashIterations=5 isHex = false
很简单,就三个属性,这个是密码加密配置文件,algorithmName算法名字,hashIterations散列次数,isHex:是否使用十六进制进行编码,在这里做配置,在类中会通过注解@Value的方式进行获取其值。
jdbc.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/xx?useUnicode=true&characterEncoding=utf-8 username=xx password=xx
数据库配置文件,这个就不用介绍了。
log4j.properties
log4j.rootLogger=info, stdout log4j.logger.org.mybatis = info log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
日志配置文件,输出级别为info
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> <!--配置全局属性--> <settings> <!--启用缓存,默认值true--> <setting name="cacheEnabled" value="true"/> <!--启用懒加载,全局开关,所有关联的对象都会延迟加载,默认值false--> <setting name="lazyLoadingEnabled" value="true"/> <!--属性启用时,会使带有延迟加载的属性对象立即加载,反之,会按需加载,默认为true,这里设置了false--> <setting name="aggressiveLazyLoading" value="false"/> <!--是否单一语句返回多结果集(需要兼容驱动),默认值true--> <setting name="multipleResultSetsEnabled" value="true"/> <!--使用列标签代替列名,默认值true--> <setting name="useColumnLabel" value="true"/> <!--允许jdbc支持自动生成主键,默认值false--> <setting name="useGeneratedKeys" value="true"/> </settings> </configuration>
spring-dao.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:context="http://www.springframework.org/schema/context" 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"> <context:component-scan base-package="com.supin51.dao"/> <!--1.使用通配符扫描所有属性文件,包含密码加密、数据库配置、日志配置--> <context:property-placeholder location="classpath:*.properties"/> <!--2.配置数据库相关参数及数据库连接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--配置连接池属性--> <property name="driverClass" value="${driver}" /> <!-- 基本属性 url、user、password --> <property name="jdbcUrl" value="${url}" /> <property name="user" value="${username}" /> <property name="password" value="${password}" /> <!--c3p0私有属性--> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!--关闭连接后不自动commit--> <property name="autoCommitOnClose" value="false"/> <!--获取连接超时时间--> <property name="checkoutTimeout" value="10000"/> <!--当获取连接失败重试次数--> <property name="acquireRetryAttempts" value="2"/> </bean> <!--3.配置SqlSessionFactory对象--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--往下才是mybatis和spring真正整合的配置--> <!--注入数据库连接池--> <property name="dataSource" ref="dataSource"/> <!--配置mybatis全局配置文件:mybatis-config.xml--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!--扫描entity包,使用别名,多个用;隔开--> <property name="typeAliasesPackage" value="com.supin51.domain"/> <!--扫描sql配置文件:mapper需要的xml文件--> <property name="mapperLocations" value="classpath*:com/supin51/mapper/*.xml"/> </bean> <!--4:配置扫描Dao接口包,动态实现DAO接口,注入到spring容器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--注入SqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 给出需要扫描的Dao接口--> <property name="basePackage" value="com.supin51.dao"/> </bean> </beans>
spring-service.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: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.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"> <!--扫描service包下所有使用注解的类型--> <context:component-scan base-package="com.supin51.service.impl"/> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入数据库连接池--> <property name="dataSource" ref="dataSource"/> </bean> <!--配置基于注解的声明式事务,默认使用注解来管理事务行为--> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
spring-mvc.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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"> <!--启动包扫描功能,spring自动扫描base-package包下面的所有java类文件,则把这些类注册为Spring的bean --> <context:component-scan base-package="com.supin51.admin.controller" /> <!-- 自动注册处理映射器RequestMappingHandlerMapping 和处理器适配器RequestMappingHandlerAdapter, 这是springMvc为@controller分发请求必须注册的驱动 --> <mvc:annotation-driven /> <!--使用默认的servlet来响应静态文件 --> <mvc:default-servlet-handler /> <!--<!–静态资源映射–>--> <!--<!–本项目把静态资源放在了webapp的statics目录下,资源映射如下–>--> <!--<mvc:resources mapping="/css/**" location="/statics/css/"/>--> <!--<mvc:resources mapping="/js/**" location="/statics/js/"/>--> <!--<mvc:resources mapping="/image/**" location="/statics/image/"/>--> <!--<mvc:resources mapping="/h-ui/**" location="/statics/h-ui/"/>--> <!--<mvc:resources mapping="/h-ui.admin/**" location="/statics/h-ui.admin/"/>--> <!--<mvc:resources mapping="/lib/**" location="/statics/lib/"/>--> <!--装配文件上传功能所需要的multipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <!-- 最大文件上传字节大小,10MB --> <property name="maxUploadSize"> <value>10485760</value> </property> <!-- 请求的编码格式,默认为ISO-8859-1,必须和jsp页面的pageEncoding一致,以便正确读取表单 --> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean> <!--拦截器定义--> <mvc:interceptors> <!--登录认证拦截器,--> <mvc:interceptor> <!--拦截所有请求--> <mvc:mapping path="/**"/> <!--使用bean自定义一个Interceptor--> <bean class="com.supin51.interceptor.AuthenticationInterceptor"/> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**/*.action"/> <mvc:mapping path="/**/*.action/*"/> <mvc:mapping path="/**/*.json"/> <bean class="com.supin51.interceptor.AuthorizationInterceptor"/> </mvc:interceptor> </mvc:interceptors> <!--视图解析器,解析跳转到/WEB-INF/views/下,spring4.0版本后可以直接忽略,不用配置此项 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--前缀 --> <property name="prefix" value="/WEB-INF/views/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
最后就是webapp下的web.xml文件了
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Archetype Created Web Application</display-name> <!--不拦截静态资源文件--> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.json</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpeg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.icon</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.ico</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.swf</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.eot</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.svg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.ttf</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.woff</url-pattern> </servlet-mapping> <!-- Spring servlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 字符过滤器--> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>/WEB-INF/views/login.jsp</welcome-file> </welcome-file-list> </web-app>
因为要使用Hui-admin前端框架,所以呢,也要把所需的静态文件引入进来,在这里我就不上传了,请到Hui-admin官网下载,http://www.h-ui.net/index.shtml
我是新建了一个statics文件夹,然后把所需的文件放入进来的,我把所有的jsp文件都放入到了views文件夹下。
到这里,整个项目的环境配置搭建,就结束了
运行tomcat看看是否能运行起来。我一般是开发时用maven运行,除非调试的时候,用tomcat调试,maven的好处是在编写jsp文件时,改完后不需要重新运行项目,刷新即可看见效果。
另外一种运行方式,项目既然使用了maven,那肯定也得用maven来运行,之前在父项目的pom文件中已经加入了tomcat-maven的插件,回顾一下这段代码,<path>/</path> /映射根路径 ,这样子,访问时就直接:http://localhost:8888 <port>8888</port>,端口被配置成了8888
<!-- 插件 --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- 引入tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/</path> <port>8888</port> </configuration> </plugin> </plugins>
右击父项目,构建一下工程
然后进行maven install
最后点击Web层的plugins中的tomcat7:run运行项目
本小节结束。下一节讲解,权限的RBAC模型,数据库文件关系。实现登录认证还有加密