SSM框架(Spring+SpringMVC+MyBatis)是目前Java WEB开发使用较多的框架,搭建起来起来比较麻烦,之前也曾搭建成功过。这次通过结合SSM开发的相关学习视频,再一次系统性的将整个SSM框架的搭建过程记录下来,以此来方便日后的开发。本文的前提条件,是Maven已经成功安装完毕!
搭建环境:
IDEA 2017.1 Maven 3.3.9 Jdk 1.7 Tomcat 7.0 Mysql 5.7
1、建立Maven Project
(1)新建一个Maven项目
(2)输入GroupId和ArtifactId,可随意输入,GroupId可认为是项目的包名,ArtifactId为项目的名称
(3) 找到Maven安装的Home,关于Maven使用的镜像仓库是在Setting.xml中可修改的,这里注意下选择的Setting.xml的存放位置
(4)完成
(5)查看IDEA Console中Maven项目是否建立成功
[INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: maven-archetype-webapp:RELEASE [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.test [INFO] Parameter: artifactId, Value: test [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: com.test [INFO] Parameter: packageInPathFormat, Value: com/test [INFO] Parameter: package, Value: com.test [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: groupId, Value: com.test [INFO] Parameter: artifactId, Value: test [INFO] Project created from Archetype in dir: C:\Users\Administrator\AppData\Local\Temp\archetypetmp\test [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 13.736 s [INFO] Finished at: 2018-12-13T11:13:40+08:00 [INFO] Final Memory: 14M/150M [INFO] ------------------------------------------------------------------------ [INFO] Maven execution finished
(6)建立好的Maven项目树状结构图
2、整合SSM框架
(1)在Pom.xml中添加Spring、SpringMVC、MyBatis相关jar
<!--属性中,配置使用的Spring和mybatis版本号--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <!--配置spring和mybatis的版本号--> <spring.version>3.2.0.RELEASE</spring.version> <mybatis.version>3.2.7</mybatis.version> </properties>
<!--第一部分:Spring配置--> <!--Spring Core--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!--Spring整合Mybatis的jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--Spring Mvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency>
<!--第二部分 MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis-spring整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency>
<!--第三部分:Servlet Web--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.4</version> </dependency>
<!-- 第四部分 数据库的连接 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.3</version> </dependency>
<!--第五部分:其他如日志、单元测试等--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.1</version> </dependency> <!--单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>
(2)在main/,新建java、resources文件夹,分别设为Sources Root和Resources Root,java主要是用来存放业务逻辑代码,resources主要是存放相关配置文件。
(3)在resources/,新建db.properties和log4j.properties,主要用于存放数据库的连接配置信息和日志配置信息。
db.properties:
jdbc.driver=com.mysql.jdbc.Driver //使用的是mysql jdbc驱动 jdbc.url=jdbc:mysql://localhost:3306/database //url、port、database jdbc.username=root //账号 jdbc.password=123456 //密码
log4j.properties:
#Global logging configuration\uff0c\u5efa\u8bae\u5f00\u53d1\u73af\u5883\u4e2d\u8981\u7528debug log4j.rootLogger=DEBUG, stdout # 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
(4) 在mybatis/,创建mybatis整合需要的配置文件sqlMapConfig.xml
<!-- 配置别名 --> <typeAliases> <!-- 批量扫描别名 --> <package name="cn.ssm.test.po"/> </typeAliases>
注:由于使用spring和mybatis的整合包进行mapper扫描,这里不需要进行mapper配置了。但必须遵循:mapper.xml和mapper.java文件同名且在一个目录。
(5) 在spring/,创建spring整合需要的配置文件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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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/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"> <!--第一部分:DAO层配置(Spring整合Mybatis)--> <!-- 加载properties文件中的内容,properties中的key要有一定的特殊命名规则--> <context:property-placeholder location="classpath:db.properties"/> <!--配置数据源,dbcp--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxActive" value="30"/> <property name="maxIdle" value="5"/> </bean> <!--Spring整合Mybatis配置--> <!--SqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--数据库连接池--> <property name="dataSource" ref="dataSource"/> <!--读取mybatis配置文件--> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/> </bean> <!--Mapper扫描器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--如需扫描多个包,中间用逗号隔开--> <property name="basePackage" value="cn.ssm.test.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!--第二部分:Service层配置--> <bean id="itemsService" class="cn.ssm.test.service.impl.ItemsServiceImpl"/> <!--第三部分:事务管理器--> <!-- 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource. DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- aop --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.ssm.test.service.impl.*.*(..))"/> </aop:config> </beans>
(6) 在spring/,创建springmvc.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:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--可以扫描controller、service等包--> <context:component-scan base-package="cn.ssm.test.controller"/> <!--使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置 mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了, 如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和 RequestMappingHandlerAdapter,实际开发时使用mvc:annotation-driven--> <mvc:annotation-driven conversion-service="conversionService"/> <!--视图解析器--> <bean class="org.springframework.web.servlet.view. InternalResourceViewResolver"> <!--配置前缀--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--配置后缀--> <property name="suffix" value=".jsp"/> </bean> <!--自定义参数--> <bean id="conversionService" class="org.springframework.format.support. FormattingConversionServiceFactoryBean"> <!--转换器--> <property name="converters"> <util:list list-class="java.util.ArrayList"> <bean class="cn.ssm.test.controller.converter. CustomDateConverter"/> </util:list> </property> </bean> </beans>
(7) 创建逆向工程,自动生成po及mapper文件
Po文件主要生成的是数据库表的字段定义,相关字段的Get和Set方法。
mapper文件主要生成的是数据库表中一般字段的查询接口及相关xml文件等。
po对象:
public class Items { private Integer id; private String name; private Float price; private String pic; private Date createtime; private String detail; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic == null ? null : pic.trim(); } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail == null ? null : detail.trim(); } }
Mapper:
mapper接口类:
public interface ItemsMapper { int countByExample(ItemsExample example); int deleteByExample(ItemsExample example); int deleteByPrimaryKey(Integer id); int insert(Items record); int insertSelective(Items record); List<Items> selectByExampleWithBLOBs(ItemsExample example); List<Items> selectByExample(ItemsExample example); Items selectByPrimaryKey(Integer id); int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example); int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example); int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example); int updateByPrimaryKeySelective(Items record); int updateByPrimaryKeyWithBLOBs(Items record); int updateByPrimaryKey(Items record); }
Mapper xml文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="cn.ssm.test.mapper.ItemsMapper" > <resultMap id="BaseResultMap" type="cn.ssm.test.po.Items" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="price" property="price" jdbcType="REAL" /> <result column="pic" property="pic" jdbcType="VARCHAR" /> <result column="createtime" property="createtime" jdbcType="TIMESTAMP" /> </resultMap> <resultMap id="ResultMapWithBLOBs" type="cn.ssm.test.po.Items" extends="BaseResultMap" > <result column="detail" property="detail" jdbcType="LONGVARCHAR" /> </resultMap> <sql id="Example_Where_Clause" > <where > <foreach collection="oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator=","> #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Update_By_Example_Where_Clause" > <where > <foreach collection="example.oredCriteria" item="criteria" separator="or"> <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator=","> #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Base_Column_List" > id, name, price, pic, createtime </sql> <sql id="Blob_Column_List" > detail </sql> <select id="selectByExampleWithBLOBs" resultMap="ResultMapWithBLOBs" parameterType="cn.ssm.test.po.ItemsExample" > select <if test="distinct" > distinct </if> <include refid="Base_Column_List" /> , <include refid="Blob_Column_List" /> from items <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null" > order by ${orderByClause} </if> </select> <select id="selectByExample" resultMap="BaseResultMap" parameterType="cn.ssm.test.po.ItemsExample" > select <if test="distinct" > distinct </if> <include refid="Base_Column_List" /> from items <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null" > order by ${orderByClause} </if> </select> <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> , <include refid="Blob_Column_List" /> from items where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from items where id = #{id,jdbcType=INTEGER} </delete> <delete id="deleteByExample" parameterType="cn.ssm.test.po.ItemsExample" > delete from items <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </delete> <insert id="insert" parameterType="cn.ssm.test.po.Items" > insert into items (id, name, price, pic, createtime, detail ) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=REAL}, #{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, #{detail,jdbcType=LONGVARCHAR} ) </insert> <insert id="insertSelective" parameterType="cn.ssm.test.po.Items" > insert into items <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="name != null" > name, </if> <if test="price != null" > price, </if> <if test="pic != null" > pic, </if> <if test="createtime != null" > createtime, </if> <if test="detail != null" > detail, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="name != null" > #{name,jdbcType=VARCHAR}, </if> <if test="price != null" > #{price,jdbcType=REAL}, </if> <if test="pic != null" > #{pic,jdbcType=VARCHAR}, </if> <if test="createtime != null" > #{createtime,jdbcType=TIMESTAMP}, </if> <if test="detail != null" > #{detail,jdbcType=LONGVARCHAR}, </if> </trim> </insert> <select id="countByExample" parameterType="cn.ssm.test.po.ItemsExample" resultType="java.lang.Integer" > select count(*) from items <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </select> <update id="updateByExampleSelective" parameterType="map" > update items <set > <if test="record.id != null" > id = #{record.id,jdbcType=INTEGER}, </if> <if test="record.name != null" > name = #{record.name,jdbcType=VARCHAR}, </if> <if test="record.price != null" > price = #{record.price,jdbcType=REAL}, </if> <if test="record.pic != null" > pic = #{record.pic,jdbcType=VARCHAR}, </if> <if test="record.createtime != null" > createtime = #{record.createtime,jdbcType=TIMESTAMP}, </if> <if test="record.detail != null" > detail = #{record.detail,jdbcType=LONGVARCHAR}, </if> </set> <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExampleWithBLOBs" parameterType="map" > update items set id = #{record.id,jdbcType=INTEGER}, name = #{record.name,jdbcType=VARCHAR}, price = #{record.price,jdbcType=REAL}, pic = #{record.pic,jdbcType=VARCHAR}, createtime = #{record.createtime,jdbcType=TIMESTAMP}, detail = #{record.detail,jdbcType=LONGVARCHAR} <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExample" parameterType="map" > update items set id = #{record.id,jdbcType=INTEGER}, name = #{record.name,jdbcType=VARCHAR}, price = #{record.price,jdbcType=REAL}, pic = #{record.pic,jdbcType=VARCHAR}, createtime = #{record.createtime,jdbcType=TIMESTAMP} <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByPrimaryKeySelective" parameterType="cn.ssm.test.po.Items" > update items <set > <if test="name != null" > name = #{name,jdbcType=VARCHAR}, </if> <if test="price != null" > price = #{price,jdbcType=REAL}, </if> <if test="pic != null" > pic = #{pic,jdbcType=VARCHAR}, </if> <if test="createtime != null" > createtime = #{createtime,jdbcType=TIMESTAMP}, </if> <if test="detail != null" > detail = #{detail,jdbcType=LONGVARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKeyWithBLOBs" parameterType="cn.ssm.test.po.Items" > update items set name = #{name,jdbcType=VARCHAR}, price = #{price,jdbcType=REAL}, pic = #{pic,jdbcType=VARCHAR}, createtime = #{createtime,jdbcType=TIMESTAMP}, detail = #{detail,jdbcType=LONGVARCHAR} where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="cn.ssm.test.po.Items" > update items set name = #{name,jdbcType=VARCHAR}, price = #{price,jdbcType=REAL}, pic = #{pic,jdbcType=VARCHAR}, createtime = #{createtime,jdbcType=TIMESTAMP} where id = #{id,jdbcType=INTEGER} </update> </mapper>
(8) 在service文件夹内,通过使用service整合mapper接口,实现相关查询功能
@Autowired private ItemsMapper itemsMapper; @Override public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { //通过ItemsMapperCustom查询数据库 return itemsMapperCustom.findItemsList(itemsQueryVo); }
(9) 创建controller,使用注解功能
@Controller //为了对url进行分类管理 ,可以在这里定义根路径,最终访问url是根路径+子路径 //比如:商品列表:/items/queryItems.action @RequestMapping("/items") public class ItemsController { @Autowired private ItemsService itemsService; // 商品查询 @RequestMapping("/queryItems") public ModelAndView queryItems(HttpServletRequest request) throws Exception { //测试forward后request是否可以共享 System.out.println(request.getParameter("id")); // 调用service查找 数据库,查询商品列表 List<ItemsCustom> itemsList = itemsService.findItemsList(null); // 返回ModelAndView ModelAndView modelAndView = new ModelAndView(); // 相当 于request的setAttribut,在jsp页面中通过itemsList取数据 modelAndView.addObject("itemsList", itemsList); // 指定视图 // 下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为 // modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp"); // 上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀 modelAndView.setViewName("items/itemsList"); return modelAndView; } }
(10) 在WEB-INF下添加jsp文件,在jsp文件夹内添加html文件
(11) 修改web.xml的配置,修改访问路径、配置外路径等。
<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" id="WebApp_ID" version="2.5"> <display-name>test</display-name> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- springmvc前端控制器 --> <servlet> <servlet-name>spring_mybatis</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation配置springmvc加载的配置文件 (配置处理器映射器、适配器等等) 如果不配置contextConfigLocation, 默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring_mybatis</servlet-name> <!-- 第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析 第二种:/,所以访问的地址都由DispatcherServlet进行解析, 对于静态文件的解析需要配置不让DispatcherServlet进行解析, 使用此种方式可以实现 RESTful风格的url 第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时, 仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错。 --> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- post乱码过虑器 --> <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>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</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>
3、实现Tomcat热部署
(1)找到Edit configurations,进入到如下界面
(2)点击fix或者点击Deployment,进入如下界面设置相关参数
(3)更改下图中的两项参数
(4)Build项目
(5)build后的结果展示
4、注意事项
(1)需要在pom.xml中的build下添加如下代码,解决编译后的mapper文件内无法编译XXXmapper.xml内容
<!--编译main/java/*下的.xml文件-->
<resources> <resource> <directory>src/main/java</directory><!---设置编译的包--> <includes> <include>**/*.xml</include><!---该路径下的.xml类型文件也要编译--> <include>**/*.properties</include><!---该路径下的.properties 类型文件也要编译--> </includes> <filtering>false</filtering> </resource> </resources>
(2)热部署的时候,注意不要改变Application context的默认配置,需要修改首页显示的页面,路径可在Server中配置
共同学习,写下你的评论
评论加载中...
作者其他优质文章