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);
    }
}
相关推荐
2401_cf3 小时前
为什么hadoop不用Java的序列化?
java·hadoop·eclipse
帮帮志3 小时前
idea整合maven环境配置
java·maven·intellij-idea
belldeep3 小时前
如何阅读、学习 Tcc (Tiny C Compiler) 源代码?如何解析 Tcc 源代码?
c语言·开发语言
LuckyTHP3 小时前
java 使用zxing生成条形码(可自定义文字位置、边框样式)
java·开发语言·python
无声旅者6 小时前
深度解析 IDEA 集成 Continue 插件:提升开发效率的全流程指南
java·ide·ai·intellij-idea·ai编程·continue·openapi
Blossom.1186 小时前
使用Python实现简单的人工智能聊天机器人
开发语言·人工智能·python·低代码·数据挖掘·机器人·云计算
da-peng-song6 小时前
ArcGIS Desktop使用入门(二)常用工具条——数据框工具(旋转视图)
开发语言·javascript·arcgis
galaxy_strive6 小时前
qtc++ qdebug日志生成
开发语言·c++·qt
Ryan-Joee6 小时前
Spring Boot三层架构设计模式
java·spring boot
TNTLWT6 小时前
Qt功能区:简介与安装
开发语言·qt