自学内容网 自学内容网

B站狂神说+mybatis增删改查操作

1.新增mapper接口

package com.aloha.dao;

import com.aloha.pojo.User;

import java.util.List;

public interface UserMapper {
    // 查询全部用户
    List<User> getUserList();
    // 根据ID查询用户
    User getUserById(int id);
    // 增加用户
    int addUser(User user);
    // 修改用户
    int updateUser(User user);
    // 删除用户
    int deleteUser(int id);
}

2.修改mapper接口同级目录下的xml文件(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">
<!--namespace=绑定一个对于的Dao/Mapper接口-->
<mapper namespace="com.aloha.dao.UserMapper">
    <!--select查询语句-->
    <select id="getUserList" resultType="com.aloha.pojo.User">
        select * from mybatis.user;
    </select>
    <select id="getUserById" parameterType="int" resultType="com.aloha.pojo.User">
        select * from mybatis.user where id = #{id}
    </select>

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

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

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

3.编写src同级别目录下的test文件

在这里插入图片描述

package com.aloha.dao;

import com.aloha.pojo.User;
import com.aloha.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserDaoTest {
    @Test
    public void test() {
        // 1. 获取SqlSessionFactory对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        // 2. 执行sql
        // 方式一: getMapper
        try {
            UserMapper userdao = sqlSession.getMapper(UserMapper.class);
            List<User> userList = userdao.getUserList();

            for (User user : userList) {
                System.out.println(user);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            // 3. 关闭SqlSession
            sqlSession.close();
        }
    }

    @Test
    public void test2() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        try {
            UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
            User user = usermapper.getUserById(1);

            System.out.println(user);
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            // 3. 关闭SqlSession
            sqlSession.close();
        }
    }

    @Test
    public void test3() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int res = mapper.addUser(new User(4, "周杰伦", "12333"));
        if(res > 0) {
            System.out.println("插入成功");
        }
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test4() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int res = mapper.updateUser(new User(1, "无敌", "12333"));
        if(res > 0) {
            System.out.println("修改成功");
        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test5() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.deleteUser(4);
        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();
    }
}

4.万能的Map

使用场景:比如插入一个对象,不想填所有的方法

UserMapper

// 万能的Mapper
    int addUser2(Map<String, Object> map);

UserMapper.xml

    <insert id="addUser2" parameterType="map">
        insert into mybatis.user (id, pwd) values (#{userId},#{passWord});
    </insert>

测试方法

    @Test
    public void test6() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", 4);
        map.put("passWord", "222333");
        mapper.addUser2(map);

        sqlSession.close();
    }

原文地址:https://blog.csdn.net/qq_61033357/article/details/143434321

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!