mybatis

新增操作

创建UserMapper类

package com.ming.mapper;
​
import com.ming.pojo.User;
import org.apache.ibatis.annotations.*;
​
import java.util.List;
​
@Mapper//运行时,会自动生成该类接口的实现类对象,并且将该接口交给IOC容器
public interface UserMapper {
    @Delete("delete from db02.tb_emp where id=#{id}")
    public int  delete(Integer id);
    @Options(useGeneratedKeys = true,keyProperty = "id")//返回id值
    @Insert("insert into db02.tb_emp(db02.tb_emp.username, db02.tb_emp.name, db02.tb_emp.gender, " +
            "db02.tb_emp.image, db02.tb_emp.job, db02.tb_emp.entrydate," +
            " db02.tb_emp.dept_id, db02.tb_emp.create_time, db02.tb_emp.update_time)"+"" +
            "values(#{username},#{name},#{gender},#{imge},#{job},#{entrydate},#{depId},#{createTime},#{updateTime} )")
    public void insert(User user);
}
​

创建User类

package com.ming.pojo;
​
import lombok.*;
​
import java.time.LocalDate;
import java.time.LocalDateTime;
​
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private short gender;
    private String imge;
    private short job;
    private LocalDate entrydate;
    private Integer depId;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
​
}
​

新建测试类

package com.ming;
​
import com.ming.mapper.UserMapper;
import com.ming.pojo.User;
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;
​
@SpringBootTest
class Springbootmybatis01ApplicationTests {
​
    @Autowired
    private UserMapper userMapper;
​
​
    @Test
    public void testinsert(){
        User user = new User();
        user.setUsername("zhangsan3");
        user.setName("张三3");
        user.setImge("1.jpg");
        user.setGender((short) 1);
        user.setJob((short) 1);
        user.setEntrydate(LocalDate.of(2024,5,14));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setId(1);
        userMapper.insert(user);
        System.out.println(user.getId());
    }
​
}
​

@Options(useGeneratedKeys = true,keyProperty = "id")可以返回id值

删除操作

    @Test
    public void testdelete(){
        int delete = userMapper.delete(16);
        System.out.println(delete);
    }

配置mybatis的日志 指定输出到控制台

# 配置mybatis的日志 指定输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

注意 #{}是?通配符 而${} 是直接将将数据进行拼接

更新操作

在UserMapper中添加更新操作

@Update("update tb_emp set username=#{username},name=#{name}," +
            "gender=#{gender},image=#{imge},job=#{job},entrydate=#{entrydate}, " +
            "dept_id=#{depId},update_time=#{updateTime} where id=#{id} ")
    public void update(User user);

在测试类中书写更新的实例

    @Test
    public void update(){
        User user = new User();
        user.setId(21);
        user.setUsername("zhangsan001");
        user.setName("张三001");
        user.setImge("21.jpg");
        user.setGender((short) 1);
        user.setJob((short) 1);
        user.setEntrydate(LocalDate.of(2024,5,14));
        user.setUpdateTime(LocalDateTime.now());
        user.setDepId(1);
        userMapper.update(user);
​
    }

查询

在UserMapper中添加查询操作

@Select("select * from tb_emp where id=#{id}")
    public User getById(Integer id);

在测试类中书写更新的实例

@Test
    public void testGetById(){
        User byId = userMapper.getById(21);
        System.out.println(byId);
    }

数据封装

实体类属性名和数据库查询返回值的字段名一致,mybatis会自动封装

如果实体类的属性名和数据库查询到返回的字段不一样,不能封装

三种封装方式

方案一:

方案二:

方案三:

在application.properties中配置

# 开启mybatis的驼峰命名自动开关 a_clom会自动封装到aClom中
mybatis.configuration.map-underscore-to-camel-case=true

通配符查询

使用函数concat('%',#{name},'%')可以在字符串中进行查询

 @Select("select * from tb_emp where name like concat('%',#{name},'%') and gender= #{gender} and entrydate between #{begin} and #{end} order by update_time desc")
    public List<User> selectlist(@Param("name") String name, @Param("gender") short gender, @Param("begin") LocalDate begin, @Param("end") LocalDate end);
}
在List集合中 要使用对于的@Param()进行匹配 否则会报错

在springboot1.x版本/单独使用mmybatis的时候需要加

@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中。

 @Test
    public void testList(){
        List<User> list = userMapper.selectlist("张", (short) 1,
                LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
        System.out.println(list);
    }

  • 11
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值