首页 > 作文

Java中使用MyBatis

更新时间:2023-04-04 21:45:47 阅读: 评论:0

目录
mybatis-plus官网使用测试数据插入数据库测试查询所有测试删除数据测试修改数据

mybatis-plus

mybatis-plus (opens new window)(简称 mp)是一个 mybatis (opens new window)的增强工具,在 mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

mybatis可以直接在xml中通过sql语句操作数据库,很灵活。但其操作都要通过sql语句进行,就必须写大量的xml文件,很麻烦。mybatis-plus就很好的解决了这个问题。

官网

mybatis-plus 官方文档

使用

这里我用的数据库是mysql8,新建test数据库,并创建ur表

建表语句

t names utf8mb4;t foreign_key_checks = 0; -- ------------------------------ table structure for ur-- ----------------------------drop table if exists `ur`;create table `ur`  (  `id` varchar(50) character t utf8 collate utf8_general_ci not null comment '主键',  `name` varchar(20) character t utf8 collate utf8_general_ci null default null comment '姓名',  `age` int(0) null default null comment '年龄',  `address` varchar(50) character t utf8 collate utf8_general_ci null default null comment '地址',  primary key (`id`) using btree) engine = innodb character t = utf8 collate = utf8_general_ci row_format = dynamic; t foreign_key_checks = 1;

引入依赖

这里我用的数据库是mysql8

     <parent>        <groupid>org.springframework.boot</groupid>        <artifactid>spring-boot-starter-parent</artifactid>        <version>2.6.3</version>        <relativepath/>    </parent>     <dependencies>        <dependency>            <groupid>org.projectlombok</groupid>            <artifactid>lombok</arti服装店装修图片fac神探高伦布电视剧tid>       消防管理制度 </dependency>        <dependency>            <groupid>org.springframework.boot</groupid>            <artifactid>spring-boot-starter</artifactid>        </dependency>        <dependency>            <groupid>org.springframework.boot</groupid>            <artifactid>spring-boot-devtools</artifactid>            <scope>runtime</scope>            <optional>true</optional>        </dependency>        <dependency>            <groupid>org.springframework.boot</groupid>            <artifactid>spring-boot-starter-test</artifactid>            <scope>test</scope>        </dependency>        <depen情绪的解析dency>            <groupid>org.springframework.boot</groupid>            <artifactid>spring-boot-test</artifactid>        </dependency>        <dependency>            <groupid>junit</groupid>            <artifactid>junit</artifactid>        </dependency>        <dependency>            <groupid>org.springframework</groupid>            <artifactid>spring-test</artifactid>        </dependency>        <dependency>            <groupid>com.baomidou</groupid>            <artifactid>mybatis-plus-boot-starter</artifactid>            <version>3.5.1</version>        </dependency>        <!--druid连接池-->        <dependency>            <groupid>com.alibaba</groupid>            <artifactid>druid-spring-boot-starter</artifactid>            <version>1.1.9</version>        </dependency>        <dependency>            <groupid>mysql</groupid>            <artifactid>mysql-connector-java</artifactid>            <version>8.0.25</version>        </dependency>    </dependencies>

分别创建application.yml及application-dev.yml文件

application.yml

# spring配置spring:  # 环境设置  profiles:    active: dev  # 模板引擎  thymeleaf:    mode: html    encoding: utf-8    # 禁用缓存    cache: fal  # 服务模块  devtools:    restart:      # 热部署开关      enabled: true

application-dev.yml

# 开发环境配置rver:  # 服务器的http端口,默认为80  port: 8081 spring:  datasource:    type: com.alibaba.druid.pool.druiddatasource    driver-class-name: com.mysql.cj.jdbc.driver    druid:      url: jdbc:mysql://localhost:3307/test?uunicode=true&characterencoding=utf8&zerodatetimebehavior=converttonull&ussl=true&rvertimezone=gmt%2b8      urname: root      password: 123456      initial-size: 5      min-idle: 10      max-active: 20      max-wait: 60000      time-between-eviction-runs-millis: 60000      min-evictable-idle-time-millis: 300000      validation-query: lect 'x'      test-while-idle: true      test-on-borrow: fal      test-on-return: fal      pool-prepared-statements: true      max-open-prepared-statements: 50      max-pool-prepared-statement-per-connection-size: 20

编写实体类ur.java,这里用到lombok

@datapublic class ur {    private string id;    private string name;    private integer age;    private string address;}

创建mapper文件夹,并编写mapper 类urmapper.java

public interface urmapper extends bamapper<ur> { }

在 spring boot 启动类中添加@mapperscan注解,扫描 mapper 文件夹:

@springbootapplication@mapperscan("com.mybatisplus.mapper")public class mainapplication {    public static void main(string[] args)    {        springapplication.run(mainapplication.class, args);    }}

添加测试类,进行功能测试:

测试数据插入数据库

    @autowired    private urmapper urmapper;     @test    public void testinrt() {        system.out.println(("----- inrt method test ------"));        ur ur = new ur();        // 自动生成主键,例如550e8400-e29b-11d4-a716-446655440000        string id = uuid.randomuuid().tostring();        string replaceall = id.replaceall("-", "");        //550e8400e29b11d4a716446655440000        ur.tid(replaceall);        ur.tname("张三");        ur.tage(21);        ur.taddress("北京市海淀区");        int inrt = urmapper.inrt(ur);        if(inrt>0){            system.out.println("插入成功:"+ur);        }el{            system.out.println("插入失败!");        }    }

测试查询所有

    @autowired    private urmapper us越俎代庖怎么读音ermapper;            @test    public void testlect() {        system.out.println(("----- lectall method test ------"));        list<ur> urlist = urmapper.lectlist(null);        urlist.foreach(system.out::println);    }

测试删除数据

删除姓名等于“张三”的记录

    @autowired    private urmapper urmapper;        @test    public void testdelete() {        system.out.println(("----- delete method test ------"));        lambdaquerywrapper<ur> wrapper = new lambdaquerywrapper<>();        ur ur = new ur();        ur.tname("张三");        wrapper.eq(ur::getname, ur.getname());        int delete = urmapper.delete(wrapper);        if(delete>0){            system.out.println("删除成功:"+ur);        }el{            system.out.println("删除失败!");        }    }

测试修改数据

修改前先执行下插入

修改还可以用urmapper.updatebyid(t entity)方法来修改,传入一个实体类

    @autowired    private urmapper urmapper;        @test    public void testupdate() {        system.out.println(("----- update method test ------"));        lambdaquerywrapper<ur> wrapper = new lambdaquerywrapper<>();        ur ur = new ur();        ur.tname("张三");        wrapper.eq(ur::getname, ur.getname());        ur urnew = new ur();        urnew.tname("李四");        int update = urmapper.update(urnew, wrapper);        if(update>0){            system.out.println("修改成功:"+ur);        }el{            system.out.println("修改失败!");        }    }

到此这篇关于java中使用mybatis-plus操作数据库的实例的文章就介绍到这了,更多相关mybatis-plus操作数据内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 21:45:46,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/4db14f2d60cdac2475c73c3785d87743.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Java中使用MyBatis.doc

本文 PDF 下载地址:Java中使用MyBatis.pdf

标签:测试   数据库   数据   操作
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图