insert into tr" />

小言_互联网的博客

Mybatis(4)之CRUD

335人阅读  评论(0)

首先是 增 ,我们要在数据库中增加一个数据

先来看看我们之前的插入语句


  
  1. <insert id = "insertRole" >
  2. insert into try(id,name,age) values( 3, 'nuonuo', 20)
  3. < /insert >

请注意,我们这里的  insert 是固定的,但在实际的业务场景中,我们需要通过输入不同的代码,传递给数据库,意思就是  数据是不固定的。在

jdbc中,我们通过  ?  占位符可以实现传递  ,那么在 ,mybatis中我们可以使用 #{}来实现站位的功能,

同时,mybatis优于jdbc的点就是因为rom映射,因此我们必须将#{}中填写对应的字段来实现匹配。

改造后的insert语句如下


  
  1. <insert id = "insertRole" >
  2. insert into try(id,name,age) values(#{id},#{name},#{age})
  3. < /insert >

请看一下我们的pojo类


  
  1. package com.chenchen.pojo;
  2. public class role {
  3. private Integer id;
  4. private String name;
  5. private Integer age;
  6. public role(Integer id, String name, Integer age) {
  7. this.id = id;
  8. this.name = name;
  9. this.age = age;
  10. }
  11. public role() {
  12. }
  13. public Integer getId() {
  14. return id;
  15. }
  16. public void setId(Integer id) {
  17. this.id = id;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName( String name) {
  23. this.name = name;
  24. }
  25. public Integer getAge() {
  26. return age;
  27. }
  28. public void setAge(Integer age) {
  29. this.age = age;
  30. }
  31. @ Override
  32. public String toString() {
  33. return "role{" +
  34. "id=" + id +
  35. ", name='" + name + '\' ' +
  36. ", age=" + age +
  37. '} ';
  38. }
  39. }

首先我们来解释一下运行原理:

程序调用 insertRole这个id的插入语句,就会自动匹配 pojo的属性类 与字段,然后传入数据库,中中间会使用 get方法获取属性值,因此务必  字段名与属性名一致

我们来写个类测试一下

首先我们需要创建一个 role对象


  
  1. @ Test
  2. public void insertTest(){
  3. / / 首先我们需要创建一个role对象,这样才能实现映射
  4. role role = new role( 5, "可爱的绘梨衣", 18);
  5. sqlSession.insert( "insertRole",role);
  6. sqlSession. commit();
  7. sqlSession. close();
  8. }

这样就可以了,并且实现映射

同时我们可以使用map集合来做,不在记录

然后是删除,那么我们就需要一个delete语句在mapper中


  
  1. < delete id = "deleteRole" >
  2. delete from try where id = #{id}
  3. < / delete >
public  void  deleteRole(){
    System.out.println("删除的数量"+sqlSession.delete("deleteRole", 1));
    sqlSession.commit();
    sqlSession.close();
}

请注意 这里的 id 参数只有一个,所以可以任意的写匹配符号

然后是修改语句 ,如上 请完成mapper的补充


  
  1. <update id = "updateRole" >
  2. update try set name = #{name} where id = #{id}
  3. < /update >

  
  1. public void upd(){
  2. role role = new role( 2, "莫山山", 18);
  3. sqlSession.update( "updateRole",role);
  4. sqlSession. commit();
  5. }

请注意,这里我们更新数据库中的数据只有两种方式,一种是传入一个参数,一种是传入一个完整的对象,但是没关系,即使我们传入一个完整的对象,后面的update语句也会根据get方法获取到对应的数据的。

然后是查找

查找分为查一个和查多个

首先是查一个


  
  1. < select id = "selectRole" resultType = "com.chenchen.pojo.role" >
  2. select * from try where id = #{id}
  3. < / select >

  
  1. public void select(){
  2. role role = sqlSession.selectOne( "selectRole", 5);
  3. System.out.println(role);
  4. sqlSession. commit();
  5. sqlSession. close();
  6. }

请注意,从数据库中获得的对象一定是一个数据集,但是mybatis需要将数据集转换为对应的pojo实例,所以我们必须在select中确定role的类型

然后是查所有

public  void  selectAll(){
      List<role>  list =  sqlSession.selectList("selectAll",role.class);
       System.out.println(list);
}
<select id="selectAll"  resultType="com.chenchen.pojo.role">
    select * from try
</select>

请注意,这个时候返回的结果集是一个数组,用数组来承接形成后的对象真的很方便

这就是全过程

我们来总结一下

首先使用 #{id}的占用符在XXmapper.xml中编写对应的 crud语句,如果是查询语句请设置好类型,然后写好id 在程序中拿到id,创建好对象,然后调用对应的方法就可以

我们再来说说namespace的作用

namespace可以区分不同的配置环境

 

 

 


转载:https://blog.csdn.net/m0_58565716/article/details/129102293
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场