Mybatis CRUD操作

存在一张简单的数据表
在这里插入图片描述
mybatis对数据库进行增删改查操作步骤:

编写接口

public interface UserMapper {
   //查询全部用户
   List<User> getUserList();

   //根据id查询用户
   User getUserById(int id);

   //插入一个用户
   int addUser(User user);

   //修改用户
   int update(User user);

   //map修改用户
   int update2(HashMap<Object, Object> map);

   //删除用户
   int delete(int id);

}

实现接口的UserMapper.xml

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

       <!--namespace绑定一个对应的接口-->
<mapper namespace="com.tt.Dao.UserMapper">
     <!--id对应绑定接口的方法, resultType对应实体类-->
    <select id="getUserList" resultType="com.tt.pojo.User">
        select * from mybatis.user
    </select>

<!--        根据id查用户信息-->
    <select id="getUserById" parameterType="int" resultType="com.tt.pojo.User">
        select * from mybatis.user where id=#{id}
    </select>

<!--    添加一个用户-->
    <insert id="addUser" parameterType="com.tt.pojo.User">
        insert into mybatis.user (id,name,pwd) value (#{id},#{name },#{pwd})
    </insert>

<!--    修改用户-->
    <update id="update" parameterType="com.tt.pojo.User">
        update mybatis.user set name =#{name},pwd=#{pwd} where id=#{id}
    </update>

    <!--   map修改用户-->
    <update id="update2" parameterType="map">
        update mybatis.user set name =#{username},pwd=#{password} where id=#{userId}
    </update>


    <!--    删除用户-->
    <delete id="delete" parameterType="int">
        delete from mybatis.user where id=#{id}
    </delete>
</mapper>

test目录下编写测试类

public class UserMapperTest {
    @Test
  public void Test(){
      // 第一步,获取sqlSession对象
      SqlSession sqlSession = MybatisUtils.getSqlSession();
      //执行sql
      UserMapper userdao = sqlSession.getMapper(UserMapper.class);
      List<User> userList = userdao.getUserList();

      //第二步,遍历输出
      for (User user : userList) {
          System.out.println(user);
          System.out.println("-------------------------------");
      }
      //关闭sqlSession
      sqlSession.close();
  }

  //根据id查询
  @Test
    public void testgetUserById(){
      SqlSession sqlSession = MybatisUtils.getSqlSession();
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      User user = mapper.getUserById(1);
      System.out.println(user);
      sqlSession.close();
  }

  //插入用户
    @Test
    public void testAddUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addUser(new User(4, "乌拉", "4545454"));

        //事务提交
        sqlSession.commit();
        sqlSession.close();
    }

    //修改用户
    @Test
    public void testUpdate(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.update(new User(4,"嘎达","666666"));
        //事务提交
        sqlSession.commit();
        sqlSession.close();
    } //map修改用户
    @Test
    public void testUpdate2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("username","康师傅");
        map.put("password","111111");
        map.put("userId",3);
        mapper.update2(map);

        sqlSession.commit();
        sqlSession.close();
    }



    //修改用户
   @Test
    public void testDelete(){
       SqlSession sqlSession = MybatisUtils.getSqlSession();
       UserMapper mapper = sqlSession.getMapper(UserMapper.class);
       mapper.delete(4);
       //事务提交
       sqlSession.commit();
       sqlSession.close();
   }
}

注意:增删改需要提交事务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值