Spring Boot Mybatis入门示例

本文档介绍了Spring Boot 2.3.4与Mybatis的整合步骤,从数据库初始化到配置添加,再到实体类、Mapper接口、配置文件的编写,最后进行测试和启动应用。在测试部分使用了Junit5,并确保数据回滚,避免污染数据库。
摘要由CSDN通过智能技术生成

Spring Boot Mybatis 入门示例


基于Spring Boot 2.3.4,Junit5

步骤说明

    整个工程的最终目录结构如下,添加文件或者新建的目录的参考:

└─src
    ├─main
    │  ├─java
    │  │  └─com
    │  │      └─mall
    │  │          └─MallWeb
    │  │              ├─controllers
    │  │              ├─mapper
    │  │              ├─model
    │  │              └─services
    │  └─resources
    │      └─mybatis
    │          └─mapper
    └─test
        └─java
            └─com
                └─mall
                    └─MallWeb
                        └─mapper

数据库初始化语句

    数据库表的初始化语句全部在下面,也可以根据自己的情况进行修改

CREATE DATABASE  IF NOT EXISTS `mall`;

USE `mall`;

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(16) NOT NULL,
  `password` varchar(16) NOT NULL,
  `phoneNumber` varchar(15) NOT NULL,
  `money` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

build.gradle:依赖添加

    需要添加下面依赖

  • ‘mysql:mysql-connector-java:8.0.14’
  • ‘org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0’
  • ‘org.projectlombok:lombok:1.16.16’
dependencies {
   
	implementation 'org.springframework.boot:spring-boot-starter-web'

	//	MySQL数据库需要
	implementation 'mysql:mysql-connector-java:8.0.14'
	//	spring mybatis
	implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'
	//	lombok,用于Entity的自动get和set方法生成,不用自己写一大推的get和set方法
	implementation 'org.projectlombok:lombok:1.16.16'

	testImplementation('org.springframework.boot:spring-boot-starter-test') {
   
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}
}

application.properties:配置添加

    配置文件的编写需要注意参数的配置,比如SSL那个一般要设置为false,driver-class-name也要注意一下,不要写错了,文件的大致内容如下:

# mybatis的config文件位置配置
mybatis.config-location=classpath:mybatis/mybatis-config.xml
# 各个表的xml文件位置配置
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.neo.model

# 数据库连接信息配置,自行更换数据库,用户名和密码
spring.datasource.url=jdbc:mysql://localhost:3306/mall?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8\
  &useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#springboot + mybatis设置将SQL语句打印到控制台
logging.level.com.mall.MallWeb.mapper=debug

代码编写

入口环境配置Mapper扫描配置

    在入口函数添加Mapper扫描配置,这样不必在每个Mapper上加上Mapper注解,大致如下:

package com.mall.MallWeb;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author lw
 */
@SpringBootApplication
@MapperScan("com.mall.MallWeb.mapper")
public class MallWebApplication {
   

	public static void main(String[] args) {
   
		SpringApplication.run(MallWebApplication.class, args);
	}

}
编写实体类

    在代码目录下创建model文件夹,用于存储实体(数据库表)。编辑实体类,大致如下:

ppackage com.mall.MallWeb.model;

import java.io.Serializable;

/**
 * @author lw
 */
public class User implements Serializable {
   

    private Long id;
    private String name;
    private String password;
    private String phoneNumber;
    private Long money;

    public User(String name, String password, String phoneNumber) {
   
        this.name = name;
        this.password = password;
        this
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值