【Java闭关修炼】MyBatis-注解开发

常用注解开发的介绍

好处:不需要写xml映射配置文件,但是仍然需要在核心配置文件中配置映射配置文件,直接在mapper接口方法的上方指定需要的sql操作

在这里插入图片描述

注解实现查询的操作

在这里插入图片描述

  • 接口方法中使用注解
package com.itheima.bean.mapper1;
import com.itheima.bean.Student;
import org.apache.ibatis.annotations.Select;
import java.util.List;

public interface StudentMapper {

    // 查询全部操作  使用注解查询操作
    @Select("SELECT * FROM student")
    public abstract List<Student> selectAll();
}

  • 核心配置文件中配置映射关系
    
    <mappers>
        <package name="com.itheima.mapper1"/>
    </mappers>

  • 测试类
package com.itheima.dynamic;

import com.itheima.bean.Student;
import com.itheima.bean.mapper1.StudentMapper1;
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;
import java.util.List;

public class Test03 {


    @Test
    public void selectAll() throws IOException {
        // 加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        // 获取sqlSession工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);// 自动提交事务

        // 通过工厂对象获取sqlSession对象
        SqlSession sqlSession = build.openSession(true);

        // 获取StudentMapper接口的实现类对象
        StudentMapper1 mapper = sqlSession.getMapper(StudentMapper1.class);

        // 调用实现类对象中的方法  接受结果
        List<Student> students = mapper.selectAll();


        // 处理结果
        for (Student student : students) {
            System.out.println(student);
        }

        // 释放资源

        sqlSession.close();
        is.close();
    }
}

注解实现新增操作

  • 注解实现插入操作
 // 插入学生操作
    @Insert("INSERT INTO student VALUES (#{id},#{name},#{age})")
    public abstract Integer insert(Student stu);

  • 测试类
 @Test
    public void insert() throws IOException {
        // 加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        // 获取sqlSession工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);// 自动提交事务

        // 通过工厂对象获取sqlSession对象
        SqlSession sqlSession = build.openSession(true);

        // 获取StudentMapper接口的实现类对象
        StudentMapper1 mapper = sqlSession.getMapper(StudentMapper1.class);


        Student stu = new Student(1, "xxx", 23);
        Integer insert = mapper.insert(stu);// 返回影响的行数

        System.out.println(insert);
        // 释放资源

        sqlSession.close();
        is.close();


    }

注解实现修改操作

  • 注解实现修改数据操作
    @Update("UPDATE student SET name =#{name},age = #{age} WHERE id = #{id}")
    public abstract Integer update(Student stu);

  • 测试方法
@Test
    public void update() throws IOException {
        // 加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        // 获取sqlSession工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);// 自动提交事务

        // 通过工厂对象获取sqlSession对象
        SqlSession sqlSession = build.openSession(true);

        // 获取StudentMapper接口的实现类对象
        StudentMapper1 mapper = sqlSession.getMapper(StudentMapper1.class);


        Student stu = new Student(1, "xxx", 23);
        Integer result = mapper.update(stu);// 返回影响的行数  修改数据

        System.out.println(result);
        // 释放资源

        sqlSession.close();
        is.close();


    }

注解实现删除操作

  • 注解实现删除操作
    // 删除操作
    @Delete("DELETE FROM student WHERE id = #{id}")
    public abstract Integer delete(Integer id);

  • 测试类

    public void delete() throws IOException {
        // 加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        // 获取sqlSession工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);// 自动提交事务

        // 通过工厂对象获取sqlSession对象
        SqlSession sqlSession = build.openSession(true);

        // 获取StudentMapper接口的实现类对象
        StudentMapper1 mapper = sqlSession.getMapper(StudentMapper1.class);

        Integer result = mapper.delete(4);

        System.out.println(result);
        // 释放资源

        sqlSession.close();
        is.close();
    }


总结

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少写代码少看论文多多睡觉

求打赏,求关注,求点赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值