SSM项目搭建的主要步骤

SSM项目搭建的主要步骤

  • 一、说明
  • 二、配置springMVC
    • 1、前端控制器(DispatcherServlet)
    • 2、拦截器(HandlerInterceptor)
    • 3、视图解析器(ViewResolver,全称为InternalResourceViewResolver(内部资源的视图解析器))
    • 4、后端控制器(Controller)
    • 5、url映射处理器(HanlderMapping)
    • 扩展配置
    • 6、全站乱码处理过滤器(CharacterEncodingFilter)
    • 7、开启springMVC注解
    • 8、放行静态资源文件
  • 三、配置Mybatis
    • 1、数据源(使用druid连接池,DruidDataSource)
    • 2、会话工厂(SqlSessionFactoryBean)
    • 3、Mapper扫描(MapperScannerConfigurer)
    • 4、Mybatis自身的配置(主要用来配置mybatis自身的某些功能,最后使用会话工厂来加载)
  • 四、配置Spring
    • 1、事务控制
    • 2、导入properties配置文件的三种方式
      • 2.1 使用util:properties标签引入
      • 2.2 使用context:property-placeholder标签引入
      • 2.3 使用PropertyPlaceholderConfigurer类引入
  • 五、整体搭建
    • 1、核心配置文件中引入其他的配置文件
    • 2、包路径的创建
    • 3、配置Tomcat服务器插件,并配置tomcat启动。
  • 六、附录
    • 1、spring-mybatis.xml、spring-mvc.xml、spring.xml和spring-configs.xml的引用配置
    • 2、mybatis-config.xml的dtd配置
    • 3、mapper文件的dtd配置

一、说明

因本人个人习惯,一般将项目的核心的配置分为如下几个配置文件配置(名字也是本人的个人爱好,不喜别喷),下文说到配置文件时就不会再进行解释。
spring-mybatis.xml:springMybatis配置文件,主要用来配置与mybatis有关的配置,如数据源等。
spring-mvc.xml:springMVC配置文件,主要用来配置与springMVC有关的配置,如视图解析器等。
spring.xml:spring配置文件,主要用来配置其他的配置,如包扫描,事务管理等。
spring-configs.xml:核心配置文件,主要用来引入其他的配置文件和配置一些共用的配置,如引入springMybatis配置文件和配置包扫描等。

二、配置springMVC

springMVC的五大组件,包括“前端控制器”、“后端控制器”、“视图解析器”、“拦截器”和“url映射处理器”。

1、前端控制器(DispatcherServlet)

1.1 在web.xml文件中配置
1.2 给“contextConfigLocation”变量指定核心配置文件的路径(如:classpath:/spring-configs.xml)。在前端控制器启动的时候会使用其来加载核心配置文件。
1.3 设置优先级,一般设置为1(数字越小优先级越高)。如果没有设置,服务器启动时不会启动前端控制器。
1.4 具体代码配置如下:
	
	<servlet>
		<servlet-name>dispatcherServletservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		<init-param>
			
			<param-name>ContextConfigLocationparam-name>
			<param-value>classpath:/spring-configs.xmlparam-value>
		init-param>
		<load-on-startup>1load-on-startup> 
	servlet>
	
	
	<servlet-mapping>
		<servlet-name>dispatcherServletservlet-name>
		<url-pattern>/url-pattern>
	servlet-mapping>

2、拦截器(HandlerInterceptor)

本人没有配置

3、视图解析器(ViewResolver,全称为InternalResourceViewResolver(内部资源的视图解析器))

3.1 在springMVC配置文件中配置
3.2 给“Prefix”变量指定前缀,在解析视图时和后缀一起与传入的字符串拼接成完整的url路径
3.3 给“Suffix”变量指定后缀,同上
3.4 具体代码配置如下:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="Prefix" value="/WEB-INF/views/"/>
    	<property name="Suffix" value=".jsp"/>
bean>

4、后端控制器(Controller)

4.1 可以配置也可以不配置。
4.2 如果想配置就使用bean标签在springMVC配置文件中配置(一般不建议使用)
4.3 如果不想配置就使用注解的方式。在类前面添加后端控制器注解(@Controller),然后在spring配置文件中配置扫描就行了。(建议使用)
4.4 具体代码实现:
@Controller //后端控制器注解
@RequestMapping("/") //url映射路径注解
public class PageController {
	@RequestMapping("doTestPage")
	public String doTestPage(){
		return "test_page";
	}
}

5、url映射处理器(HanlderMapping)

