IDEA下的SSM框架整合

根据上课的内容进行整合发现jdk1.8和spring3的不兼容,索性换成idea+maven
环境搭建

在这里插入图片描述

通过上面的连接搭好的环境具备了所有的jar包,下一步把目录建好,然后进行整合
下面的内容建立在jar包齐全的基础上,并且设置java目录为source root,resources为resources文件

1、整合Dao

1.1、 创建SqlMapConfig.xml配置文件

在这里插入图片描述
这一步是编写mybatis配置文件的全局配置,包含了全局的设置

<?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>
  
   <settings>
       <setting name="lazyLoadingEnabled" value="true"/>
       <setting name="aggressiveLazyLoading" value="false"/>
   </settings>
   <typeAliases>
       <!--定义单个-->
       <!--<typeAlias type="cn.itcast.mybatis.po.User" alias="user"/>-->
       <!--批量定义别名,效果为:别名就是类名-->
       <package name="cn.iot.ssm.po"/>
   </typeAliases>

   <!--加载映射文件,交给spring管理,这里不需要再配置-->
   <mappers>
       <!--<package name="cn.iot.ssm.mapper"/>-->
       <!--         <mapper resource="sqlmap\User.xml"/> -->
       <!--<package name="cn.itcast.mybatis.sqlMapper"/>-->
       <!--不需要配置了,交给spring去管理-->
       <!--<mapper class="cn.itcast.mybatis.sqlMapper.UserMapper"/>-->
       <!--         <mapper class="cn.itcast.mybatis.sqlMapper.OrdersMapperCustomMapper"/> -->
   </mappers>
</configuration>

1.2 通过spring容器管理dao

在这里插入图片描述

这一步是为了管理单例模式的连接池和sqlSessionFactory,开启了mybatis和mysql的通路,并且开启mapper扫描,把mapper对象注入容器中,再通过注入方式得到mapper对象,利用mapper对象操作数据库。

<?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">
    <!--加载数据库的配置文件-->
    <context:property-placeholder location="classpath:config/db.properties"/>
    <!-- 配置c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 注入属性值 -->
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    	 
    </bean>
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--加载mybatis的配置文件-->
        <property name="configLocation" value="classpath:config/mybatis/SqlMapConfig.xml"/>
        <!--数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--mapper的批量扫描,从mapper包中扫描出接口,自动创建代理对象,也就是实现上面的那种方法,-->
    <!--并且在spring容器中注册
    -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定扫描的包名-->
        <!--规范仍然要保持:java文件和xml文件名称一致,且在一个目录下;方法和selectId一致-->
        <property name="basePackage" value="cn.iot.ssm.mapper"/>
        <!--指定sqlSessionFactory,注意:它是String类型的,因此给他value = "一个字符串",
        而不是ref=""引用一中封装类型-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
</beans>

1.3 在web.xml注册spring容器(applica-dao.xml)

在这里插入图片描述
在< web-app >标签中加入,通过通配符注册dao和接下去用到的service,transaction的spring配置文件,在后面的整合就可以忽略spring容器自身的注册了。

并且添加监听器

 <display-name>Archetype Created Web Application</display-name>
  <!-- 加载spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/spring/applicationContext-*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

1.4 通过逆向工程生成mapper和pojo

具体参照mybatis逆向工程,这里不多讲
这一步是业务拓展的关键,根据具体需求编写接口和配置,需要底层到sql语句,写mapper.java接口和mapper.xml配置文件
在这里插入图片描述

Dao层整合完毕,接下去在Service中就能调用dao的方法了

2、整合service层

在整合dao的第二步中,我们已经把mapper对象添加到了spring容器。因此service直接注入即可

2.1 service和serviceimpl
public interface ItemsService {
	// 商品查询
	   List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
package cn.iot.ssm.service;

import cn.iot.ssm.mapper.ItemsMapperCustom;
import cn.iot.ssm.pojo.query.ItemsCustom;
import cn.iot.ssm.pojo.query.ItemsQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ItemsServiceImpl implements ItemsService{

	// ItemsMapperCustom所在的包已经被spring容器加载了,因此直接注入
	 @Autowired
	 private ItemsMapperCustom  itemsMapperCustom;
	public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
		return itemsMapperCustom.findItemsList(itemsQueryVo);
	}
}
2.2 在spring容器中注册service

