IDEA中SSM(Spring + SpringMVC + Mybatis)的配置整合

1.第一步:首先創建一個web工程
在這裏插入圖片描述
然後在web/WEB-INF的目錄下創建lib包,並將ssm整合的jar包導入
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
導入後不要忘了,右擊lib目錄將jar包添加到庫中的操作
在這裏插入圖片描述
2.第二步:首先進行springmvc的配置
在ssm目錄下新建config文件,在config文件下創建springmvc.xml文件
在這裏插入圖片描述
然後右擊config文件,將此文件標記爲ROOT
在這裏插入圖片描述
建好controller層的包目錄,在springmvc.xml中配置註解掃描到controller層
在這裏插入圖片描述
然後進行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:mvc="http://www.springframework.org/schema/mvc"
       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-3.2.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- 1.配置註解掃描位置 -->
    <context:component-scan base-package="com.zxh.backoffice.web.controller" />

    <!-- 2.配置註解處理映射-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

    <!--3.配置適配器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

    <!-- 4.配置springmvc視圖解析器 視圖解析器解析的視頻路徑爲:前綴 + 後綴 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

然後在web.xml文件中進行配置,加載springmvc.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 3.0的springmvc 默認加載WEB-INF下的dispatcher-servlet.xml文件 3.2的springmvc
            加載DispatcherServlet-servlet.xml文件 -->
        <init-param>
            <!-- 修改黑底springmvc加載的配置文件路徑 -->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>

在controller層下創建UserController.java文件,進行到controller層的測試

package com.zxh.backoffice.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/list")
    public String list(){
        return "user/list";
    }
}

在web目錄下,建好jsp文件
在這裏插入圖片描述
list.jsp

<%--
  Created by IntelliJ IDEA.
  User: 趙秀浩
  Date: 2020/1/6
  Time: 20:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用戶頁面</title>
</head>
<body>
用戶列表:
</body>
</html>

啓動Tomcat進行測試,如下效果即說明到controller層的配置成功。
在這裏插入圖片描述
3.第三步:Model、mapper、service層的配置
首先準備好相應的數據庫表,然後在工程中新建好Model、mapper、service這三層目錄,然後將model和mapper文件加入(model和mapper的代碼可用通過逆向工程生成的)
在這裏插入圖片描述
在這裏插入圖片描述
準備一個案列方便後續進行測試如:
ItemsMapper.java接口
在這裏插入圖片描述
ItemsMapper.xml
在這裏插入圖片描述
ItemsService.java
在這裏插入圖片描述
service實現類,注意:在service層上加上註解
在這裏插入圖片描述

然後再先創建mybatis的配置文件mybatis.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>
    <!-- 別名配置 -->
    <typeAliases>
        <!-- 批量配置別名:指定批量定義別名的類包,別名爲類名(首字母大小寫都可) -->
        <package name="com.zxh.backoffice.model"/>
    </typeAliases>
    <mappers>
        <!-- 批量加載映射文件 -->
        <package name="com.zxh.backoffice.mapper"/>
    </mappers>
</configuration>

然後進行spring的配置
在這裏插入圖片描述
數據庫的數據源文件
db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatisday01
jdbc.username=root
jdbc.password=root

spring配置文件的配置,配置c3p0數據源和mybatis的會話工廠,及添加Bean的註解裝配
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-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!--加載配置文件-->
    <context:property-placeholder location="classpath:db.properties"/>

    <!--1.配置數據源-->
    <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}"/>
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="2"/>
    </bean>

    <!--2.配置sessionFactory-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 指定配置文件位置 -->
        <property name="configLocation" value="classpath:mybatis.xml"/>
    </bean>

    <!--3.自動生成dao,mapper-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zxh.backoffice.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
    </bean>

    <!--自動掃描service-->
    <context:component-scan base-package="com.zxh.backoffice"/>

</beans>

然後在Web.xml中配置spring容器
在這裏插入圖片描述

<!--配置spring-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

再爲測試用例添加jsp頁面,編寫controller層代碼
在這裏插入圖片描述
UserController.java

package com.zxh.backoffice.web.controller;

import com.zxh.backoffice.model.Items;
import com.zxh.backoffice.service.IItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/items")
public class ItemsController {

    @Autowired
    private IItemsService iItemsService;

    @RequestMapping("/list")
    public String list(){

        List<Items> allItems = iItemsService.findAllItems();
        System.out.println(allItems);
        return "items/list";
    }
}

啓動Tomcat進行整合測試
在這裏插入圖片描述
在這裏插入圖片描述

配置事務:
在這裏插入圖片描述

<!--配置事務-->
    <!-- 4.配置事務管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 5.開啓事務註解-->
    <tx:annotation-driven></tx:annotation-driven>
   

然後在service層添加事務註解
在這裏插入圖片描述
測試事務:
在這裏插入圖片描述
UserController.java

  @RequestMapping("/save")
    public String save(){

        //創建一個商品
        Items item = new Items();
        item.setName("iphone12");
        item.setPrice(5499.00F);
        item.setCreatetime(new Date());
        item.setDetail("666");
        //保存數據
        iItemsService.saveOrUpdate(item);
        return "items/list";
    }

int i = 1/0 沒有註釋掉時,數據將不會添加進數據庫,註釋掉則可以添加到數據庫,說明事務配置成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章