5.1 不需要配置URL映射处理器,但需要使用注解方式配置url映射路径。在后端控制器类或方法的前面加上url映射路径的注解(@ RequestMapping)。(建议使用,本人表示只会注解方式,其他方式不会)
5.2 具体代码实现:
@Controller //后端控制器注解
@RequestMapping("/") //url映射路径注解
public class PageController {
	@RequestMapping("doTestPage")
	public String doTestPage(){
		return "test_page";
	}
}

扩展配置

6、全站乱码处理过滤器(CharacterEncodingFilter)

6.1 在web.xml文件中配置
6.2 给“encoding”变量指定编码格式(一般为UTF-8)
6.3 具体代码配置如下:
	
	<filter>
		<filter-name>characterEncodingFilterfilter-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>characterEncodingFilterfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>

注:该过滤器只针对POST请求,其他请求无效。请求参数中包含中文的,建议使用POST请求

7、开启springMVC注解

7.1 在springMVC配置文件中配置
7.2 具体代码配置如下:

<mvc:annotation-driven/>
7.3 扩展
	7.3.1 配置阿里巴巴的fashjson对象,主要用于高并发时的数据传输格式转化
	7.3.2 具体代码配置如下:
	 
	<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
				<property name="SupportedMediaTypes">
					<list>
						<value>text/html;charset=utf-8value>
						<value>application/json;charset=utf-8value>
					list>
				property>
			bean>
		mvc:message-converters>
	mvc:annotation-driven>

注:如果不开启springMVC注解,会出现一些意想不到的问题。例如:@GetMapping注解无效,@DateTimeFormat注解无效等

8、放行静态资源文件

8.1 在springMVC配置文件中配置
8.2 具体代码配置如下:

<mvc:default-servlet-handler/>

注:放行后,过滤器和前端控制器将不会再进行拦截

三、配置Mybatis

1、数据源(使用druid连接池,DruidDataSource)

1.1 在springMybatis配置文件配置
1.2 给“DriverClassName”变量指定驱动
1.3 给“Url”变量指定数据库的url
1.4 给“Username”变量指定数据库的用户名
1.5 给“Password”变量指定数据库的密码
1.6 具体代码配置如下:
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    	
    	<property name="DriverClassName" value="#{jdbc.jdbcDriver}"/>
    	
    	<property name="Url" value="#{jdbc.jdbcUrl}"/>
    	
    	<property name="Username" value="#{jdbc.jdbcUsername}"/>
    	
    	<property name="Password" value="#{jdbc.jdbcPassword}"/>
    bean>

2、会话工厂(SqlSessionFactoryBean)

2.1 在springMybatis配置文件配置
2.2 给“DataSource”变量指定一个数据源(这里为druid连接池)
2.3 给“ConfigLocation”变量指定mybatis自身的配置文件(mybatis-config.xml),用于加载mybatis自身的配置
2.4 给“MapperLocations”变量指定mapper文件的路径,为后面的生成动态代理对象做准备
2.5 给“TypeAliasesPackage”变量指定扫描路径,相当于给指定包下的pojo对象起别名。typeAliasesPackage 默认只能扫描某一个路径下,或以逗号等分割的 几个路径下的内容,不支持通配符和正则
2.6 具体代码配置如下:
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<property name="DataSource" ref="dataSource"/>
		
		<property name="ConfigLocation" value="classpath:/mybatis/mybatis-config.xml"/>
		
		<property name="MapperLocations" value="classpath*:/mybatis/mapper/*Mapper.xml"/>
		
		<property name="TypeAliasesPackage" value="com.jt.manage.pojo"/>
	bean>

注:会话工厂(SqlSessionFactory)是通过SqlSessionFactoryBean创建的,所有配置的是SqlSessionFactoryBean,而不是SqlSessionFactory

3、Mapper扫描(MapperScannerConfigurer)

3.1 在springMybatis配置文件配置
3.2 给“SqlSessionFactoryBeanName”变量指定一个会话工厂类,也可以不写,spring默认注入
3.3 给“”变量指定dao接口的包路径,主要用于扫描并生成对应的动态代理对象
3.4 具体代码配置如下:

	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		
		<property name="SqlSessionFactoryBeanName" value="sqlSessionFactory"/>
		
		<property name="BasePackage" value="com.jt.**.dao"/>
	bean>

注:MapperScannerConfigurer类会扫描指定的dao包下的接口,并关联对应的mapper动态生成代理对象,代理对象的“BeanId”默认为接口名(首字母小写)

4、Mybatis自身的配置(主要用来配置mybatis自身的某些功能,最后使用会话工厂来加载)

4.1 配置时需要注意标签的配置顺序,如果顺序颠倒会出现问题。可能配置无效或报错。将鼠标放在configuration标签上面会提示其他标签的配置顺序,具体如下:
	配置顺序	(properties?, settings?,typeAliases?,typeHandlers?,objectFactory?, objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers?)

