mybatis学习-02-增删改查

2022/2/1 23:11:18

本文主要是介绍mybatis学习-02-增删改查,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

总流程:
1.编写接口
2.编写对应的mapper中的sql语句
3.测试
1. namespace
namespace中的包名要和Dao/Mapper中的接口保持一致!
2. select
选择,查询语句:

  • id:就是对应的namespace中的方法名;
  • resulType:sql语句执行的返回值!
  • parameterType:参数类型
    注意增删改需要提交事务!

userMapper.java接口:

package com.zuo.dao;

import com.zuo.pojo.User;

import java.util.List;

/**
 * @description
 */
public interface UserMapper {
    //查询全部用户
    List<User> getUserList();
    //根据id查用户
    User getUserById(int id);
    //insert一个用户
    int addUser(User user);
    //修改用户
    int updateUser(User user);
    //删除一个用户
    int deleteUser(int id);
}

userMapper.xml接口对应的mapper

<?xml version="1.0" encoding="UTF8" ?>
        <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zuo.dao.UserMapper">
        <select id="getUserList" resultType="com.zuo.pojo.User">select * from mybatis.user</select>
        <select id="getUserById" parameterType="int" resultType="com.zuo.pojo.User">select * from user where id = #{id}</select>
        <insert id="addUser" parameterType="com.zuo.pojo.User">insert into user (id,name,pwd) values (#{id},#{name},#{pwd})</insert>
        <update id="updateUser" parameterType="com.zuo.pojo.User">update user set name=#{name},pwd=#{pwd} where id= #{id}</update>
        <delete id="deleteUser" parameterType="int">delete from user where id=#{id}</delete>
</mapper>


UserMapperTest.java测试:

package com.zuo.dao;

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

import java.util.List;

/**
 * @description
 */
public class UserMapperTest {
    @Test
    public void test(){
        //第一步:获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //方式一:getMapper执行sql
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getUserList();
        for(User user: userList){
            System.out.println(user);
        }
        //关闭sqlSession
        sqlSession.close();
    }
    @Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.getUserById(1);
        System.out.println(user);
        sqlSession.close();
    }
    @Test
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int res = userMapper.addUser(new User(10, "afa", "123"));
        if(res>0) {
            System.out.println("插入成功!");
        }
        //提交事务
        sqlSession.commit();//提交事务
        sqlSession.close();
    }
    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int res = mapper.updateUser(new User(10, "haha", "12345"));
        if(res>0) {
            System.out.println("修改成功!");
        }
        sqlSession.commit();//提交事务
        sqlSession.close();
    }
    @Test
    public void deleteUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(10);
        sqlSession.commit();//提交事务
        sqlSession.close();
    }
}



这篇关于mybatis学习-02-增删改查的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程