Mybatis增删改查

增删改查基本上就是一个操作,一模一样的,首先是UserMpper接口:

请添加图片描述

package com.ftz.Demo.dao;

import com.ftz.Demo.po.User;

import java.util.List;

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

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


    /**
     * 增加用户
     */
    int addUser(User user);

    /**
     * 修改用户
     */

    int updateUser(User user);

    /**
     * 删除用户
     */
    int deleteUser(int id);





}

然后就是对应的UserMapper.xml:

请添加图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ftz.Demo.dao.UserMapper">
    <select id="getUserList" resultType="com.ftz.Demo.po.User">
        select * from mybatis.user
    </select>

    <select id="getUserById" parameterType="int"  resultType="com.ftz.Demo.po.User">
        select  * from mybatis.user where id = #{id}
    </select>

   <insert id="addUser" parameterType="com.ftz.Demo.po.User" >
        insert  into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd});
   </insert>

    <update id="updateUser" parameterType="com.ftz.Demo.po.User">
        update mybatis.user  set name = #{name},pwd=#{pwd} where id = #{id};
    </update>

    <delete id="deleteUser" parameterType="int">
        delete  from mybatis.user where id = #{id};
    </delete>
</mapper>

接下来写测试,由于我们这都写在了一个Mapper里面所以配置文件不用动,直接写测试:

测试有三部是固定的,

  • 1: SqlSession sqlSession = MybatisUtil.getSqlSession();

  • 2: UserMapper mapper = sqlSession.getMapper(UserMapper.class);

  • 3: sqlSession.close();

然后其他的各自找对应的方法传参,记住增删改是需要多一个提交事务才可以生效: sqlSession.commit();

请添加图片描述

package com.ftz.Demo.dao;

import com.ftz.Demo.po.User;
import com.ftz.Demo.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserDaoTest {
    @Test
    public void test() {

       SqlSession sqlSession =  MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();

        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test
    public  void  getUserById(){
        SqlSession sqlSession =  MybatisUtil.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User userById = mapper.getUserById(1);
        System.out.println(userById);


        sqlSession.close();

    }

    /**
     * 增删改去要提交事务
     */
    @Test
    public  void  addUser(){
        SqlSession sqlSession =  MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int  number = mapper.addUser(new User(4, "邓家佳", "123321"));

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

        sqlSession.close();


    }
    
    @Test
    public  void  updateUser(){
        SqlSession sqlSession =  MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUser(new User(4, "笑嘻嘻", "12332112"));

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

        sqlSession.close();
        
        
    }

    @Test
    public  void  deleteUser(){
        SqlSession sqlSession =  MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(4);
        //提交事务
        sqlSession.commit();

        sqlSession.close();




    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

water-之

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值