Java中使用MyBatis-Plus操作数据库的实例
mybatis-plus
mybatis-plus (opens new window)(简称 mp)是一个 mybatis (opens new window)的增强工具,在 mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
mybatis可以直接在xml中通过sql语句操作数据库,很灵活。但其操作都要通过sql语句进行,就必须写大量的xml文件,很麻烦。mybatis-plus就很好的解决了这个问题。
官网
使用
这里我用的数据库是mysql8,新建test数据库,并创建user表
建表语句
set names utf8mb4; set foreign_key_checks = 0; -- ---------------------------- -- table structure for user -- ---------------------------- drop table if exists `user`; create table `user` ( `id` varchar(50) character set utf8 collate utf8_general_ci not null comment '主键', `name` varchar(20) character set utf8 collate utf8_general_ci null default null comment '姓名', `age` int(0) null default null comment '年龄', `address` varchar(50) character set utf8 collate utf8_general_ci null default null comment '地址', primary key (`id`) using btree ) engine = innodb character set = utf8 collate = utf8_general_ci row_format = dynamic; set 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</artifactid> </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> <dependency> <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: false # 服务模块 devtools: restart: # 热部署开关 enabled: true
application-dev.yml
# 开发环境配置 server: # 服务器的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?useunicode=true&characterencoding=utf8&zerodatetimebehavior=converttonull&usessl=true&servertimezone=gmt%2b8 username: 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: select 'x' test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: true max-open-prepared-statements: 50 max-pool-prepared-statement-per-connection-size: 20
编写实体类user.java,这里用到lombok
@data public class user { private string id; private string name; private integer age; private string address; }
创建mapper文件夹,并编写 mapper 类 usermapper.java
public interface usermapper extends basemapper<user> { }
在 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 usermapper usermapper; @test public void testinsert() { system.out.println(("----- insert method test ------")); user user = new user(); // 自动生成主键,例如550e8400-e29b-11d4-a716-446655440000 string id = uuid.randomuuid().tostring(); string replaceall = id.replaceall("-", ""); //550e8400e29b11d4a716446655440000 user.setid(replaceall); user.setname("张三"); user.setage(21); user.setaddress("北京市海淀区"); int insert = usermapper.insert(user); if(insert>0){ system.out.println("插入成功:"+user); }else{ system.out.println("插入失败!"); } }
测试查询所有
@autowired private usermapper usermapper; @test public void testselect() { system.out.println(("----- selectall method test ------")); list<user> userlist = usermapper.selectlist(null); userlist.foreach(system.out::println); }
测试删除数据
删除姓名等于“张三”的记录
@autowired private usermapper usermapper; @test public void testdelete() { system.out.println(("----- delete method test ------")); lambdaquerywrapper<user> wrapper = new lambdaquerywrapper<>(); user user = new user(); user.setname("张三"); wrapper.eq(user::getname, user.getname()); int delete = usermapper.delete(wrapper); if(delete>0){ system.out.println("删除成功:"+user); }else{ system.out.println("删除失败!"); } }
测试修改数据
修改前先执行下插入
修改还可以用usermapper.updatebyid(t entity)方法来修改,传入一个实体类
@autowired private usermapper usermapper; @test public void testupdate() { system.out.println(("----- update method test ------")); lambdaquerywrapper<user> wrapper = new lambdaquerywrapper<>(); user user = new user(); user.setname("张三"); wrapper.eq(user::getname, user.getname()); user usernew = new user(); usernew.setname("李四"); int update = usermapper.update(usernew, wrapper); if(update>0){ system.out.println("修改成功:"+user); }else{ system.out.println("修改失败!"); } }
到此这篇关于java中使用mybatis-plus操作数据库的实例的文章就介绍到这了,更多相关mybatis-plus操作数据内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: Android 9 读写eeprom at24c256
下一篇: 安卓NDK程序崩溃调试方法
推荐阅读
-
Java中数组操作 java.util.Arrays 类常用方法的使用
-
Spring jdbc中数据库操作对象化模型的实例详解
-
Oracle数据库中emp表的基本操作实例讲解
-
Oracle中的SQL使用操作实例
-
Drupal7中常用的数据库操作实例
-
PHP中MongoDB数据库的连接、添加、修改、查询、删除等操作实例
-
HTML5中indexedDB 数据库的使用实例
-
使用JAVA中的动态代理实现数据库连接池 JavaSQLJDBC应用服务器互联网
-
eclipse Java web项目数据库由oracle更改为mysql中遇到的问题(使用JPA注解)附上修改过程
-
Jedis如何使用(java操作redis数据库的工具)