当前位置: 首页  >> 关于我们  >> 帮助中心  >> 查看详情

怎么在spring boot2中使用mybatis实现增删改查操作

本篇文章给大家分享的是有关怎么在spring boot2中使用mybatis实现增删改查操作,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

新建springboot项目

使用idea新建springboot项目(springboot项目快速搭建)

(1)new project

(2)gav设置

2.2 项目整体图及说明2.2.1 整体图

2.2.2 说明

项目包含4大内容

(1)pom.xml

maven项目必备,用于定义项目、获取jar包、打包等。

(2)项目配置文件

有两个,一个是项目内配置文件;一个是用于mybatis-generate生成相关数据库操作文件。

(3)spcrudapplication

项目启动类,springboot项目必备。

(4)springmvc对应类。

包含controller、service、db等相关类。

2.3 详细说明

2.3.1 pom文件

 4.0.0   org.springframework.boot  spring-boot-starter-parent  2.1.7.RELEASE     com.laowang spcrud 0.0.1-SNAPSHOT spcrud Demo project for Spring Boot   1.8         org.springframework.boot   spring-boot-starter-web       org.springframework.boot   spring-boot-starter-test   test         mysql   mysql-connector-java   8.0.15       org.springframework.boot   spring-boot-starter-jdbc   2.1.5.RELEASE         io.springfox   springfox-swagger2   2.7.0       io.springfox   springfox-swagger-ui   2.7.0          org.mybatis.spring.boot  mybatis-spring-boot-starter  2.1.0              src/main/resources/          src/main/java         **/*.xml                  org.springframework.boot    spring-boot-maven-plugin          org.mybatis.generator    mybatis-generator-maven-plugin    1.3.5         src/main/resources/generatorConfig.xml     true     true          

说明:

包含5块内容

(1)web启动包 ;

(2)数据库 ;

(3)swagger;

(4)mybatis;

(5)打包;

2.3.2 资源文件

(1)application.properties

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ruanjianlaowang?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8spring.datasource.username=rootspring.datasource.password=root

说明: 数据库配置文件,连接、用户名、密码

(2)mybatis资源文件

                                                              

说明:

包含几块内容:

(a)classPathEntry 标签定义的是mysql-connector的jar包地址

(b)jdbcConnection 数据库连接信息

(c)javaModelGenerator、sqlMapGenerator、javaClientGenerator定义的是生成文件存放的地址;

(d)table具体执行生成代码的tabel,增加几个标签,不生成example方法。

2.3.3 启动类

package com.laowang.spcrud;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication@EnableSwagger2@MapperScan("com.laowang.spcrud.db.mapper")public class SpcrudApplication {

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

}

说明:

@SpringBootApplication所有springboot项目启动必备

@EnableSwagger2 启动swagger

@MapperScan加载mpper文件。

2.3.4 springmvc类

(1)TestController

package com.laowang.spcrud;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication@EnableSwagger2@MapperScan("com.laowang.spcrud.db.mapper")public class SpcrudApplication {

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

}

ctroller类包含增删改查4个方法,使用了rest请求的方式。

(2)TestService

package com.laowang.spcrud.service;import com.laowang.spcrud.db.entity.TLaowang;import com.laowang.spcrud.db.mapper.TLaowangMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class TestService {
 @Autowired private TLaowangMapper tLaowangMapper;


 /**
  * 增加
  * @auther: 软件老王
  */ public void insertRecord(TLaowang tLaowang) {
  tLaowangMapper.insert(tLaowang);
 }

 /**
  * 删除
  * @auther: 软件老王
  */ public void deleteByPrimaryKey(int id) {
  tLaowangMapper.deleteByPrimaryKey(id);
 }
 /**
  * 更新
  * @auther: 软件老王
  */ public void updateByPrimaryKeySelective(TLaowang tLaowang) {
  tLaowangMapper.updateByPrimaryKeySelective(tLaowang);
 }

 /**
  * 查询
  * @auther: 软件老王
  */ public TLaowang selectByPrimaryKey(int id) {
  return tLaowangMapper.selectByPrimaryKey(id);
 }
}

TestService类,增删改查的服务类。

(3)实体类TLaowang

package com.laowang.spcrud.db.entity;public class TLaowang {
 private Integer id;

 private String name;

 private String password;

 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 == null ? null : name.trim();
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password == null ? null : password.trim();
 }
}

操作实体类,包含三个字段:id、name、password

(4)mpper接口类TLaowangMapper

package com.laowang.spcrud.db.mapper;import com.laowang.spcrud.db.entity.TLaowang;public interface TLaowangMapper {
 int deleteByPrimaryKey(Integer id);

 int insert(TLaowang record);

 int insertSelective(TLaowang record);

 TLaowang selectByPrimaryKey(Integer id);

 int updateByPrimaryKeySelective(TLaowang record);

 int updateByPrimaryKey(TLaowang record);
}

(5)mapper接口xml

       id, name, password
   select 
  from t_laowang
 where id = #{id,jdbcType=INTEGER}
   delete from t_laowang
 where id = #{id,jdbcType=INTEGER}
     SELECT LAST_INSERT_ID()
  insert into t_laowang (name, password)
 values (#{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR})
     SELECT LAST_INSERT_ID()
  insert into t_laowang
     name,
      password,
        #{name,jdbcType=VARCHAR},
      #{password,jdbcType=VARCHAR},
      update t_laowang
     name = #{name,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
    where id = #{id,jdbcType=INTEGER}
   update t_laowang
 set name = #{name,jdbcType=VARCHAR},
  password = #{password,jdbcType=VARCHAR}
 where id = #{id,jdbcType=INTEGER}
 

4与5在一起,这里使用了mybatis自动生成的增删改查方法,未做扩展,真实项目中除了这几个外,肯定还会做些扩展,比如根据name查询等。

2.4 数据库建表语句

CREATE TABLE `t_laowang` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(50) DEFAULT NULL,
 `password` varchar(50) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

以上就是怎么在spring boot2中使用mybatis实现增删改查操作,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注辰讯云资讯频道。

  • 24H在线
  • Tg纸飞机