Mybatis - 基础(增加、更新、条件查询)


一、增加(@Insert)

1、概念

Mybatis的增加是使用java中的注解来完成增加,增加的过程中会用到的注解为:@Insert、@Options

2、语法

在@Mapper接口中实现SQL语句的语法为:

//    增加
    @Options(useGeneratedKeys = true,keyProperty = "字段名")
    //进行返回keyProperty所指定的字段值
    @Insert("sql语句(insert)")//在里面实现增加(insert)的sql语句,在增加时可以使用预编译SQL
    public void insertEmp(Emp emp);//当添加多个字段值时可以直接使用实体类进行传值

主键返回注解:
@Options(useGeneratedKeys = true,keyProperty = “字段名”)
此注解的作用是:当你增加完之后想让他返回一个主键的值可以使用注解来完成,
注意:在你想要输出此字段的值是可以使用此接口的实现类的get方法。

3、实例

接口(Mapper)实现SQL语句的代码块:

//    增加
    @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},#{depId},#{createTime},#{updateTime})")
    public void insertEmp(Emp emp);

测试(Test)代码块

//	映射EmpMapper接口
	@Autowired
    private EmpMapper userMapper;
//    增加测试
    @Test
    public void testInsertEmp(){
        Emp testEmp = new Emp();

        testEmp.setUserName("zxyy");
        testEmp.setName("宙月阳two");
        testEmp.setGender((short) 1);
        testEmp.setImage("2.png");
        testEmp.setJob((short) 3);
        testEmp.setEntryDate(LocalDate.of(1999,10,1));
        testEmp.setDepId(3);
        testEmp.setUpdateTime(LocalDateTime.now());
        testEmp.setCreateTime(LocalDateTime.now());

        userMapper.insertEmp(testEmp);
        System.out.println(testEmp.getId());

    }

Emp实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
    private Integer id,depId;
    private String userName,password,name,image;
    private short gender,job;
    private LocalDate entryDate;
    private LocalDateTime createTime,updateTime;
}

二、更新(@Update)

1、概念

更新代码与上面的添加代码很相似,只不过是其中的SQL语句发生了改变,其中用到的注解为:@Update

2、语法

在@Mapper接口中实现SQL语句的语法为:

//    更新操作
    @Update("SQL语句(update)")
    public void updateEmp(Emp emp);

3、实例

在@Mapper接口中的代码块为:

//    更新操作
    @Update("update emp set username=#{userName},name = #{name},gender=#{gender},image= #{image},dept_id=#{depId},update_time=now() where id = #{id};")
    public void updateEmp(Emp emp);

测试(Test)代码块

//	映射EmpMapper接口
	@Autowired
    private EmpMapper userMapper;
//    更新测试
    @Test
    public void testUpdatetEmp(){
        Emp testEmp = new Emp();

        testEmp.setId(1);
        testEmp.setUserName("zyy2");
        testEmp.setName("宙月阳");
        testEmp.setGender((short) 1);
        testEmp.setImage("2.png");
        testEmp.setJob((short) 3);
        testEmp.setEntryDate(LocalDate.of(1999,10,1));
        testEmp.setDepId(3);
        testEmp.setUpdateTime(LocalDateTime.now());
        userMapper.updateEmp(testEmp);

    }

Emp实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
    private Integer id,depId;
    private String userName,password,name,image;
    private short gender,job;
    private LocalDate entryDate;
    private LocalDateTime createTime,updateTime;
}

三、查询(@Select)

1、概念

Mybatis的增加是使用java中的注解来完成查询,查询的过程中会用到的注解为:@Select

2、语法

在@Mapper接口中实现SQL语句的语法为:

//   条件查询
    @Select("select * from emp where gender = #{gender} and entrydate between #{start} and #{end} and name like(concat('%',#{name},'%'))")
    public List<Emp> ifSelectEmp(short gender, LocalDate start,LocalDate end,String name);

字符串拼接方法:concat(String a,String b),此方法是Mysql中的一个方法,在MySQL中可以使用:
在这里插入图片描述
为什么要使用concat方法,而不是直接使用预编译SQL(#{…}):
在上面代码中可以看到此方法是用在like条件查询中,需要使用“%”或“_”来作为占位符进行查询,可以是如过直接使用的话会发现就没有办法使用预编译了,所以在使用预编译之前要先使用concat方法进行字符串拼接就可以使用了:
在这里插入图片描述

3、实例

接口(Mapper)实现SQL语句的代码块:

//   条件查询
    @Select("select * from emp where gender = #{gender} and entrydate between #{start} and #{end} and name like(concat('%',#{name},'%'))")
    public List<Emp> ifSelectEmp(short gender, LocalDate start,LocalDate end,String name);

测试(Test)代码块

//	映射EmpMapper接口
	@Autowired
    private EmpMapper userMapper;
//    条件查询测试
    @Test
    public void testSelectEmp(){
        List listEmp = userMapper.ifSelectEmp((short) 1,LocalDate.of(2010,01,01),LocalDate.of(2020,01,01),"张");
        listEmp.stream().forEach(emp ->{
            System.out.println(emp);
        });
    }

Emp实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
    private Integer id,depId;
    private String userName,password,name,image;
    private short gender,job;
    private LocalDate entryDate;
    private LocalDateTime createTime,updateTime;
}


总结:整体来说Mybatis的增加、更新以及条件查询操作并不难,只要注意不要将属性名或者sql代码敲混了,还是很容易实现的。

资料来源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值