4.2 开启驼峰自动映射
	具体代码配置如下:
	<settings>
		
		<setting name="mapUnderscoreToCamelCase" value="true" />
	settings>

四、配置Spring

1、事务控制

1.1 配置事务控制
具体配置如下:

<tx:annotation-driven/>
1.2 定义事务管理器
具体配置如下:
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="DataSource" ref="dataSource">property>
	bean>
1.3 定义事务策略
具体配置如下:
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*"   propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="find*"   propagation="SUPPORTS" read-only="true"/>
			<tx:method name="*" 	  propagation="SUPPORTS" read-only="true"/>
		tx:attributes>
	tx:advice>
1.4	定义事务切面
具体配置如下:
	
	<aop:config>
		
		<aop:pointcut expression="execution(* com.jt.manage.service..*.*(..))" id="pc"/>
		
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
	aop:config>

2、导入properties配置文件的三种方式

2.1 使用util:properties标签引入

2.1.1 这种引入只能同时引入一个或一批命名有规律并在同一个目录下的properties配置文件
2.1.2 具体代码配置如下:
<context:property-placeholder location="classpath:/property/*.properties"/>

2.2 使用context:property-placeholder标签引入

2.2.1 这种引入只能同时引入一个或一批命名有规律并在同一个目录下的properties配置文件
2.2.2 具体代码配置如下:
<context:property-placeholder location="classpath:/property/*.properties"/>

2.3 使用PropertyPlaceholderConfigurer类引入

2.3.1 这种引入可以同时引入一个或一批properties配置文件,其中配置文件不需命名有规律,路径也可以有不同。
2.3.2 具体代码配置如下:
<bean id="jdbc" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="Locations">
		<list>
			<value>classpath:/property/jdbc.propertiesvalue>
		list>
	property>
bean>

五、整体搭建

1、核心配置文件中引入其他的配置文件

1.1 核心配置文件中引入其他的配置文件,在服务器启动后由前端控制器一起加载
1.2 具体代码配置如下:

<import resource="classpath:/spring/spring-mybatis.xml"/>
<import resource="classpath:/spring/spring-mvc.xml"/>
<import resource="classpath:/spring/spring.xml"/>

2、包路径的创建

2.1 包括service、dao、controller等包。

3、配置Tomcat服务器插件,并配置tomcat启动。

3.1 在pom配置文件中添加Tomcat服务器插件配置
3.1.1 具体代码配置如下:
	<build>
		<plugins>
			
			<plugin>
				<groupId>org.apache.tomcat.mavengroupId>
				<artifactId>tomcat7-maven-pluginartifactId>
				<version>2.2version>
				<configuration>
					<port>80port> 
					<path>/path> 
				configuration>
			plugin>
		plugins>
	build>
3.2 配置Tomcat启动
3.2.1 具体操作如下:

SSM项目搭建的主要步骤_第1张图片
SSM项目搭建的主要步骤_第2张图片
SSM项目搭建的主要步骤_第3张图片

六、附录

1、spring-mybatis.xml、spring-mvc.xml、spring.xml和spring-configs.xml的引用配置

1.1 spring-mybatis.xml、spring-mvc.xml、spring.xml和spring-configs.xml这些配置文件需要配置相应的引用(如:xmlns="http://www.springframework.org/schema/beans")
1.2 为了方便,上面的四个配置文件都使用相同的引用配置,这些引用包含了这些配置文件所有可能使用到的引用。
1.3 事实上对于某个配置文件来说,某些配置其实是不需要的。如在spring.xml文件中,mvc的引入没有使用到,可以去掉。
1.4 下面是spring-configs.xml的配置:


<beans default-lazy-init="true" 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    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" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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/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-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
 	
 	
 	
	<context:component-scan base-package="com.jt" />
	
	<import resource="classpath:spring-mvc.xml"/>
	<import resource="classpath:spring-mybatis.xml"/>
	
 beans>

2、mybatis-config.xml的dtd配置

2.1 mybatis-config.xml需要引入mybatis-3-config.dtd这个约束文件
2.2 具体配置如下:


<configuration>
	
	<settings>
		
		<setting name="mapUnderscoreToCamelCase" value="true" />
		
		<setting name="cacheEnabled" value="false" />
	settings>
configuration>

3、mapper文件的dtd配置

3.1 mapper文件需要引入mybatis-3-mapper.dtd这个约束文件
3.2 具体配置如下:


<mapper namespace="com.jt.manage.mapper.UserMapper">
	<select id="findUserList" resultType="User">
		select * from user
	select>
mapper>

你可能感兴趣的:(Java)