mybatis-plus study

<?xml version="1.0" encoding="UTF-8"?<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

 

application.yml

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/xiejiaohui?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

studentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo">
    <select id="selectMapById" resultType="map">
        select id, name, age, email from student where id = #{id}
    </select>
</mapper>
StudentMapper.java
package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.pojo.Student;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
public interface StudentMapper extends BaseMapper<Student> {

    Map<String, Object> selectMapById(Long id);

}
Student.java
package com.example.demo.pojo;

import lombok.*;

//@NoArgsConstructor
//@AllArgsConstructor
//@Getter
//@Setter
//@EqualsAndHashCode
@Data
public class Student {

    private Long id;

    private String name;

    private Integer age;

    private String email;

}
DemoApplication.java
package com.example.demo;


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

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

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

}
MyBatisPlusTest.java
package com.example.demo;

import com.example.demo.mapper.StudentMapper;
import com.example.demo.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class MyBatisPlusTest {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void testSelectList() {
        List<Student> list = studentMapper.selectList(null);
        list.forEach(System.out::println);
    }

    @Test
    public void testInsert() {
        // INSERT INTO student ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
        Student student = new Student();
        student.setName("lisi");
        student.setAge(15);
        student.setEmail("lisi@netbank.com");
        int result = studentMapper.insert(student);
        System.out.println("result: " + result);
    }

    @Test
    public void testDelete() {
        // DELETE FROM student WHERE id=?
        int result = studentMapper.deleteById(1533374669196050433L);
        System.out.println("result: " + result);

        // DELETE FROM student WHERE name = ? AND age = ?
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("name", "Sandy");
//        map.put("age", 18);
//        int result = studentMapper.deleteByMap(map);
//        System.out.println("result: " + result);

        // DELETE FROM student WHERE id IN ( ? , ? , ? )
//        List<Long> list = Arrays.asList(1L, 2L, 3L);
//        int result = studentMapper.deleteBatchIds(list);
//        System.out.println("result: " + result);

    }

    @Test
    public void testUpdate() {
        // UPDATE student SET name=?, age=?, email=? WHERE id=?
        Student student = new Student();
        student.setName("Wang Wu");
        student.setAge(22);
        student.setId(2L);
        student.setEmail("WangWu@netbank.com");
        int result = studentMapper.updateById(student);
        System.out.println("result: " + result);
    }

    @Test
    public void testSelect() {
        // SELECT id,name,age,email FROM student WHERE id=?
//        Student student = studentMapper.selectById(1L);
//        System.out.println(student);

        // SELECT id,name,age,email FROM student WHERE id IN ( ? , ? , ? )
//        List<Long> list = Arrays.asList(1L, 2L, 3L);
//        List<Student> list2 = studentMapper.selectBatchIds(list);
//        list2.forEach(System.out::println);

        // SELECT id,name,age,email FROM student WHERE name = ? AND age = ?
//        Map<String, Object> map = new HashMap<>();
//        map.put("name", "Jame");
//        map.put("age", 18);
//        List<Student> list2 = studentMapper.selectByMap(map);
//        list2.forEach(System.out::println);

        // SELECT id,name,age,email FROM student
//        List<Student> students = studentMapper.selectList(null);
//        students.forEach(System.out::println);

        Map<String, Object> map = studentMapper.selectMapById(1L);
        System.out.println("map: " + map);

    }
}

 

posted @ 2022-06-05 21:36  Josh_Xie  阅读(21)  评论(0编辑  收藏  举报