栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis-plus in(mybatis-plus鍏ラ棬)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis-plus in(mybatis-plus鍏ラ棬)

使用Mybatis Plus进行curd操作

1、创建数据库,创建数据库表
安装SQLyog 64bit
大佬整理SQLyog Ultimate版本
在SQLyog中新建链接,新建数据库,新建表,并插入数据

2、创建springboot工程

默认就行了。
3、在项目中引入mp依赖
在pom.xml中引入相关依赖,注意JDK版本需要1.8以上。


        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.1
        
        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
            org.projectlombok
            lombok
            true
        

4、配置数据库信息
在application.properties设置数据库

5、编写核心代码

package com.example.demomp.entity;

import lombok.Data;

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;

}
package com.example.demomp.mapper;

import com.baomidou.mybatisplus.core.mapper.baseMapper;
import com.example.demomp.entity.User;


public interface UserMapper extends baseMapper {

}
package com.example.demomp;

import com.example.demomp.entity.User;
import com.example.demomp.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class DemoMpApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void findAll() {
        List users = userMapper.selectList(null);
        System.out.println(users);
    }

    void contextLoads() {
    }

}

6、查看sql输出日志
在application.properties配置

#mybatis Log
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

显示操作详细信息,便于排查错误

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@750f64fe] was not registered for synchronization because synchronization is not active
2022-03-19 16:25:14.189  INFO 30508 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-03-19 16:25:14.552  INFO 30508 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@1734902032 wrapping com.mysql.cj.jdbc.ConnectionImpl@6d293993] will not be managed by Spring
==>  Preparing: SELECT id,name,age,email FROM user
==> Parameters: 
<==    Columns: id, name, age, email
<==        Row: 1, 赵大, 38, 000001@mail.com
<==        Row: 2, 牛二, 32, 000002@mail.com
<==        Row: 3, 张三, 23, 000003@mail.com
<==        Row: 4, 李四, 20, 000004@mail.com
<==        Row: 5, 王五, 18, 000005@mail.com
<==      Total: 5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@750f64fe]

7、sql添加操作
在test中添加add测试

//add
    @Test
    public void testAdd(){
        User user = new User();
        //User.setId(to_Long(6));
        user.setName("周六");
        user.setAge(16);
        user.setEmail("000006@mail.com");
        int insert = userMapper.insert(user);
        System.out.println(insert);
    }

结果为

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@738d37fc] was not registered for synchronization because synchronization is not active
2022-03-19 16:50:05.150  INFO 32176 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-03-19 16:50:05.413  INFO 32176 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@56531119 wrapping com.mysql.cj.jdbc.ConnectionImpl@781aff8b] will not be managed by Spring
==>  Preparing: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
==> Parameters: 1505104307882504194(Long), 周六(String), 16(Integer), 000006@mail.com(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@738d37fc]
1


8、sql更新操作
根据主键更新

    //update
    @Test
    public void testUpdate(){
        User user = new User();//need Id
        user.setId(1505104307882504194L);
        user.setName("橄榄六");
        int count = userMapper.updateById(user);
        System.out.println(count);
    }
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cc8416a] was not registered for synchronization because synchronization is not active
2022-03-20 23:42:19.619  INFO 28140 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-03-20 23:42:19.966  INFO 28140 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@430845669 wrapping com.mysql.cj.jdbc.ConnectionImpl@44b940a2] will not be managed by Spring
==>  Preparing: UPDATE user SET name=? WHERe id=?
==> Parameters: 橄榄六(String), 1505104307882504194(Long)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cc8416a]
1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/776572.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号