美文网首页
Mybatis入门实现CURD

Mybatis入门实现CURD

作者: zzqsmile | 来源:发表于2021-10-14 14:52 被阅读0次

    Mapper字段笔记

    • namespace

    namespace的包名要和UserMapper/UserDao接口报名一致

    • id

    id就是namespace中对应的方法名

    • resultType

    resultType是SQL语句返回值

    • parameterType

    参数类型

    编写步骤

      1. 编写接口
        UserMapper/UserDao:
    package com.zzqsmile.dao;
    
    import com.zzqsmile.pojo.User;
    
    import java.util.List;
    
    public interface UserMapper {
        //查询全部用户
        List<User> getUserList();
    
        //根据ID查询用户
        User getUserByID(int id);
    
        //增加用户
        int userAdd(User user);
    
        //修改用户
        int userUpd(User user);
    
        //删除用户
        int userDel(int id);
    }
    
      1. 编写对应的Mapper中的sql语句
        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.zzqsmile.dao.UserMapper">
        <!--select查询-->
        <select id="getUserList" resultType="com.zzqsmile.pojo.User">
            select * from mybatis.user
        </select>
        
        <select id="getUserByID" resultType="com.zzqsmile.pojo.User" parameterType="int">
            select * from mybatis.user where id = #{id}
        </select>
    
        <insert id="userAdd" parameterType="com.zzqsmile.pojo.User">
            insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd})
        </insert>
    
        <update id="userUpd" parameterType="com.zzqsmile.pojo.User">
            update mybatis.user set name = #{name},pwd = #{pwd} where id = #{id}
        </update>
    
        <delete id="userDel" parameterType="int">
            delete from mybatis.user where id = #{id}
        </delete>
    
    </mapper>
    
      1. 测试
    package com.zzqsmile.dao;
    
    import com.zzqsmile.pojo.User;
    import com.zzqsmile.utils.MybatisUtils;
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;
    
    import java.util.List;
    
    public class UserDaoTest {
    
        @Test
        public void Test(){
            //第一步:获取SqlSesson对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
    
            try {
                //第二步:执行SQL
                //方式1:getMapper推荐使用
                UserMapper mapper = sqlSession.getMapper(UserMapper.class);
                List<User> userList = mapper.getUserList();
    
                //方式2:
                //List<User> userList = sqlSession.selectList("com.zzqsmile.dao.UserDao.getUserList");
    
                for (User user : userList) {
                    System.out.println(user);
                }
    
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
                //关闭sqlSession
                sqlSession.close();
            }
        }
    
    
        @Test
        public void getUserById(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    
            User userByID = mapper.getUserByID(1);
            System.out.println(userByID);
        }
    
        //增删改需要提交事物
        @Test
        public void addUser(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    
            int res = mapper.userAdd(new User(4, "zhangsan", "111111"));
            if (res>0){
                System.out.println("添加成功");
            }
    
            //提交事务
            sqlSession.commit();
            sqlSession.close();
        }
    
        @Test
        public void userUpd(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    
            int res1 = mapper.userUpd(new User(4, "zhangsan", "123123"));
            if (res1>0){
                System.out.println("修改成功");
            }
    
            //提交事务
            sqlSession.commit();
            sqlSession.close();
        }
    
        @Test
        public void userDel(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    
            int res2 = mapper.userDel(4);
            if (res2>0){
                System.out.println("删除成功");
            }
    
            //提交事务
            sqlSession.commit();
            sqlSession.close();
        }
    }
    
    

    注意,增删改需要提交事物才能生效

    万能Map

    • UserMapper:
    //万能Map
        int userAdd2(Map<String,Object> map);
    
    • UserMapper.xml
        <insert id="userAdd2" parameterType="map">
            insert into mybatis.user (id,pwd) values (#{id},#{userpwd})
        </insert>
    
    • UserDaoTest
        @Test
        public void addUser2(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    
            HashMap<String, Object> stringObjectHashMap = new HashMap<String, Object>();
            stringObjectHashMap.put("id",5);
            stringObjectHashMap.put("userpwd","222222");
    
            mapper.userAdd2(stringObjectHashMap);
    
            //提交事务
            sqlSession.commit();
    
            sqlSession.close();
        }
    

    相关文章

      网友评论

          本文标题:Mybatis入门实现CURD

          本文链接:https://www.haomeiwen.com/subject/shquoltx.html