SSM框架整合

SSM框架整合

  • IDEA整合SSM框架
    • 开发环境
    • idea新建maven项目
    • 配置依赖,pom.xml
    • web.xml中配置Spring、SpringMVC、字符编码过滤器、加载静态资源
    • jdbc.properties 配置数据库相关属性
    • 在 spring.xml 中配置 Spring 和 Mybatis 的整合
    • config.xml 中配置一些 MyBatis 辅助信息,比如打印 SQL 等。
    • 配置 springmvc.xml
    • 实体类Book
    • BookRepository
    • BookRepository.xml
    • BookService
    • BookServiceimpl
    • BookHandler
    • 此时SSM框架就整合完成了,我的目录结构如下:

IDEA整合SSM框架

今天自己手动搭建一下SSM框架,重温一下自己之前实习的项目。

开发环境

IEDA 2020.3
Tomcat 10.0.5
Maven 3.6.3
Mysql 8.0.23

idea新建maven项目

SSM框架整合_第1张图片
SSM框架整合_第2张图片

配置依赖,pom.xml



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>org.dashenggroupId>
    <artifactId>ssmbuildartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>

    <name>ssmbuild Maven Webappname>
    
    <url>http://www.example.comurl>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>
        
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.2.11.RELEASEversion>
        dependency>
        
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.2.12.RELEASEversion>
        dependency>
        
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>5.2.12.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>5.2.14.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.5version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.2version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.23version>
        dependency>
        <dependency>
            <groupId>c3p0groupId>
            <artifactId>c3p0artifactId>
            <version>0.9.1.2version>
        dependency>
        
        <dependency>
            <groupId>jstlgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>RELEASEversion>
            <scope>compilescope>
        dependency>

    dependencies>

    <build>
        
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>*.xmlinclude>
                    <include>*.propertiesinclude>
                includes>
            resource>
        resources>

        <finalName>ssmbuildfinalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-clean-pluginartifactId>
                    <version>3.1.0version>
                plugin>
                
                <plugin>
                    <artifactId>maven-resources-pluginartifactId>
                    <version>3.0.2version>
                plugin>
                <plugin>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.8.0version>
                plugin>
                <plugin>
                    <artifactId>maven-surefire-pluginartifactId>
                    <version>2.22.1version>
                plugin>
                <plugin>
                    <artifactId>maven-war-pluginartifactId>
                    <version>3.2.2version>
                plugin>
                <plugin>
                    <artifactId>maven-install-pluginartifactId>
                    <version>2.5.2version>
                plugin>
                <plugin>
                    <artifactId>maven-deploy-pluginartifactId>
                    <version>2.8.2version>
                plugin>
            plugins>
        pluginManagement>
    build>
project>

web.xml中配置Spring、SpringMVC、字符编码过滤器、加载静态资源



<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>
  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring.xmlparam-value>
  context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>
    init-param>
  servlet>

  <servlet-mapping>
    <servlet-name>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

  
  <filter>
    <filter-name>characterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>eccodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>characterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

  
  <servlet-mapping>
    <servlet-name>defaultservlet-name>
    <url-pattern>*.jsurl-pattern>
  servlet-mapping>
  <servlet-mapping>
    <servlet-name>defaultservlet-name>
    <url-pattern>*.cssurl-pattern>
  servlet-mapping>
  <servlet-mapping>
    <servlet-name>defaultservlet-name>
    <url-pattern>*.jpgurl-pattern>
  servlet-mapping>
web-app>

jdbc.properties 配置数据库相关属性

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.181.128:3306/library?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=981001

在 spring.xml 中配置 Spring 和 Mybatis 的整合


<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:p="http://www.springframework.org/schema/p"
       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-4.3.xsd
">
    
    <context:property-placeholder location="classpath:jdbc.properties" />

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
        <property name="jdbcUrl" value="${jdbc.url}">property>
        <property name="driverClass" value="${jdbc.driver}">property>
        <property name="initialPoolSize" value="5">property>
        <property name="maxPoolSize" value="10">property>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource">property>
        <property name="mapperLocations" value="classpath:com/dasheng/repository/*.xml">property>
        <property name="configLocation" value="classpath:config.xml">property>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dasheng.repository">property>
    bean>

beans>

config.xml 中配置一些 MyBatis 辅助信息,比如打印 SQL 等。



<configuration>
    <settings>
        
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    settings>

    <typeAliases>
        
        <package name="com.dasheng.entity"/>
    typeAliases>

configuration>

配置 springmvc.xml


<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-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    
    <mvc:annotation-driven>mvc:annotation-driven>
    
    <context:component-scan base-package="com.dasheng">context:component-scan>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/">property>
        <property name="suffix" value=".jsp">property>
    bean>
beans>

实体类Book

package com.dasheng.entity;

import lombok.Data;

@Data
public class Book {
    private int id;
    private String name;
    private String author;
    private double price;
}

BookRepository

package com.dasheng.repository;

import com.dasheng.entity.Book;

import java.util.List;

public interface BookRepository {
    public List<Book> findAll();
}

BookRepository.xml



<mapper namespace="com.dasheng.repository.BookRepository">
    <select id="findAll" resultType="Book">
        select * from book
    select>
mapper>

BookService

package com.dasheng.service;

import com.dasheng.entity.Book;

import java.util.List;

public interface BookService {
    public List<Book> findAll();
}

BookServiceimpl

package com.dasheng.service.impl;

import com.dasheng.entity.Book;
import com.dasheng.repository.BookRepository;
import com.dasheng.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookRepository bookRepository;

    @Override
    public List<Book> findAll() {
        return bookRepository.findAll();
    }
}

BookHandler

package com.dasheng.controller;

import com.dasheng.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/book")
public class BookHandler {
    @Autowired
    private BookService bookService;

    @GetMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("list", bookService.findAll());
        return modelAndView;
    }
}

此时SSM框架就整合完成了,我的目录结构如下:

SSM框架整合_第3张图片
完整代码下载地址:
百度网盘地址 提取码:axks

你可能感兴趣的:(ssm)