搭建Maven、SSM框架详细步骤

文章目录

    • 一、新建一个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全解

二、先创建各种常用包,如图所示,之后用什么再添加

搭建Maven、SSM框架详细步骤_第1张图片

三、配置 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.0modelVersion>
  <groupId>包名GgroupId>
  <artifactId>项目名AartifactId>
  
  <packaging>warpackaging>
  <version>1.0版本号Vversion>
  <name>97_shiro_ssm Maven Webappname>
  <url>http://maven.apache.orgurl>
  
  

	<properties>
		<servlet.version>3.1.0servlet.version>
		<jsp.version>2.3.1jsp.version>
		<jstl.version>1.1.2jstl.version>
		<mybatis.version>3.5.2mybatis.version>
		<mybatis-spring.version>1.3.3mybatis-spring.version>
		<spring.version>4.3.24.RELEASEspring.version>
		<druid.version>1.1.20druid.version>
		<mysql.version>8.0.15mysql.version>
		<jackson.version>2.9.9jackson.version>
		<pagehelper.version>5.1.8pagehelper.version>
		
		<log4j.version>1.2.17log4j.version>
		<collection4.version>4.3collection4.version>
		<shiro.version>1.4.1shiro.version>
	properties>
	

	<dependencies>
	
		
		<dependency>
			<groupId>javax.servlet.jspgroupId>
			<artifactId>javax.servlet.jsp-apiartifactId>
			<version>${jsp.version}version>
			<scope>providedscope>
		dependency>
		
		
		<dependency>
			<groupId>javax.servletgroupId>
			<artifactId>javax.servlet-apiartifactId>
			<version>${servlet.version}version>
			<scope>providedscope>
		dependency>

		
		<dependency>
			<groupId>javax.servletgroupId>
			<artifactId>jstlartifactId>
			<version>${jstl.version}version>
		dependency>
		<dependency>
			<groupId>taglibsgroupId>
			<artifactId>standardartifactId>
			<version>${jstl.version}version>
		dependency>

		
		<dependency>
			<groupId>com.github.pagehelpergroupId>
			<artifactId>pagehelperartifactId>
			<version>${pagehelper.version}version>
		dependency>
		
		
		<dependency>
			<groupId>org.mybatisgroupId>
			<artifactId>mybatisartifactId>
			<version>${mybatis.version}version>
		dependency>
		
		
		<dependency>
			<groupId>org.mybatisgroupId>
			<artifactId>mybatis-springartifactId>
			<version>${mybatis-spring.version}version>
		dependency>
		
		
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-aspectsartifactId>
			<version>${spring.version}version>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-jdbcartifactId>
			<version>${spring.version}version>
		dependency>

		
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-webmvcartifactId>
			<version>${spring.version}version>
		dependency>
		
		
		<dependency>
			<groupId>mysqlgroupId>
			<artifactId>mysql-connector-javaartifactId>
			<version>${mysql.version}version>
		dependency>
		
		
		<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>druidartifactId>
			<version>${druid.version}version>
		dependency>

		
		<dependency>
			<groupId>com.fasterxml.jackson.coregroupId>
			<artifactId>jackson-databindartifactId>
			<version>${jackson.version}version>
		dependency>
		
		
		<dependency>
			<groupId>log4jgroupId>
			<artifactId>log4jartifactId>
			<version>${log4j.version}version>
		dependency>

		
		<dependency>
			<groupId>org.apache.commonsgroupId>
			<artifactId>commons-collections4artifactId>
			<version>${collection4.version}version>
		dependency>
		
		
		<dependency>
			<groupId>org.apache.shirogroupId>
			<artifactId>shiro-springartifactId>
			<version>${shiro.version}version>
		dependency>
	dependencies>
	
	<build>
		<finalName>06_shiro_ssmfinalName>
		
		<plugins>
			<plugin>
				
				<groupId>org.apache.tomcat.mavengroupId>
				<artifactId>tomcat7-maven-pluginartifactId>
				<version>2.2version>
				<configuration>
					<uriEncoding>UTF-8uriEncoding> 
					<port>8080port>
					<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 配置参数 缺省值 说明
initialSize 0 初始化时建立物理连接的个数。初始化发生在显示调用init方法,
或者第一次getConnection时
maxActive 8 最大连接池数量
maxIdle 8 已经不再使用,配置了也没效果
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类型)

搭建Maven、SSM框架详细步骤_第2张图片


<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


<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.xmlvalue>
			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


<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


<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


<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>EncodingFilterfilter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
		
		<init-param>
			<param-name>encodingparam-name>
			<param-value>UTF-8param-value>
		init-param>
	filter>

	<filter-mapping>
		<filter-name>EncodingFilterfilter-name>
		
		<servlet-name>springmvcservlet-name>
	filter-mapping>


	

	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
	listener>
	<context-param>
		<param-name>contextConfigLocationparam-name>
		<param-value>classpath*:applicationContext.xmlparam-value>
	context-param>
	


	
	<servlet>
		<servlet-name>springmvcservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:springmvc.xmlparam-value>
		init-param>
		
		<load-on-startup>1load-on-startup>
	servlet>
	<servlet-mapping>
		<servlet-name>springmvcservlet-name>
		<url-pattern>*.actionurl-pattern>
	servlet-mapping>
	

	<welcome-file-list>
		<welcome-file>index.jspwelcome-file>
		<welcome-file>index.htmwelcome-file>
		<welcome-file>index.htmlwelcome-file>
		<welcome-file>default.htmlwelcome-file>
		<welcome-file>default.htmwelcome-file>
		<welcome-file>default.jspwelcome-file>
	welcome-file-list>
web-app>

============================ 上述ssm框架已经搭建完毕 =========================

  • 下一篇在此基础上,集成shiro (java安全框架),
    Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。

搭建Maven、SSM、shiro框架详细步骤

你可能感兴趣的:(Maven,SSM框架)