建立你的第一个Mybatis程序(二)| 青训营笔记

107 阅读3分钟

这是我参与「第三届青训营 -后端场」笔记创作活动的第3篇笔记

基本增删改查

以下都以一个实例来说明

实例结构如下:

数据库中表:

表名:Student ​ 字段:id(int),name(varchar),gender(int),major(varchar)

Student 类:

全路径:com.joe.Bean.Student

代码如下:

public class Student {
    private int id;
    private String name;
    private int gender;
    private String major;
    //除了这些属性之外还有这些属性的get和set方法
}

StudentMapper 接口:

全路径:com.joe.Mapper.StudentMapper

声明的增删改查方法:

增:insertStudent

删:deleteStudent

改:updateStudent

查:selectStudent

主函数:

创建了一个 SqlSession 对象

代码:

public class Main {
​
    public static void main(String[] args) throws IOException {
​
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession openSession = sqlSessionFactory.openSession();
        
        //定义openSession时这么写可以自动提交:
        SqlSession openSession = sqlSessionFactory.openSession(true);
        
        //后续还有其他的操作
}

一、增(INSERT)

1. 语法

在映射文件中

<!-- parameterType可以省略 -->
<insert id = "接口中增添记录的方法名"  parameterType = "传入的参数类型,一般写某个类">
    SQL语句
</insert>

2. 实例

(1) 一般的 insert

直接将某些值插入到表中,上文中已经有过实例

(2) 将某个对象的值通过 insert 传入表中

例如,在主函数中创建了一个 Student 对象,并设置了这个对象的属性:

//创建对象
Student stuJohn = new Student();
​
//设置属性
stuJohn.setId(6);
stuJohn.setName("John");
stuJohn.setGender(1);
stuJohn.setMajor("GIS");

现在通过传对象的方式将对象中的属性写入表中

首先接口中的方法声明如下:

public interface StudentMapper {
    //声明insert方法,insert方法的参数类型是Student
    public void insertStudent(Student student);
    
    //增删改的方法的返回值除了可以是void以外,也可以是另外的返回值,比如定义成以下形式
    //1. 可以知道增添记录是否成功
    public boolean insertStudent(Student student);
    //2. 可以获取增添的记录数
    public int insertStudent(Student student);
    
    //删和改都有类似的功能,但是xml文件中不能在增删改里面指定返回值的类型
}

方法实现需要在映射文件中实现:

<mapper namespace="com.joe.Mapper.StudentMapper">
    
    <!-- parameterType设定的是传入的参数类型,这个例子中指向了Student类的全路径,指传入的参数类型就是Student -->
    <insert id="insertStudent" parameterType="com.joe.Bean.Student">
        <!-- 前面的id,name...是表中的字段,后面的#{id},#{name}...是类Student中的属性 -->
        insert into student (id, name, gender, major) values (#{id}, #{name}, #{gender}, #{major});
    </insert>
    
</mapper>

在主函数中使用方法

//创建对象
Student stuJohn = new Student();
​
//设置属性
stuJohn.setId(6);
stuJohn.setName("John");
stuJohn.setGender(1);
stuJohn.setMajor("GIS");
​
try{
    
    StudentMapper studentMapper = openSession.getMapper(StudentMapper.class);
​
    //传入类Student的对象stuJohn,这个函数功能就是在表中增添一条记录
    studentMapper.insertStudent(stuJohn);
    //提交结果
    openSession.commit();
}
finally {
    openSession.close();
}

二、删(DELETE)

1. 语法

在映射文件中

<!-- parameterType可以省略 -->
<delete id = "接口中删除记录的方法名"  parameterType = "传入的参数类型,一般写某个类">
    SQL语句
</delete>

2. 实例

同增一样,可以传入普通的参数直接删除某些符合条件的记录

也可以传入某个类的对象,通过这个对象中的属性值再删除那些符合条件的记录

三、改(UPDATE)

1. 语法

在映射文件中

<!-- parameterType可以省略 -->
<update id = "接口中修改记录的方法名"  parameterType = "传入的参数类型,一般写某个类">
    SQL语句
</update>

2. 实例

同增一样,可以传入普通的参数直接修改某些符合条件的记录

也可以传入某个类的对象,通过这个对象中的属性值再修改那些符合条件的记录

四、查(SELECT)

1. 语法

与增删改不同,一般情况下进行查询操作时,查询到的记录信息需要储存在一个类中,所以需要指定返回值的类型

在映射文件中

<!-- 注意!!!!!此时第二个参数不再试parameterType,而是resultType,变成了返回值类型 -->
<select id = "接口中查询记录的方法名"  resultType = "返回的参数类型,一般写某个类">
    SQL语句
</update>

2. 实例

在上一章 通过接口来自定义数据库的操作方法 一节中已经有过实例