码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-01-18 21:02:59      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:try   integer   NPU   todo   环境   factor   throws   div   upd   

借之前配置好的环境:

技术图片

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映射文件之基本的增删改查

标签:try   integer   NPU   todo   环境   factor   throws   div   upd   

原文地址:https://www.cnblogs.com/xiximayou/p/12210113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!