文章目录
一、新建一个Maven的Web空项目使用工具:二、先创建各种常用包,如图所示,之后用什么再添加三、配置 pom.xml 文件(需要引入jar包依赖和配置tomcat服务器)四、配置mysql 8.0.15、druid数据库 db.properties 配置文件五、配置 log4j.properties 日志文件六、配置 springmvc.xml (xml文件都建下图的file类型)七、配置 application-dao.xml七、配置application-service.xml八、配置applicationContext.xml九、配置web.xml
一、新建一个Maven的Web空项目
使用工具:
sts 或 eclipse 集成开发环境 一台电脑 windows 10
之前博客:新建一个Maven的Web项目详细步骤及Maven中的问题和bug全解
二、先创建各种常用包,如图所示,之后用什么再添加
三、配置 pom.xml 文件(需要引入jar包依赖和配置tomcat服务器)
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0
</modelVersion>
<groupId>包名G
</groupId>
<artifactId>项目名A
</artifactId>
<packaging>war
</packaging>
<version>1.0版本号V
</version>
<name>97_shiro_ssm Maven Webapp
</name>
<url>http://maven.apache.org
</url>
<properties>
<servlet.version>3.1.0
</servlet.version>
<jsp.version>2.3.1
</jsp.version>
<jstl.version>1.1.2
</jstl.version>
<mybatis.version>3.5.2
</mybatis.version>
<mybatis-spring.version>1.3.3
</mybatis-spring.version>
<spring.version>4.3.24.RELEASE
</spring.version>
<druid.version>1.1.20
</druid.version>
<mysql.version>8.0.15
</mysql.version>
<jackson.version>2.9.9
</jackson.version>
<pagehelper.version>5.1.8
</pagehelper.version>
<log4j.version>1.2.17
</log4j.version>
<collection4.version>4.3
</collection4.version>
<shiro.version>1.4.1
</shiro.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet.jsp
</groupId>
<artifactId>javax.servlet.jsp-api
</artifactId>
<version>${jsp.version}
</version>
<scope>provided
</scope>
</dependency>
<dependency>
<groupId>javax.servlet
</groupId>
<artifactId>javax.servlet-api
</artifactId>
<version>${servlet.version}
</version>
<scope>provided
</scope>
</dependency>
<dependency>
<groupId>javax.servlet
</groupId>
<artifactId>jstl
</artifactId>
<version>${jstl.version}
</version>
</dependency>
<dependency>
<groupId>taglibs
</groupId>
<artifactId>standard
</artifactId>
<version>${jstl.version}
</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper
</groupId>
<artifactId>pagehelper
</artifactId>
<version>${pagehelper.version}
</version>
</dependency>
<dependency>
<groupId>org.mybatis
</groupId>
<artifactId>mybatis
</artifactId>
<version>${mybatis.version}
</version>
</dependency>
<dependency>
<groupId>org.mybatis
</groupId>
<artifactId>mybatis-spring
</artifactId>
<version>${mybatis-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-jdbc
</artifactId>
<version>${spring.version}
</version>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-webmvc
</artifactId>
<version>${spring.version}
</version>
</dependency>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<version>${mysql.version}
</version>
</dependency>
<dependency>
<groupId>com.alibaba
</groupId>
<artifactId>druid
</artifactId>
<version>${druid.version}
</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core
</groupId>
<artifactId>jackson-databind
</artifactId>
<version>${jackson.version}
</version>
</dependency>
<dependency>
<groupId>log4j
</groupId>
<artifactId>log4j
</artifactId>
<version>${log4j.version}
</version>
</dependency>
<dependency>
<groupId>org.apache.commons
</groupId>
<artifactId>commons-collections4
</artifactId>
<version>${collection4.version}
</version>
</dependency>
<dependency>
<groupId>org.apache.shiro
</groupId>
<artifactId>shiro-spring
</artifactId>
<version>${shiro.version}
</version>
</dependency>
</dependencies>
<build>
<finalName>06_shiro_ssm
</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven
</groupId>
<artifactId>tomcat7-maven-plugin
</artifactId>
<version>2.2
</version>
<configuration>
<uriEncoding>UTF-8
</uriEncoding>
<port>8080
</port>
<path>/名称
</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
四、配置mysql 8.0.15、druid数据库 db.properties 配置文件
#
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/数据库名?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
username=root
password=123456
#
initialSize=5
maxActive=20
minIdle=5
maxIdle=10
filters=stat,wall,log4j
druid 配置参数缺省值说明
initialSize0初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时maxActive8最大连接池数量maxIdle8已经不再使用,配置了也没效果minIdle最小连接池数量filters属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall
五、配置 log4j.properties 日志文件
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=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
六、配置 springmvc.xml (xml文件都建下图的file类型)
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
<context:component-scan base-package="包.controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler/>
</beans>
七、配置 application-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-4.3.xsd">
<context:property-placeholder
location="classpath:db.properties" system-properties-mode="FALLBACK" />
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="initialSize" value="${initialSize}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="minIdle" value="${minIdle}"></property>
<property name="filters" value="${filters}"></property>
</bean>
<bean id="configuration"
class="org.apache.ibatis.session.Configuration">
<property name="logImpl"
value="org.apache.ibatis.logging.stdout.StdOutImpl"></property>
</bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configuration" ref="configuration"></property>
<property name="mapperLocations">
<array>
<value>classpath:mapper/*Mapper.xml
</value>
</array>
</property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor"></bean>
</array>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="包.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
七、配置application-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"
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/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">
<context:component-scan base-package="com.sxt.service.impl"></context:component-scan>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="myAdvise" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="reset*" propagation="REQUIRED"/>
<tx:method name="change*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* 包.service.impl.*.*(..))" id="pc"/>
<aop:advisor advice-ref="myAdvise" pointcut-ref="pc"/>
</aop:config>
</beans>
八、配置applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:application-dao.xml"/>
<import resource="classpath:application-service.xml"/>
</beans>
九、配置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">
<display-name>项目名
</display-name>
<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>
<servlet-name>springmvc
</servlet-name>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
<init-param>
<param-name>contextConfigLocation
</param-name>
<param-value>classpath:springmvc.xml
</param-value>
</init-param>
<load-on-startup>1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc
</servlet-name>
<url-pattern>*.action
</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp
</welcome-file>
<welcome-file>index.htm
</welcome-file>
<welcome-file>index.html
</welcome-file>
<welcome-file>default.html
</welcome-file>
<welcome-file>default.htm
</welcome-file>
<welcome-file>default.jsp
</welcome-file>
</welcome-file-list>
</web-app>
============================ 上述ssm框架已经搭建完毕 =========================
下一篇在此基础上,集成shiro (java安全框架), Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。
搭建Maven、SSM、shiro框架详细步骤