SSM框架整合(简易版)

《我的第一张博客=>ssm框架整合使用》

1.配置环境

使用:

  • 开发工具:IDEA2021

  • MySQL 5.7.26

  • Tomcat 8.5.56

  • Maven 3.6.1

 要求:提前配置好已经测试过,避免出现问题

1.数据库,可选择简单得数据库做测试

2. maven依赖导入:

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.47</version>
                </dependency>

        <!--   servlet jsp-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.8.M1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2.1-b03</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--    mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!--      spring-->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

所用到得依赖

静态资源得导出

<!--    静态资源导出-->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources/</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

3.建立基本结构和配置框架!

  • com.ssmbuild.pojo

  • com.ssmbuild.dao

  • com.ssmbuild.service

  • com.ssmbuild.controller

  • com.ssmbuild.filer

 mybatis-config.xml基础配置

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration:配置,核心配置-->
<configuration>

</configuration>

spring:applicationContext.xml基础配置

<?xml version="1.0" encoding="UTF8"?>
<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.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

</beans>

IDEA关联数据源


4. 编写MyBatis.xml的核心配置文件(仅仅为实体类起了别名)

 编写实体类在pojo包中(部分。下面还要有getter和setter方法和同string()方法自己加上)

 6.编写Mapper接口(一些简单得crud查询)

import com.ssmbuild.pojo.books;

import java.util.List;

public interface BookMapper {
    //增加一本书
    int add(books books);
    //删除一本书
    int delete(int bookID);
    //更新一本书
    int update(books books);
    //查询一本书
    books queryByID(int bookID);
    //查询全部书
    List<books> queryAllbook();
    //用名字查询
    List<books> QuBooK(String bookName);
    //共有多少本书
    int BookCount();
}

7.建议同级接口下建立mapper.xml(简单得查询所有书籍为例)

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:绑定接口-->
<mapper namespace="com.ssmbuild.dao.BookMapper">
    <!--id:接口的方法名,resultType:方法返回的结果类型 parameterType:传入参数返回值类型-->
    <select id="queryAllbook" resultType="books">
        select * from books
    </select>
</mapper>

8.编写service层调用dao层

package com.ssmbuild.service;

import com.ssmbuild.pojo.books;

import java.util.List;
public interface BookService {
    //增加一本书
    int add(books books);
    //删除一本书
    int delete(int bookID);
    //更新一本书
    int update(books books);
    //查询一本书
    books queryByID(int bookID);
    //查询全部书
    List<books> queryAllbook();
    //用名字查询
    List<books> QuBooK(String bookName);
    //共有多少本书
    int BookCount();
}

service接口实现类(同样举例查询所有用户)

 基础得mybatis和实体类得初始已经好了

Spring.xml整合配置

我们使用得是半自动化得dbcp数据源

编写Spring整合Mybatis的相关的配置文件;spring-dao.xml(spring.xml分层)

 Spring整合service层:spring-service.xml

  Spring整合MVC层:spring-MVC.xml

 Spring配置整合文件,applicationContext.xml把spring.xml再统一管理一目了然

 繁琐的配置终于搞定了

 10.测试是否可以正常查询

在我们的测试包下

package daoBug;

import com.ssmbuild.pojo.books;
import com.ssmbuild.service.BookService;
import com.ssmbuild.service.BookServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestNo {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookServiceImpl impl = context.getBean("BookserviceImpl", BookServiceImpl.class);
        List<books> books = impl.queryAllbook();
        for (com.ssmbuild.pojo.books book : books) {
             System.out.println(book);
        }
    }

    }

成功打印

 如果这里没有测试出出现了错误

 这个是pom中静态资源导出没有添加

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

测试到这里已经完成了

让我们进入controller层来和前端交互

 controller测试

package com.ssmbuild.controller;

import com.ssmbuild.pojo.books;
import com.ssmbuild.service.BookServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class controllerTest {
    @RequestMapping("/ts1")
    public String te(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookServiceImpl impl = context.getBean("BookserviceImpl", BookServiceImpl.class);
        List<books> books = impl.queryAllbook();
        return books.toString();
    }
}

如果出现404,就是没有把依赖的jar包导入

 

 在WEB-INF目录下建立lib目录

 

 选中lib点“+”号

 

 全选并添加就可以了




后端的简单查询配置就完成了

 以后的前端知识要自己再努力了加油

奥里给!!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值