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);
    }
}
相关推荐
开开心心_Every8 分钟前
便捷的Office批量转PDF工具
开发语言·人工智能·r语言·pdf·c#·音视频·symfony
程序猿小D1 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的个人财务管理系统,推荐!
java·数据库·mysql·spring·毕业论文·ssm框架·个人财务管理系统
转转技术团队2 小时前
二奢仓店的静默打印代理实现
java·后端
liulilittle2 小时前
LinkedList 链表数据结构实现 (OPENPPP2)
开发语言·数据结构·c++·链表
钢铁男儿2 小时前
C# 接口(什么是接口)
java·数据库·c#
丶小鱼丶3 小时前
排序算法之【归并排序】
java·排序算法
上上迁3 小时前
分布式生成 ID 策略的演进和最佳实践,含springBoot 实现(Java版本)
java·spring boot·分布式
永日456703 小时前
学习日记-spring-day42-7.7
java·学习·spring
龙谷情Sinoam3 小时前
扩展若依@Excel注解,使其对字段的控制是否导出更加便捷
java
2401_891957313 小时前
list的一些特性(C++)
开发语言·c++