springboot整合mybatis

本文介绍了Spring Boot整合MyBatis的方法。首先需导入相关的pom依赖和逆向生成文件、jdbc,接着在事务管理器里配置特定代码,最后编写测试类对查询及删除方法进行测试,还涉及Bookservice类和Bookserviceimpl类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot整合mybatis

springboot整合mybatis逆向生成插件
1.第一步导入相关的pom依赖

 <resources>
            <!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <!--解决mybatis-generator-maven-plugin运行时没有将jdbc.properites文件放入target文件夹的问题-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*.properties</include>
                    <include>*.xml</include>
                    <include>*.yml</include>
                </includes>
            </resource>
        </resources>
      <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <dependencies>
                    <!--使用Mybatis-generator插件不能使用太高版本的mysql驱动 -->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

2.导入逆向生成文件以及jdbc
在这里插入图片描述
3.在事物管理器里面配置字一行,整合mybatis就是这一行代码
在这里插入图片描述
4.写一个测试类测试一下查询以及删除的方法
在这里插入图片描述
Bookservice类

package com.zhoujun.springboot02.service;

import com.zhoujun.springboot02.entity.Book;

/**
 * @author 小俊
 * @company 流弊公司董事长
 * @create  2020-12-01 11:04
 */
public interface BookService {
    int deleteByPrimaryKey(Integer bid);

    Book selectByPrimaryKey(Integer bid);



}

Bookserviceimpl类

package com.zhoujun.springboot02.service.impl;

import com.zhoujun.springboot02.entity.Book;
import com.zhoujun.springboot02.mapper.BookMapper;
import com.zhoujun.springboot02.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 小俊
 * @company 流弊公司董事长
 * @create  2020-12-01 11:05
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Override
    public int deleteByPrimaryKey(Integer bid) {
        return bookMapper.deleteByPrimaryKey(bid);
    }

    @Override
    public Book selectByPrimaryKey(Integer bid) {
        return bookMapper.selectByPrimaryKey(bid);
    }
}

测试

package com.zhoujun.springboot02.service.impl;
import com.zhoujun.springboot02.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Test;
/**
 * @author 小俊
 * @company 流弊公司董事长
 * @create  2020-12-01 11:11
 */
@SpringBootTest
public class BookServiceImplTest  {
    @Autowired
    private BookService bookService;

    @Test
    public void deleteByPrimaryKey() {
        bookService.deleteByPrimaryKey(21);
    }

    @Test
    public void selectByPrimaryKey() {
        System.out.println(bookService.selectByPrimaryKey(28));
    }
}

测试结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值