一分钟整合SSM框架,四分钟梳理框架脉络

速成整合SSM框架

集成项目框架

springmvc,spring是web层的框架,mybatis是应用层和数据库之间的联系

  1. 从pom.xml中导入依赖(spring)(完整的pom.xml在文章最后)

spring-webmvc
spring-jdbc
spring-aop
spring-aspects

mybatis
mybatis-spring
mysql-connector-java

c3p0
jstl
javax.servlet-api
lombok
commons-dbcp

web.xml

  1. web.xml中主要包含的是对spring框架和springmvc框架的初始化

web.xml中文件的头变化


<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
web-app>

<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>encodingparam-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>

Spring.xml

  1. Spring.xml中的头文件

<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: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/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">
beans>    
  1. 使用c3p0连接池配置spring的数据源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="root">property>
    <property name="password" value="123456">property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/book?serverTimezone=CST&useSSL=false">property>
    <property name="driverClass" value="com.mysql.cj.jdbc.Driver">property>
    <property name="initialPoolSize" value="5">property>
    <property name="maxPoolSize" value="10">property>
bean>
  1. 使用mybatis中的spring框架来进行识别

读取mapper.xml配置文件,并读取mybatis的配置文件

<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource">property>
    <property name="mapperLocations" value="classpath:com/gao/repository/*.xml">property>
    <property name="configLocation" value="classpath:config.xml">property>
bean>
  1. 扫描自定义的mapper接口:spring中管理mybatis配置管理mybatis

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

mybatis.xml

头文件:


DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--设置mybatis中的日志信息的输出-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <!--在文件中实现对实体类对象的创建-->
    <typeAliases>
        <package name="com.gao.enity"/>
    </typeAliases>
</configuration>

springmvc.xml

  • 头文件

<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: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/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">
beans>
  • 主要内容

<mvc:annotation-driven>mvc:annotation-driven>

<context:component-scan base-package="com.gao">context:component-scan>

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

至此SSM配置文件完毕:

接下来我们创建enity层,Dao层,Service层,Controller层的文件:

文件目录如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-59t3T8NC-1650613152194)(C:\Users\l\AppData\Roaming\Typora\typora-user-images\image-20220422150343675.png)]

book.java

package com.gao.enity;

import lombok.Data;

@Data
public class Book {
    int bid;
    int cateid;
    String bname;
    String author;
}

实体类创建完毕.

在com.gao.reposity中创建dao层的接口和mapper.xml文件

BookRepository.java

package com.gao.repository;

import com.gao.enity.Book;

import java.util.List;

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

BookRepository.xml文件


DOCTYPE mapper PUBLIC "-//mybatis.org//DTDMapper3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gao.repository.BookRepository">
    <select id="findAll" resultType="Book">
        select * from book
    select>
mapper>

Dao层创建完毕.

创建service层中的配置

Bookservice.java

package com.gao.service;

import com.gao.enity.Book;

import java.util.List;

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

BookServiceImpl.java

package com.gao.service.Impl;

import com.gao.enity.Book;
import com.gao.repository.BookRepository;
import com.gao.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;

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

Controller层

UserController

package com.gao.controller;

import com.gao.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 UserController {
     @Autowired
     private BookService bookService;
     @GetMapping("/findAll")
     public ModelAndView index(){
         ModelAndView modelAndView = new ModelAndView();
         modelAndView.setViewName("index");
         modelAndView.addObject("list",bookService.findAll());
         return modelAndView;
     }
}

controller层完毕

整合过程中遇到的错误:

一分钟整合SSM框架,四分钟梳理框架脉络_第1张图片

别名设置错误问题:

解决方案:

​引起别名问题的就是在设置Mybatis的配置文件中的配置别名一项出现了问题

我们再来到mybatis.xml配置文件,发现别名配置错误.

由于系统时区差异引起的错误

serverTimezone=CST

Pom.xml

<dependencies>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>5.0.11.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>5.0.11.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>5.0.11.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>5.0.11.RELEASEversion>
    dependency>

    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.4.5version>
    dependency>
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.3.1version>
    dependency>
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>8.0.22version>
    dependency>
    <dependency>
      <groupId>com.mchangegroupId>
      <artifactId>c3p0artifactId>
      <version>0.9.5.3version>
    dependency>
    <dependency>
      <groupId>jstlgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
    dependency>
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <version>1.18.22version>
    dependency>
    <dependency>
      <groupId>commons-dbcpgroupId>
      <artifactId>commons-dbcpartifactId>
      <version>1.4version>
    dependency>
  dependencies>


groupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
    dependency>
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <version>1.18.22version>
    dependency>
    <dependency>
      <groupId>commons-dbcpgroupId>
      <artifactId>commons-dbcpartifactId>
      <version>1.4version>
    dependency>
  dependencies>

该项目的git源码地址:https://gitee.com/hzir/quickSSM
在此项目的基础上又新增了分页插件的使用,但是基本的SSM框架并没有改变

你可能感兴趣的:(spring,mvc)