【Mybatis 源码分析 2】基于Mybatis使用mapper.xml实现CRUD操作

目录

1. mapper.xml映射文件中添加增删查改的sql语句

2. pom.xml中添加测试用的依赖包

3. 通过SqlSession进行增删改查

【Mybatis 源码分析 1】使用maven配置mybatis,之后,我们就可以开始增删查改数据库了。

1. mapper.xml映射文件中添加增删查改的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">
<mapper namespace="mybatis-mapper">

    <!--在manage表中通过id查找记录 -->
    <select id="selectById" resultType="Entity.User">
        select * from manage where id = #{id}
    </select>

    <!--在manage表中通过name查找记录 -->
    <select id="selectByName" resultType="Entity.User">
        select * from manage where name = #{name}
    </select>

    <!--在manage表中查找所有记录 -->
    <select id="selectAll" resultType="Entity.User">
        select * from manage
    </select>

    <!--在manage表中插入数据 -->
    <insert id="insertUser">
        insert manage(id,name) values(#{id}, #{name})
    </insert>

    <!--修改manage表中指定id的name -->
    <update id="updateUserById">
        update manage set name=#{name} where id=#{id}
    </update>

    <!--删除manage表中指定id的记录 -->
    <delete id="deleteUserById">
        delete from manage where id = #{id}
    </delete>

</mapper>

2. pom.xml中添加测试用的依赖包

        <!-- 测试用的依赖包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

3. 通过SqlSession进行增删改查

package Service;

import Entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class UserService {

    @Test
    public void test() throws IOException {

        String resource= "mybatis-conf.xml";
        //以流的方式获取recource(mybatis的环境配置文件)
        InputStream inputStream= Resources.getResourceAsStream(resource);

        //创建会话工厂
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //通过工厂得到SqlSession
        SqlSession sqlSession= sqlSessionFactory.openSession();
        //通过SqlSession操作数据库

        User newUser = new User();
        newUser.setId(8);
        newUser.setName("p7");
        sqlSession.insert("insertUser", newUser);

        System.out.println(sqlSession.selectList("selectAll"));

        newUser.setName("p8");
        sqlSession.update("updateUserById", newUser);

        System.out.println(sqlSession.selectList("selectAll"));

        sqlSession.delete("deleteUserById", 8);

        System.out.println(sqlSession.selectList("selectAll"));


        //提交事务,数据表中的数据才会被修改
        sqlSession.commit();

        //释放资源
        sqlSession.close();
    }


}

SqlSession是MyBatis的关键对象,是执行持久化操作的独享,类似于JDBC中的Connection.它是应用程序与持久层之间执行交互操作的一个单线程对象,也是MyBatis执行持久化操作的关键对象.SqlSession对象完全包含以数据库为背景的所有执行SQL操作的方法,它的底层封装了JDBC连接,可以用SqlSession实例来直接执行被映射的SQL语句.

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值