1. 项目环境准备
1.1. 技术选型
整个项目使用的SSM框架 SpringMVC,Spring,MyBatis,MySql数据库,Shiro安全框架
1.2. 使用Maven创建Web项目
|
1.3. 引入maven项目pom文件依赖的和插件配置
Maven的pom文件集成了整个项目所需要的绝大部分框架依赖,后期其他依赖在实际开发中按需引入集合
<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>cn.zj</groupId> <artifactId>logistics_system</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <spring.version>4.3.2.RELEASE</spring.version> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.17</log4j.version> <mybatis.version>3.4.4</mybatis.version> <mybatis-spring.version>1.3.2</mybatis-spring.version> <druid.version>1.1.10</druid.version> <shiro.version>1.2.3</shiro.version> <mybatis-pagehelper.version>5.1.8</mybatis-pagehelper.version> <jdbcmysql.version>5.1.26</jdbcmysql.version> <junit.version>4.12</junit.version> <jstl.version>1.2</jstl.version> <jackson.version>2.9.5</jackson.version> <aspect.version>1.7.4</aspect.version> <servlet.version>3.1.0</servlet.version> <jsp.version>2.2.1</jsp.version> </properties> <dependencies> <!-- spring --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspect.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</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-test</artifactId> <version>${spring.version}</version> </dependency>
<!-- log4j日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency>
<!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency>
<!-- mybatis的分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${mybatis-pagehelper.version}</version> </dependency>
<!-- mybatis和spring集成包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency>
<!-- 加入servlet和jsp的依赖 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency>
<dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency>
<!-- 引入shiro框架的依赖 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-quartz</artifactId> <version>${shiro.version}</version> </dependency> <!-- shiro和spring集成包 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> <!-- MySQL数据库驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${jdbcmysql.version}</version> </dependency>
<!-- 缓存依赖 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.0</version> </dependency>
<!-- jstl标签库 -->
<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency>
<!-- jackson json转换工具 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency>
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> </dependency>
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency> <!-- 单元测试jar包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/logistics</path> <port>8080</port> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> </project> |
2. 框架集成配置
整个框架的 Spring,SpringMVC采用 xml方式配置(也可以自定义使用注解配置),
所以需要创建准备对应的xml配置文件
spring.xml |
Spring框架的主配置文件,主要 |
springmvc.xml |
SpringMVC配置文件 |
db.properties |
数据库连接配置文件 |
log4j.properties |
Log4j日志配置文件 |
其他配置文件后面使用到按需配置
使用Web项目,需要将框架位置到web.xml中才能生效
2.1. spring.xml文件配置
Spring.xml配置文件,主要配置spring注解配置包扫描组件位置,MyBatis代理对象创建
事务管理的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<!-- 配置包扫描位置 --> <context:component-scan base-package="cn.zj.logistics" />
<!-- 读取 db.properties数据库配置文件 --> <context:property-placeholder location="classpath:db.properties" />
<!-- 配置 druid 连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <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="maxActive" value="${jdbc.maxActive}" /> </bean>
<!-- 配置SqlSessionFactory对象的创建 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入插件 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> <!-- 方言 --> helperDialect=mysql </value> </property> </bean> </array> </property> <property name="dataSource" ref="dataSource" />
<!-- 配置映射文件 --> <property name="mapperLocations"> <array> <value>classpath:cn/sxt/edu/mapper/*Mapper.xml</value> </array> </property>
<!-- 配置别名,使用包扫描配置 --> <property name="typeAliasesPackage" value="cn.zj.logistics.pojo" />
<!-- 读取mybatis-config.xml文件(使用个性化配置,日志,缓存等等) --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean>
<!-- 使用包扫描的方式创包下面所有接口对应的代理对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 配置包扫描创建代理对象的位置 --> <property name="basePackage" value="cn.zj.logistics.mapper" />
<!-- 注意: 需要注入SqlSessionFactory工厂对象的名称 !!! --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>
<!-- MyBatis的事务配置 -->
<!-- 配置事务管理器 : what? -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource" /> </bean>
<!-- Spring事务配置 : when? --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- dql操作,一般都是只读事务 --> <tx:method name="get*" read-only="true" propagation="REQUIRED" /> <tx:method name="find*" read-only="true" propagation="REQUIRED" /> <tx:method name="select*" read-only="true" propagation="REQUIRED" /> <tx:method name="query*" read-only="true" propagation="REQUIRED" /> <!-- dml操作,非只读事务 --> <tx:method name="*" read-only="false" /> </tx:attributes> </tx:advice> <!-- 配置AOP切面,将事务切到Service层 --> <aop:config> <!-- 切入点 :where? --> <aop:pointcut expression="execution(* cn.zj.logistics.service.impl.*.*(..))" id="pt" /> <!-- 切面:切入点+通知 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" /> </aop:config> </beans>
|
2.2. springmvc.xml配置
Springmvc.xml主要配置一些springmvc框架相关配置,开启注解驱动,视图解析器,静态资源处理等等
<?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.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 ">
<!-- 开启SpringMVC的注解驱动 --> <mvc:annotation-driven />
<!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 支持静态资源处理 --> <mvc:default-servlet-handler/>
</beans> |
2.3. db.properties配置文件
数据库连接配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/logistics?characterEncoding=utf-8 jdbc.username=root jdbc.password=root jdbc.maxActive=10 |
2.4. log4j.properties日志配置文件
# Global logging configuration log4j.rootLogger=ERROR, stdout # MyBatis logging configuration... log4j.logger.cn.zj.logistics=TRACE # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n |
2.5. web.xml
Web.xml作为动态web项目的入口文件,spring和springmvc框架集成相关配置需要放到在此文件下配置
<?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_2_5.xsd" version="2.5">
<!-- 配置spring的授权过滤器,让spring来管理shiro框架 --> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <!-- 配置shirofilter的生命周期交给servlet管理 --> <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>
<!-- 字符编码过滤器 --> <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>
<!-- 1.配置SpringMVC的前端控制器(总控) --> <servlet> <servlet-name>MVC</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>MVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
<!-- 设置项目的欢迎页面 --> <welcome-file-list> <welcome-file>/login.jsp</welcome-file> </welcome-file-list> </web-app> |
1.1. Springmvc框架集成的测试
1.1.1. 新建一个AdminController
package cn.zj.logistic.controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
@Controller @RequestMapping("/admin") public class AdminController {
//后台用户列表显示页面 @RequestMapping("/adminPage.do") public String adminPage() {
return "admin_list"; } } |
1.1.2. adminPage.jsp管理员操作jsp文件
因为springmvc框架配置了视图解析器,所有SpringMVC控制器请求默认都请求转发
到 WEB-INF/view 目录下面
在WEB-INF目录下面新建一个view目录,在下面再创建一个 adminPage.jsp文件
|
来源:https://www.cnblogs.com/ruckly/p/11924223.html