MyBaits实现增删改查

本文介绍了一个使用 MyBatis 框架实现的增删改查(CRUD)操作示例,包括配置映射文件及具体的方法实现。通过示例展示了如何进行数据插入、更新、删除和查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:配置映射文件

<?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="StudentMapper" >
    <select id="selectAll" resultType="com.hanjie.bean.Student">
        select * from student;
    </select>

    <select id="selectById" resultType="com.hanjie.bean.Student" parameterType="java.lang.Integer">
        select * from student where sid = #{id}
    </select>

    <insert id="insert" parameterType="com.hanjie.bean.Student">
        insert into student values (#{sid},#{name},#{age},#{birthday})
    </insert>

    <update id="update" parameterType="com.hanjie.bean.Student">
        update student set name =#{name},age=#{age} where sid = #{sid}
    </update>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from student where sid = #{#sid}
    </delete>
    
</mapper>

二:使用

@Test
    public void delete() throws Exception{
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int res = sqlSession.update("StudentMapper.delete", 100);
        sqlSession.commit();//提交事务
        System.out.println(res);
        sqlSession.close();
        is.close();

    }
    @Test
    public void update() throws Exception{
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Student student = new Student(100,"hanjie",100,"2021-01-01");
        int res = sqlSession.update("StudentMapper.update", student);
        sqlSession.commit();//提交事务
        System.out.println(res);
        sqlSession.close();
        is.close();

    }
    @Test
    public void insert() throws Exception{
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Student student = new Student(100,"hanjie",26,"2021-01-01");
        int res = sqlSession.insert("StudentMapper.insert", student);
        sqlSession.commit();//提交事务
        System.out.println(res);
        sqlSession.close();
        is.close();

    }

    @Test
    public void selectById() throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Student student = sqlSession.selectOne("StudentMapper.selectById", 3);
        System.out.println(student);
        sqlSession.close();
        is.close();
    }

    @Test
    public void selectAll() throws Exception{
        //1.加载配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        //2.获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.通过SqlSession工厂对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.执行配置文件中sql语句,并接受结果
        List<Student> students = sqlSession.selectList("StudentMapper.selectAll");
        //5.处理结果集
        for (Student student:students) {
            System.out.println(student);
        }

        //6.释放资源
        sqlSession.close();
        is.close();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值