把service对象交给spring管理
在这里插入图片描述

<?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">
     <!-- 管理商品管理的sevice -->
     <bean id="itemsService" class="cn.iot.ssm.service.ItemsServiceImpl">
     </bean>
</beans>

3、整合springmvc

3.1 在web.xml中注册前端控制器

这是总的web.xml代码

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!-- 加载spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/spring/applicationContext-*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- springmvc前端控制器 -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation配置springmvc加载的配置文件
    如果不指定的话,它默认加载/WEB-INF/springmvc-servler.xml -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:config/springmvc/springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!-- 1、*.action ,访问任何.action都交给DispatcherServlet进行解析
         2、/,任何路径都给它解析
     -->
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

3.2 配置处理器映射器、处理器适配器等等

在这里插入图片描述
这一步开启control的扫描(control不需要单独配置),配置处理器映射器、处理器适配器、视图解析器

<?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/context
	  http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
      ">
	  <!-- 实际开发中,用的是扫描包 -->
	  <!-- 扫描controller注解,多个包中间使用半角逗号分隔 -->
	  <context:component-scan base-package="cn.iot.ssm.controller"/>

   	  <!-- 处理器映射器 -->
   	  <!--注解映射器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
   	  <!--注解适配器 -->
<!-- 	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
   	  <!-- 代替注解适配器的一种方式: mvc:annotation-driven
   	  可以代替上面的映射器和适配器编写
   	  而且它自带了很多参数绑定的方法:例如json解析 -->
   	  <mvc:annotation-driven></mvc:annotation-driven>
      <!-- 视图解析器
     	 根据视图的逻辑名字,解析得到view
      	-->
	  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 	<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
	  </bean>
</beans>

3.3 编写controller(或叫handler)

在这里插入图片描述
3.2中,已经开启了controller的扫描,那么通过注释的方式告诉spring身份,就会被springmvc调用。并且通过spring的ioc注入得到service

package cn.iot.ssm.controller;

import cn.iot.ssm.pojo.query.ItemsCustom;
import cn.iot.ssm.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

//商品的controller
@Controller
public class ItemsController {
	//注入service
	@Autowired
	private ItemsService itemsService;

	//商品查询列表
	//@RequestMapping把url和action方法进行映射
	@RequestMapping("/queryItems.action")
	public ModelAndView queryItems() throws Exception{
		
		// 不传查询条件
		List<ItemsCustom> itemsList =  itemsService.findItemsList(null);
		 
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		// 相当于request的setAttribut,在jsp页面中通过key:itemsList获取数据
		modelAndView.addObject("itemsList",itemsList);

		// 面临一个问题,传入的itemsList,没有被jsp调用
		// 有可能是我强制解析xml的原因
		System.out.println(itemsList.size());
		// 指定视图,视图解析器那设置了前后缀,那么这里省略一些名字
		modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
		return modelAndView;
	}
	//商品修改
}
最后附上页面itemsList.jsp的代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body> 
<form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
	<td>商品名称</td>
	<td>商品价格</td>
	<td>生产日期</td>
	<td>商品描述</td>
	<td>操作</td>
</tr>
	<c:out value="${itemsList.size()}"></c:out>
<c:forEach items="${itemsList }" var="item">
<tr>
	<td>${item.name }</td>
	<td>${item.price }</td>

	<td>${item.detail }</td>
	<td><a href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>

</table>
</form>
</body>

</html>

4、整合完毕,接下去有两个地方需要调试

4.1 扫描不到mapper的bean

方法一:在pom.xml中加入下面的build插入这段

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

方法二:把mapper文件夹设置为resource
在这里插入图片描述

4.2 jsp失效

在前面添加上

<%@ page isELIgnored="false" %>

5、需要sql资源的朋友可以留言,对跨度大的地方有疑问的朋友也欢迎共同探讨。

猜你喜欢

转载自blog.csdn.net/qq_37591656/article/details/86566052