mybatis注解方式使用增删改查

pom

XML 复制代码
 <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>3.0.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

数据库配置

mapper文件

java 复制代码
package com.qvfan.mybatistest.mapper;

import com.qvfan.mybatistest.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface EmpMapper {

    //根据id删除数据
    @Delete("delete from emp where id = #{id}")
    public void delete(Integer id);
    //public int delete(Integer id);

    //新增员工
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into emp( username,  name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
            "values (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime});")
    public void insert(Emp emp);


    //更新员工
    @Update("update emp set username =#{username},name=#{name},gender=#{gender},image=#{image},job=#{job},entrydate=#{entrydate},dept_id=#{deptId},update_time=#{updateTime} where id=#{id};")
    public void update(Emp emp);

    //根据id查询员工
        @Select("select * from emp where id = ${id}")
        public Emp getById(Integer id);

    //    @Select("select id,username,password,name,gender,image,job,entrydate,dept_id deptId,create_time createTime,update_time updateTime from emp where id = ${id}")
    //    public Emp getById(Integer id);

    //        @Results({
    //                @Result(column = "dept_id",property = "deptId"),
    //                @Result(column = "create_time",property = "createTime"),
    //                @Result(column = "update_time",property = "updateTime")
    //        })
    //        @Select("select * from emp where id = ${id}")
    //        public Emp getById(Integer id);


    //条件查询员工
    @Select("select * from emp where name like concat('%',#{name},'%') and gender =#{gender} and entrydate between #{begin} and #{end} order by  update_time desc ")
    public List<Emp> list(String name, Short gender, LocalDate begin,LocalDate end);
}

Emp类

java 复制代码
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Short gender;
    private String image;
    private Short job;
    private LocalDate entrydate;
    private Integer deptId;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}

测试

java 复制代码
package com.qvfan.mybatistest;

import com.qvfan.mybatistest.mapper.EmpMapper;
import com.qvfan.mybatistest.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@SpringBootTest
class MybatisTestApplicationTests {


    @Autowired
    private EmpMapper empMapper;

    @Test
    void contextLoads() {
    }
    @Test
    public void testDelete(){
     empMapper.delete(16);
    }

    @Test
    public void testInsert() {
       Emp emp = new Emp();
       emp.setUsername("Tom1");
       emp.setName("汤姆");
       emp.setImage("1.png");
       emp.setGender((short)1);
       emp.setJob((short)1);
       emp.setEntrydate((LocalDate.of(2000,1,1)) );
       emp.setCreateTime(LocalDateTime.now());
       emp.setUpdateTime(LocalDateTime.now());
       emp.setDeptId(1);

       empMapper.insert(emp);
       System.out.println(emp.getId());
    }


    @Test
    public void testUpdate(){
        Emp emp = new Emp();
        emp.setId(18);
        emp.setUsername("Tom12");
        emp.setName("汤姆1");
        emp.setImage("1.png");
        emp.setGender((short)1);
        emp.setJob((short)1);
        emp.setEntrydate((LocalDate.of(2000,1,1)) );
        emp.setUpdateTime(LocalDateTime.now());
        emp.setDeptId(1);

        empMapper.update(emp);
    }


    @Test
    public void testGetById(){
        Emp emp =empMapper.getById(20);
        System.out.println(emp);
    }

    @Test
    public void  testList(){
      List<Emp> list= empMapper.list("张",(short)1,LocalDate.of(2010,1,1),LocalDate.of(2020,1,1));
      System.out.println(list);
    }
}
相关推荐
艾莉丝努力练剑23 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
武子康1 小时前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
_殊途2 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
椰椰椰耶3 小时前
【Spring】拦截器详解
java·后端·spring
没有bug.的程序员4 小时前
JAVA面试宝典 - 《MyBatis 进阶:插件开发与二级缓存》
java·面试·mybatis
倔强青铜34 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
没有羊的王K5 小时前
SSM框架学习——day1
java·学习
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
又菜又爱coding5 小时前
安装Keycloak并启动服务(macOS)
java·keycloak