39--SSM整合(方案一)

SSM整合--2020-06-25

创建一个maven的web工程

导入pom.xml文件(junit,servlet,jsp,jstl,log4j,spring核心依赖,mybatis依赖,mysql驱动等...)

<dependencies>
    <!-- 单元测试 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <!-- 整合log4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.4</version>
    </dependency>
    <!-- Servlet/JSP/JSTL -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- 整合spring框架(包含springmvc) 这个jar文件包含springmvc开发时的核心类, 同时也会将依赖的相关jar文件引入进来(spring的核心jar文件也包含在内) -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <!--这个jar文件包含对Spring对JDBC数据访问进行封装的所有类 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>

    <!-- 整合mybatis框架 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.8</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.2.2</version>
    </dependency>
    <!-- mysql驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.32</version>
    </dependency>
    <!-- druid连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.6</version>
    </dependency>
</dependencies>

使用idea配置maven资源过滤(pom.xml)

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

创建源码目录以及web资源目录

 java源码目录
    com.gavin.controller 控制层
    	xxxController.java
    com.gavin.pojo 实体Bean
    	Xxx.java
    com.gavin.service 业务层
    	(I)XxxService.java
    	   XxxServiceImpl.java
    com.gavin.dao dao层
    	(I)XxxMapper.java
 java资源目录
 	spring(目录)
 		applicationContext.xml
 		spring-mvc.xml
 		spring-service.xml
 		spring-dao.xml
 	mybatis(目录)
 		mappers(目录)
 			XxxMapper.xml
 			XxxMapper.xml
 		mybatis-config.xml
 	log4j.properties
 	jdbc.properties
 web资源目录
 	web(目录)
 		WEB-INF(目录)
 			css(目录)
 			jsp(目录)
 				xxx.jsp
 		web.xml
 		index.jsp

新建mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

</configuration>

新建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">

</beans>

整合mybatis层

新建jdbc.properties数据库配置文件

jdbc.driver=com.mysql.jdbc.Driver
# useSSL属性为是否使用ssl安全链接,可能导致无法访问数据库
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=1234

更改mybatis核心配置文件 mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    <!--别名,为了使用model类时方便,不需要写全名-->
    <typeAliases>
        <package name="com.gavin.pojo"/>
    </typeAliases>
    
    <!--绑定mapper映射文件-->
    <!--此处的映射文件由spring整合
	<mappers>
        <mapper resource="mappers/userMapper.xml"/>
    </mappers> 
	-->

</configuration>

实体类编写

dao层编写,mapper接口

编写对应的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>
	<select id="selectAllUser" resultType="User">
        SELECT * from user
    </select>
</mapper>

编写service层以及实现接口(service层调用dao层)

Spring层整合其他层

编写spring-dao.xml配置文件,用来整合mybatis

<?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
                           https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置整合mybatis -->
    <!-- 1.关联数据库文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 2.数据库连接池 -->
    <!--数据库连接池
dbcp  半自动化操作  不能自动连接
c3p0  自动化操作(自动的加载配置文件 并且设置到对象里面)
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置连接池属性 -->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <!-- 3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 配置mapper文件-->
        <!-- 扫描所有的 XxxMapper.xml映射文件,读取其中配置的SQL语句 -->
		<property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"/>
    </bean>

    <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
    <!--解释 : https://www.cnblogs.com/jpfss/p/7799806.html-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.gavin.dao"/>
    </bean>

</beans>

编写spring-service.xml配置文件,Spring整合service层

<?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.xsd">

    <!-- 扫描service相关的bean -->
    <context:component-scan base-package="com.gavin.service" />

    <!--UserServiceImpl注入到IOC容器中, 也可以通过注解自动装配-->
    <bean id="UserServiceImpl" class="com.gavin.service.UserServiceImpl">
		<property name="userMapper" ref="userMapper"/>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
    </bean>
</beans>

SpringMVC层

