Mybatis持久层框架 | CRUD

社区

CRUD

CRUD是指在做计算处理时的增加(Create)、读取(Read)、更新(Update)和删除(Delete):主要被用在描述软件系统中数据库或者持久层的基本操作功能

环境准备

  • 环境目录

  • UserMapper接口(对接数据库中的数据)
package com.wei.dao;

import com.wei.pojo.User;

import java.util.List;

public interface UserMapper {

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

  • UserMapper.xnl(Mapper中编写sql语句)
<?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.wei.dao.UserMapper">
    
<!--select查询语句查询全部用户-->    
    <select id="getUserById" parameterType="int" resultType="com.wei.pojo.User">
        select * from mybatis.user where id = #{id}
    </select>
    
</mapper>
  • UserDaoTest(测试类)
package com.wei.dao;

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

import java.util.List;

public class UserDaoTest {

    @Test
    public void getUserById(){
        //获取执行sql对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        //获得接口UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用1号用户
        User user = mapper.getUserById(1);
        System.out.println(user);


        //关闭SqlSession,避免造成内存出现问题
        sqlSession.close();
    }
    
}
  • namespace:namespace中的包名要和接口的包名一致
  • id:就是对应的UserMapper接口中的方法名
  • resultType:Sql语句执行的返回值
  • parameterMap:参数类型
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.wei.dao.UserMapper">
    
<!--select查询语句查询全部用户-->    
    <select id="getUserById" parameterType="int" resultType="com.wei.pojo.User">
        select * from mybatis.user where id = #{id}		// #{id} : 获取UserMapper接口 中 User getUserById(int id); 的id
    </select>
    
</mapper>

Tip:增删改需要提交事物 ( sqlSession.commit(); )

select

  • UserMapper.xnl中的查询语句
<!--select查询语句-->
    <select id="getUserList" resultType="com.wei.pojo.User">
        select * from mybatis.user
    </select>
  • UserMapper接口
//获取全部用户
    List<User> getUserList();
  • UserDaoTest(测试类)
    @Test
    public void test(){
        //获取执行sql对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        //获得接口UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //通过增强for循环遍历数组
        List<User> userList = mapper.getUserList();
        for (User user :userList){
            System.out.println(user);
        }

        //关闭SqlSession,避免造成内存出现问题
        sqlSession.close();
    }

insert

  • UserMapper.xnl中的查询语句
    <!--insert查询语句-->
    <!--User对象中的属性可以直接取-->
    <insert id="addUser" parameterType="com.wei.pojo.User">
        insert into mybatis.user(id, name, pwd) values (#{id}, #{name}, #{pwd});
    </insert>
  • UserMapper接口
    //插入一个用户inserst
    int addUser(User user);
  • UserDaoTest(测试类)
    //根据id插入数据
    @Test
    public void addUser(){

        //获取执行sql对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        //获得接口UserMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        int qwe = mapper.addUser(new User(5, "qwe", "123456"));
        if (qwe>0){
            System.out.println("插入成功!");
        }
        //提交事物
        sqlSession.commit();

        //关闭SqlSession,避免造成内存出现问题
        sqlSession.close();
    }

update

  • UserMapper.xnl中的查询语句
    <!--更新数据-->
    <update id="updateUser" parameterType="com.wei.pojo.User">
        update mybatis.user set name = #{name},pwd  = #{pwd} where id = #{id};
    </update>
  • UserMapper接口
    //更新数据
    int updateUser(User user);
  • UserDaoTest(测试类)
    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUser(new User(5, "呵呵", "123"));

        sqlSession.commit();
        sqlSession.close();
    }

delete

  • UserMapper.xnl中的查询语句
    <!--删除用户-->
    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id = #{id};
    </delete>
  • UserMapper接口
    //删除用户
    int deleteUser(int id);
  • UserDaoTest(测试类)
    @Test
    public void deleteUser(){

        SqlSession sqlSession = MybatisUtils.getSqlSession();

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

        sqlSession.commit();
        sqlSession.close();

    }

resultType和parameterType 区别

  • resultType :主要针对于从数据库中提取相应的数据出来 如:select
  • parameterType :主要针对于将信息存入到数据库中 如:insert 、update、alter
0
0
0
0
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论