当前位置:   article > 正文

Mybatis对数据的增删改_mybatis中update是,参数是integer

mybatis中update是,参数是integer

接口类

  1. package com.qst.dao;
  2. import com.qst.mybatis.Employee;
  3. //可设置返回值 为long boolean Integer
  4. public interface EmployeeMapper {
  5. public Employee getEmpById(Integer id);
  6. public long updateEmp(Employee employee);
  7. public boolean insertEmp(Employee employee);
  8. public Integer deleteEmp(Integer id);
  9. }

sql实现

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.qst.dao.EmployeeMapper">
  5. <select id="getEmpById" resultType="com.qst.mybatis.Employee">
  6. select * from employee where id = #{id}
  7. </select>
  8. <insert id="insertEmp" >
  9. insert into employee(name,gender,email) values(#{name},#{gender},#{email})
  10. </insert>
  11. <delete id="deleteEmp" >
  12. delete from employee where id = #{id}
  13. </delete>
  14. <update id="updateEmp">
  15. update employee set name = #{name},gender=#{gender},email=#{email} where id = #{id}
  16. </update>
  17. </mapper>

测试  

插入一条信息

  1. @Test
  2. public void test03() throws IOException {
  3. SqlSessionFactory sqlSessionFactory = getSqlsessionFactory();
  4. SqlSession openSession = sqlSessionFactory.openSession();
  5. try{
  6. EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
  7. Employee emp = new Employee("jack","0","126");
  8. boolean insertEmp = mapper.insertEmp(emp);
  9. System.out.println(insertEmp);
  10. openSession.commit();
  11. }finally {
  12. openSession.close();
  13. }
  14. }

 

删除一条信息

  1. @Test
  2. public void test03() throws IOException {
  3. SqlSessionFactory sqlSessionFactory = getSqlsessionFactory();
  4. SqlSession openSession = sqlSessionFactory.openSession();
  5. try{
  6. EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
  7. long deleteEmp= mapper.deleteEmp(1);
  8. System.out.println(deleteEmp);
  9. openSession.commit();
  10. }finally {
  11. openSession.close();
  12. }
  13. }

修改一条信息

  1. @Test
  2. public void test03() throws IOException {
  3. SqlSessionFactory sqlSessionFactory = getSqlsessionFactory();
  4. SqlSession openSession = sqlSessionFactory.openSession();
  5. try{
  6. EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
  7. Employee emp = new Employee(1"jack","0","126");
  8. Integer updateEmp = mapper.updateEmp(emp);
  9. System.out.println(updateEmp);
  10. openSession.commit();
  11. }finally {
  12. openSession.close();
  13. }
  14. }

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号