MyBits-mapper动态代理方式(接口方式)的CRUD

原则:约定优于配置。

  1. 硬编码方式:*.java
    Configuration conf = new Configuration();
    conf.setName("myProject");
    
  2. 配置方式:*.xml
    <name>myProject</name>
    
  3. 约定方式:默认值name为myProject
实现步骤

约定的目标:省略掉statement,即根据约定可以直接定位出SQL语句。

  1. 新建一个接口,用于操作MyBits,接口中的方法必须遵循以下约定:
    • 方法名和mapper.xml文件中标签的id值一样
    • 方法的输入参数与mapper.xml文件中标签的parameterType类型一致
    • 方法的返回值和mapper.xml文件中标签的resultType类型一致
  2. 除了以上约定,要实现接口中方法和Mapper.xml中SQL标签----对应,还需要以下1点:
    • namespace的值,就是接口的全类名(接口-----------mapper.xml一一对应)
  3. 匹配(约定)的过程:
    • 根据接口名找到mapper.xml文件(根据的是namespace=接口全类名)
    • 根据接口的方法名找到mapper.xml文件中的SQL标签(方法名=SQL标签的ID值)
    • 以上两点可以保证:当我们掉用接口时,程序能自动定位到某一个Mapper.xml文件中的SQL标签

习惯中将mapper.xml文件和接口放在同一个包下
StudentMapper.xml文件:

<?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="com.gub.mapper.StudentMapper">
    <select id="queryStudentById" resultType="com.gub.vo.Student" parameterType="int">
        SELECT * FROM student WHERE sid = #{sid}
    </select>
    <insert id="insertStudent" parameterType="com.gub.vo.Student" >
        INSERT INTO student (name,age,graname) VALUES (#{name},#{age},#{graname});
    </insert>
    <delete id="deleteStudentById" parameterType="int">
        DELETE FROM student WHERE sid=#{sid}
    </delete>
    <update id="updateStudentById" parameterType="com.gub.vo.Student" >
        UPDATE student SET name=#{name},age=#{age},graname=#{graname} WHERE sid=#{sid}
    </update>
    <select id="querySelectAll" resultType="com.gub.vo.Student">
        SELECT * FROM Student
    </select>
</mapper>

StudentMapper接口:

package com.gub.mapper;

import com.gub.vo.Student;

import java.util.List;

//操作MyBits的接口
public interface StudentMapper {
    public Student queryStudentById(int id);
    public void insertStudent(Student student);
    public void deleteStudentById(int sid);
    public void updateStudentById(Student student);
    public List<Student> querySelectAll();
}

测试根据ID查询:

package com.gub.test;

import com.gub.mapper.StudentMapper;
import com.gub.vo.Student;
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 java.io.IOException;
import java.io.Reader;

public class TestStudent {
    public static void main(String[] args) throws IOException {
        Reader reader = Resources.getResourceAsReader("conf.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //取得mapper接口
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        //根据ID查询
        Student student = studentMapper.queryStudentById(1);
        System.out.println(student);
        sqlSession.commit();
        sqlSession.close();
    }
}

测试查询全部:

	//取得mapper接口
	 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
	 //查询全部数据
	 List<Student> students = studentMapper.querySelectAll();

测试增加:

	Student student = new Student();
	student.setName("hasaka");
	student.setAge(18);
	student.setGraname("six six six");
	StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
	//增加数据
	studentMapper.insertStudent(student);
	sqlSession.commit();

测试修改:

//取得mapper接口
	Student student = new Student();
	student.setSid(5);
	student.setName("CN");
	student.setAge(18);
	student.setGraname("six six six");
	StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
	//修改数据
	studentMapper.updateStudentById(student);
	sqlSession.commit();

测试删除:

	StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
	//删除数据
	studentMapper.deleteStudentById(5);
	sqlSession.commit();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值