mybatis3.1-[topic-16-17]-對映檔案_增刪改查_insert_獲取自增主鍵的值
阿新 • • 發佈:2018-11-28
筆記要點
出錯分析與總結
/**測試第16章的增,刪,改 的內容 * 錯誤1: <insert id="addEmp" parameterType="com.bean.Employee"> ,注意選取資料型別是parameterType,不是Map * 錯誤2: 按順序查詢全部資料,但是delete清楚資料後, id自增變數不是從零開始的;所以自己的while迴圈不好! * --------- * 1. mybatis 支援允許增刪改查,直接定義如下型別: Integer/long/boolean型別 * 2. 手動提交即可, openSession.commit(); * 3.*/
工程組織
EmployeeMapper.xml (加入 增,刪,改 的sql對映語句的內容)
<?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.dao.EmployeeMapper"> <!-- namespace: 名稱空間 id: 唯一標識 resultType:返回值型別 #{id} : 從傳遞過來的引數中取出id值 public Employee getEmpById(Integer id);--> <select id="getEmpById" resultType="com.bean.Employee" databaseId="mysql"> select * from tbl_employee where id = #{id} </select> <!-- public void addEmp(Employee employee); parameterType : 引數型別,可以省略; mysql支援自增主鍵,自增主鍵的獲取:mybatis也是利用statement.getGeneratedKeys() 呼叫方法: 在標籤中新增即可: useGeneratedKeys="true";--> <insert id="addEmp" parameterType="com.bean.Employee" useGeneratedKeys="true" keyProperty="id"> insert into tbl_employee(last_name,email,gender) values (#{lastName},#{email},#{gender}) </insert> <!--public void updateEmp(Employee employee);--> <update id="updateEmp" > update tbl_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id} </update> <!--public void deleteEmpById(Integer id);--> <delete id="deleteEmpById"> delete from tbl_employee where id=#{id} </delete> </mapper>
EmployeeMapper.java [com.dao 下的]
package com.dao; import com.bean.*; //介面式程式設計! namespace: 名稱空間,指定為介面名字; public interface EmployeeMapper { public Employee getEmpById(Integer id); //新增返回值型別,表示mybatis的會話狀態是true或者false public boolean addEmp(Employee employee); public boolean updateEmp(Employee employee); public boolean deleteEmpById(Integer id); }
class test_tp16 測試類 [com.test下的]
package com.test; import com.bean.Employee; import com.dao.EmployeeMapper; 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 java.io.IOException; import java.io.InputStream; /**測試第16章的增,刪,改 的內容 * 錯誤1: <insert id="addEmp" parameterType="com.bean.Employee"> ,注意選取資料型別是parameterType,不是Map * 錯誤2: 按順序查詢全部資料,但是delete清楚資料後, id自增變數不是從零開始的;所以自己的while迴圈不好! * --------- * 1. mybatis 支援允許增刪改查,直接定義如下型別: Integer/long/boolean型別 * 2. 手動提交即可, openSession.commit(); * 3. */ public class test_tp16 { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream=Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); } @Test public void test03() throws IOException{ //預設是不自動提交資料的,需要我們自己手動提交 SqlSession openSession = getSqlSessionFactory().openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); //1-1 測試:增加一條記錄 // Employee employee1 = new Employee(null, "jerry", "[email protected]", "0"); // mapper.addEmp(employee1); //1-1-1 測試自增:增加一條記錄 Employee employee1 = new Employee(null, "葫蘆娃", "葫蘆娃@163.com", "0"); mapper.addEmp(employee1); //1-2 測試; 更新一號的名字為 jerry,性別為1 // Employee employee = new Employee(1, "jerry", "[email protected]", "1"); // mapper.updateEmp(employee); //1-3 測試刪除,刪除第二號的員工 // boolean b= mapper.deleteEmpById(2); // System.out.println(b); //1-4 測試; 按順序查詢全部資料, Employee employee; int i=1; while ((employee = mapper.getEmpById(i))!=null){ i++; System.out.println(employee); } //2.手動提交 openSession.commit(); } finally { openSession.close(); } } }