配置web.xml

<!--DispatcherServlet-->
<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!--encodingFilter-->
<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>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!--Session过期时间-->
<session-config>
    <session-timeout>15</session-timeout>
</session-config>

编写spring-mvc.xml配置文件, Spring整合MVC层

<?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:mvc="http://www.springframework.org/schema/mvc"
       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
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解驱动 -->
    <mvc:annotation-driven />
    <!-- 2.静态资源默认servlet配置-->
    <mvc:default-servlet-handler/>

    <!-- 3.配置jsp 显示ViewResolver视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 4.扫描web相关的bean -->
    <context:component-scan base-package="com.gavin.controller" />

</beans>

配置整合文件applicaitonContext.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="spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>
    
</beans>

至此ssm整合完毕

开发中需要注意的地方:

关于通用页面跳转的方法

/**
	 * 通用页面跳转方法
	 * @return
	 */
@RequestMapping("/{pages}")
public String toPage(@PathVariable String pages) {
    return pages;
}

自动装配注解(例如:)

/*获取DoorMapper接口的子类实例
	 * @Autowired 到spring容器中根据类型去寻找该接口对应的子类的实例
	 * 由于在spring的配置文件中,配置过扫描com.gavin.dao包下的所有接口
	 * spring会为DoorMapper接口创建实例
	 * 通过自动装配,将DoorMapper接口的子类实例赋值给doorMapper
	 */

@Autowired
private DoorMapper doorMpper;

全站500/404提示

当通过浏览器访问服务器中不存在的资源时,浏览器的页面上会显示404;

当服务器端程序出现错误时,浏览器的页面上会显示500异常信息;

可以自己写更友好的404和500页面,代替默认的404和500页面。

在web.xml文件中配置全站404和500跳转页面

<web-app>
	...
	<!-- 配置全站500异常跳转页面 -->
	<error-page>
		<error-code>500</error-code>
		<location>/500.jsp</location>
	</error-page>
	
	<!-- 配置全站404跳转页面 -->
	<error-page>
		<error-code>404</error-code>
		<location>/404.jsp</location>
	</error-page>
	...
	
</web-app>

修改数据时要回填数据,以及下拉框如何操作数据

<select name="doorId">
    <!-- 遍历所有门店, 显示所有门店列表 -->
    <c:forEach items="${dList}" var="door">
        <option value="${door.id}">${door.name}</option>
    </c:forEach>
</select>

关于提交时间,以及时间在页面显示的格式化

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<input type="text" name="payTime" 
       value='<fmt:formatDate 
              value="${ order.payTime }" 
              pattern="yyyy/MM/dd HH:mm:ss"/>'/>

关于数据库的字段名和实体类属性名不相同的处理办法

1 在查询的时候使用别名,与实体类相对应
2 通过map映射(在xxxMapper.xml文件中填写)
<!-- 
  数据表列字段与pojo类属性映射关系
  type:指定将查询的结果封装到哪个类pojo对象中
  id:指定一个唯一表示resultMap的值
  -->
<resultMap type="com.tedu.pojo.Order" id="orderRM">
    <id column="id" property="id"/>
    <result column="door_id" property="doorId"/>
    <result column="order_no" property="orderNo"/>
    <result column="order_type" property="orderType"/>
    <result column="pnum" property="pnum"/>
    <result column="cashier" property="cashier"/>
    <result column="order_time" property="orderTime"/>
    <result column="pay_time" property="payTime"/>
    <result column="pay_type" property="payType"/>
    <result column="price" property="price"/>
</resultMap>
<!-- 1.查询所有订单信息	 id值为对应接口中方法的名字
  resultMap:指定为resultMap标签的id值
  -->
<select id="findAll" resultMap="orderRM">
    select * from tb_order
</select>

posted on 2020-06-25 22:24  liqiangbk  阅读(254)  评论(0编辑  收藏  举报

导航