首页 > 其他分享 >基于springboot实现SSM整合

基于springboot实现SSM整合

时间:2023-02-16 22:23:26浏览次数:33  
标签:springboot id SSM Book 整合 import com public itheima

(1)SpringBoot整合Spring(不存在)

(2)SpringBoot整合SpringMVC(不存在)

(3)SpringBoot整合MyBatis(主要)

一、新建springboot项目。

 

 在application.yml配置文件中添加数据源信息

 

二、创建domain.book表对象

package com.itheima.domain;

public class Book {
    private Integer id;
    private String name;
    private String type;
    private String description;

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", description='" + description + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

三、创建dao.BookDao接口,书写数据库方法

package com.itheima.dao;

import com.itheima.domain.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;


@Mapper
public interface BookDao {
    @Select("select * from tb_book where id = #{id}")
    public Book getById(Integer id);
}

四、创建service.BookService接口,书写对应数据库方法

package com.itheima.service;

import com.itheima.domain.Book;

public interface BookService {
    public Book getById(Integer id);
}

五、创建service.impl.BookServiceImpl类封装实现BookService接口方法。

package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    @Override
    public Book getById(Integer id) {
        Book book = bookDao.getById(id);
        return book;
    }
}

六、在test中测试

package com.itheima;

import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot08MybatisApplicationTests {

    @Autowired
    private BookService bookService;

    @Test
    void contextLoads() {
        Book book = bookService.getById(12);
        System.out.println(book);
    }

}

结果:

 

 不用内置数据源,设置druid数据源。

在pom中添加druid依赖:

<!--        druid数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

 

标签:springboot,id,SSM,Book,整合,import,com,public,itheima
From: https://www.cnblogs.com/fxzm/p/17128515.html

相关文章

  • Spring 整合 RabbitMQ
    Spring整合RabbitMQ创建spring项目引入pompom依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns......
  • springboot整合junit
    packagecom.itheima1;importcom.itheima.Springboot07TestApplication;importcom.itheima.service.BookService;importorg.junit.jupiter.api.Test;importorg.s......
  • springboot多环境开发兼容问题(Maven和boot)
    <?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:sch......
  • springboot---多环境启动命令格式
    一、多环境命令启动maven插件中首先clean,再package打包,(修改字符集为UTF-8)使用cmd命令java-jars(Tab键自动补全) -spring.profiles.active=test启动项目  修改端......
  • Springboot项目中注入bean失败的问题排查
    Springboot项目中注入bean失败的问题排查这是一个Spring常见的问题,下面我们从测试方法和普通方法出问题两个角度来下如何解决测试方法先查看目录是否有误测试类的包名......
  • springboot基础配置yml
    yaml语法规则大小写敏感属性层级关系使用多行描述,每行结尾使用冒号结束使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)属性值前面添加空格(属性名与属......
  • springboot自定义校验工具类
    参考:https://betheme.net/news/txtlist_i120686v.html?action=onClickhttps://www.ngui.cc/el/2571188.html?action=onClick一、原生注解在springboot中,我们可以使用ja......
  • SpringBoot
    一.SpringBoot 1.1SpringBoot概述1.1.1什么是SpringBootSpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework同属于spring的产品:其最主......
  • springboot 使用微信支付
    参考:Java实现微信支付_被编程征服的秃发女子的博客-CSDN博客一.微信支付流程支付流程:用户点击支付按钮调用接口[/deposit]=>返回给小程序payInfo和订单编号orderNum......
  • 前后端分离项目(vue+springboot)集成pageoffice实现在线编辑office文件
    前后端分离项目下使用PageOffice原理图集成步骤前端vue项目在您Vue项目的根目录下index.html中引用后端项目根目录下pageoffice.js文件。例如:<scripttype="text/......