mybatis映射文件之基本的增删改查

Posted 西西嘛呦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis映射文件之基本的增删改查相关的知识,希望对你有一定的参考价值。

借之前配置好的环境:

1、首先在Employee .java中加上有参的构造器和无参的构造器。

2、采用mapper中的class属性配置映射文件。

    <mappers>
        <mapper class="com.gong.mybatis.dao.EmployeeMapper" />        
    </mappers>

3、EmployeeMapper.java

package com.gong.mybatis.dao;

import com.gong.mybatis.bean.Employee;

public interface EmployeeMapper {
    public Employee getEmpById(Integer id);
    
    public boolean addEmp(Employee employee);
    
    public void updateEmp(Employee employee);
    
    public void deleteEmp(Integer id);
}

4、EmployeeMapper.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.gong.mybatis.dao.EmployeeMapper">
    <select id="getEmpById" resultType="com.gong.mybatis.bean.Employee">
        select id,last_name lastName,email,gender from tbl_employee where id = #{id}
    </select>
    <insert id="addEmp" parameterType="com.gong.mybatis.bean.Employee">
        insert into tbl_employee(last_name,gender,email)
        values(#{lastName},#{gender},#{email}) 
    </insert>
    <update id="updateEmp" >
        update tbl_employee set last_name=#{lastName},gender=#{gender},email=#{email}
        where id = #{id}
    </update>
    <delete id="deleteEmp">
        delete from tbl_employee where id=#{id}
    </delete>
</mapper>

说明:接口中的增删改方法可以有Integer、long、boolean返回值。在EmployeeMapper.xml中parameterType为输入的参数类型,resultType为返回的参数类型。

5、在TestMybatis.java中进行单元测试

package com.gong.mybatis.test;

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

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 com.gong.mybatis.bean.Employee;
import com.gong.mybatis.dao.EmployeeMapper;
import com.gong.mybatis.dao.EmployeeMapperAnnotation;

public class TestMybatis {
    
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(is);
    }
    
    @Test
    public void test03() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        //不会自动提交数据
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
            /*
            //添加员工
            Employee employee = new Employee(null,"bob","1","123455777@qq.com");
            mapper.addEmp(employee);
            
            /*
            //修改员工
            Employee employee = new Employee(1,"xiximayou","1","xiximayou@qq.com");
            mapper.updateEmp(employee);
            */
            /*
            //删除员工
            Integer id = 3;
            mapper.deleteEmp(id);
            */
            //手动提交数据
            openSession.commit();
            
            
        } finally {
            // TODO: handle finally clause
            openSession.close();
        }
    }

}

原本数据库中存在的数据:

 结果:

(1)添加

(2)修改

(3) 删除

继查找功能之后,本节实现的增删改功能均已正确实现。 

以上是关于mybatis映射文件之基本的增删改查的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis从入门到精通系列 13——基于注解配置的增删改查

[mybatis]快速搭建一个mybatis程序,实现对数据的增删改查

MYBATIS01_概述及优缺点快速搭建工程Mybatis的增删改查操作总结

MyBatis的增删改查操作

mybatis生成的增删改查怎么用

MyBatis框架入门小案例(关于用mybatis框架对数据库的增删改查)