Mybatis【CRUD】

松安民
2023-12-01

3.CRUD

1.namespace

namespace中的包名要和Dao/mapper接口的包名一致

2.select

选择,查询语句;

id:就是对应的namespace中的方法名

resultType:Sql语句执行的返回值

1.编写接口

  //查询全部用户
    List<User> getUserList();
    //根据ID查询用户
    User getUserById(int id);

2.编写对应的Mapper中的sql语句

<select id="getUserList" resultType="com.jiang.pojo.User">
       select * from mybatis.user
   </select>

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

3.测试

 @Test
    public void test(){
        //第一步:获得SqlSession对象
        SqlSession sqlSesssion = MybatisUtils.getSqlSesssion();
        //执行SQL
        //方式一:getMapper
        UserMapper userDao = sqlSesssion.getMapper(UserMapper.class);
        List<User> userList = userDao.getUserList();

    /*
      //方式二:
        List<User> userList = sqlSesssion.selectList("com.jiang.dao.UserDao.getUserList");
*/
        for (User user:userList) {
            System.out.println(user);
        }

        //关闭SqlSession
        sqlSesssion.close();
    }

 @Test
    public void getUserById(){
        SqlSession sqlSesssion = MybatisUtils.getSqlSesssion();

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

        User user = mapper.getUserById(1);
        System.out.println(user);
        sqlSesssion.close();
    }

3.Insert

1.编写接口

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

2.编写SQL语句

parameterType :输入的属性  -----基本类型int可以忽略

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

3.测试

//增删改需要加事物
    @Test
    public void addUser(){
        SqlSession sqlSesssion = MybatisUtils.getSqlSesssion();

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

       int res= mapper.addUser(new User(4,"王五","123456"));
       if(res>0){
           System.out.println("插入成功");
       }

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

4.update

1.编写接口

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

2.编写SQL语句

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

3.测试

 @Test
    public void updateUser(){
        SqlSession sqlSesssion = MybatisUtils.getSqlSesssion();
        UserMapper mapper = sqlSesssion.getMapper(UserMapper.class);

        int res=mapper.updateUser(new User(4,"田七","123"));
        if(res>0){
            System.out.println("更新数据成功");
        }
        sqlSesssion.commit();
        sqlSesssion.close();
    }

5.delete

1.编写接口

 //删除一个用户
    int deleteUser(int id);

2.编写SQL语句

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

3.测试

@Test
    public void deleteUser(){
        SqlSession sqlSesssion = MybatisUtils.getSqlSesssion();
        UserMapper mapper = sqlSesssion.getMapper(UserMapper.class);
        int res=mapper.deleteUser(4);
        if(res>0){
            System.out.println("删除用户成功");
        }
        sqlSesssion.commit();
        sqlSesssion.close();
    }

注意:

增删改操作需要提交事务

6.分析错误

标签不要匹配错

resource绑定mapper,需要使用路径

程序配置文件必须符合规范

NullPointException,没有注册到资源

输出的XML文件中存在中文乱码问题

Maven资源没有导出问题

6.万能Map

1.UserMapper接口

//用万能Map插入用户
public void addUser2(Map<String,Object> map);

2.UserMapper.xml

<!--对象中的属性可以直接取出来 传递map的key-->
<insert id="addUser2" parameterType="map">
    insert into user (id,name,password) values (#{userid},#{username},#{userpassword})
</insert>

3.测试

    @Test
    public void test3(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userid",4);
        map.put("username","酱油11");
        map.put("userpassword",123456);
        mapper.addUser2(map);
        //提交事务
        sqlSession.commit();
        //关闭资源
        sqlSession.close();
    }

Map传递参数,直接在sql中取出key即可! 【parameter=“map”】

对象传递参数,直接在sql中取出对象的属性即可! 【parameter=“Object”】

只有一个基本类型参数的情况下,可以直接在sql中取到

多个参数用Map , 或者注解!

8.思考题

 模糊查询怎么写?

1.Java代码执行的时候,传递通配符% %

List<User> userList = mapper.getUserLike("%李%");

2.在sql拼接中使用通配符(注意防止SQL注入)

select * from user where name like "%"#{value}"%"

https://www.bilibili.com/video/BV1NE411Q7Nx?p=5

 类似资料: