Mybatis----简单实现增删改查

上一篇有环境搭建和运行,在其基础上在学习增删改,冲冲冲~

先创建数据库,并添加一些数据

CREATE TABLE `tbl_employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `last_name` varchar(255) DEFAULT NULL,
  `gender` char(1) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

创建JavaBean

package bean;

public class Employee {
    private Integer id;
    private String lastName;
    private String email;
    private String gender;

    public Employee() {
    }

    public Employee(Integer id, String lastName, String email, String gender) {
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.gender = gender;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getlastName() {
        return lastName;
    }

    public void setlastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "pojo.Employee{" +
                "id=" + id +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}

创建接口并创建方法

package dao;

import bean.Employee;

import java.util.List;
import java.util.Map;

public interface EmployeeMapper {

	public Employee getEmpById(Integer id);

    public int addEmp(Employee e);

    public boolean updateEmp(Employee e);

    public int deleteEmpById(Integer id);
}

创建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="dao.EmployeeMapper">
    <!--
        namespace: 名称空间;指定为接口的全类名
        id :唯一标识
        resultType:返回值类型
        #{id}: 从传过来的参数中取出id值    -->

<!--  public Employee getEmpById(Integer id);  -->
    <select id="getEmpById" resultType="emp" databaseId="mysql">
        select * from tbl_employee where id = #{id}
    </select>

<!--public int addEmp(Employee e);-->
    <insert id="addEmp" parameterType="bean.Employee">
        insert into tbl_employee(last_name,email,gender)
        value(#{lastName},#{email},#{gender})
    </insert>

<!--  public boolean updateEmp(Employee e);  -->
    <update id="updateEmp">
        update tbl_employee
            set last_name = #{lastName},email=#{email},gender=#{gender}
            where id = #{id}
    </update>
    
<!--  public int deleteEmpById(Integer id);  -->
    <delete id="deleteEmpById">
        delete from tbl_employee where id=#{id}
    </delete>

</mapper>

别忘了把这个xml文件注册到mybatis-config.xml文件中,不然会报异常
org.apache.ibatis.binding.BindingException: Type interface dao.EmployeeMapper is not known to the MapperRegistry


测试:增删改都要手动提交,也可以在获取openSession时调用重载方法,openSession(true)开启自动提交

    @Test
    public void test03() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);

            //增加员工
            Employee e = new Employee(null, "jerry", "123456@qq.com", "0");
            mapper.addEmp(e);
            System.out.println(e.getId());

            //修改员工信息
            Employee e1 = new Employee(1, "jerry", "123456@qq.com", "1");
            mapper.updateEmp(e1);
            
            //删除员工
            mapper.deleteEmpById(2);

            //手动提交
            openSession.commit();
        }finally {
            openSession.close();
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>