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",""); mapper.addEmp(employee); /* //修改员工 Employee employee = new Employee(1,"xiximayou","1",""); mapper.updateEmp(employee); */ /* //删除员工 Integer id = 3; mapper.deleteEmp(id); */ //手动提交数据 openSession.commit(); } finally { // TODO: handle finally clause openSession.close(); } } }
原本数据库中存在的数据:
结果:
(1)添加
(2)修改
(3) 删除
继查找功能之后,本节实现的增删改功能均已正确实现。
相关推荐
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
dongxurr 2020-08-08
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
Dullonjiang 2020-07-30
jimgreatly 2020-07-27
liqiancao 2020-07-26
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26
helloxusir 2020-07-25
牧场SZShepherd 2020-07-20