2023-01-09 一、Mybatis映射文件 1、映射文件根標簽 mapping標簽: 該標簽中的namespace要求與介面的全類名一致 2、映射文件子標簽 (1)cache(該命名空間的緩衝配置) (2)cache-ref(引用其他命名空間的緩存配置) (3)resultMap(描述如何從數 ...
2023-01-09
一、Mybatis映射文件
1、映射文件根標簽
mapping標簽:
該標簽中的namespace要求與介面的全類名一致
2、映射文件子標簽
(1)cache(該命名空間的緩衝配置)
(2)cache-ref(引用其他命名空間的緩存配置)
(3)resultMap(描述如何從資料庫結果集中載入對象,是最複雜也是最強大的元素)
註意:resultType解決不了的問題,交給resultMap(一般是多表查詢)。
(4)sql(可被其他語句引用的可重用語句塊)
(5)insert(映射插入語句)
(6)update(映射更新語句)
(7)delete(映射刪除語句)
(8)select(映射查詢語句)
3、示例代碼
(1)“day02_mybatis.src.main.java.com.hh.mybatis.mapper.EmployeeMapper”中的代碼
package com.hh.mybatis.mapper; import com.hh.mybatis.pojo.Employee; import java.util.List; public interface EmployeeMapper { /** * 通過id獲取員工信息 * @param empId * @return */ public Employee selectEmployee(int empId); /** * 添加員工信息 * @param employee */ public void insertEmployee(Employee employee); /** * 刪除員工信息 * @param empId */ public void deleteEmployeeById(int empId); /** * 修改員工信息 * @param employee */ public void updateEmployee(Employee employee); /** * 查詢所有員工信息 * @return */ public List<Employee> selectAllEmps(); }
(2)映射文件中的代碼(day02_mybatis.src.main.resources.mapper下的EmployeeMapper.xml)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.hh.mybatis.mapper.EmployeeMapper"> <select id="selectEmployee" resultType="employee"> SELECT id, last_name, email, salary FROM tbl_employee WHERE id=#{empId} </select> <insert id="insertEmployee"> INSERT INTO tbl_employee(last_name,email,salary) VALUES (#{lastName},#{email},#{salary}) </insert> <delete id="deleteEmployeeById"> delete from tbl_employee where id=#{empId} </delete> <update id="updateEmployee"> update tbl_employee set last_name = #{lastName}, email = #{email}, salary = #{salary} where id = #{id} </update> <select id="selectAllEmps" resultType="employee"> SELECT id, last_name, email, salary FROM tbl_employee </select> </mapper>
(3)測試類代碼(day02_mybatis.src.test.java.TestMybatis)
import com.hh.mybatis.mapper.EmployeeMapper; import com.hh.mybatis.pojo.Employee; 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; import java.util.List; public class TestMybatis { @Test public void TestMybatis(){ try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通過sqlSessionFactory獲取sqlSession對象 SqlSession sqlSession = sqlSessionFactory.openSession(); //獲取EmployeeMapper的代理對象 EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); System.out.println("employeeMapper.getClass().getName() = " + employeeMapper.getClass().getName()); Employee employee = employeeMapper.selectEmployee(1); System.out.println("employee = " + employee); } catch (IOException e) { e.printStackTrace(); } } /** * 測試添加員工 */ @Test public void TestMybatisInsert(){ try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //通過sqlSessionFactory獲取sqlSession對象 SqlSession sqlSession = sqlSessionFactory.openSession(); //獲取EmployeeMapper的代理對象 EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); // Employee wangwu = new Employee(null,"wangwu","[email protected]",50.0); //測試添加員工信息 // employeeMapper.insertEmployee(wangwu); //測試刪除員工信息 // employeeMapper.deleteEmployeeById(2); // Employee wangwu = new Employee(1,"zhangsan1","[email protected]",102.0); // // employeeMapper.updateEmployee(wangwu); //查詢所有員工信息 List<Employee> employees = employeeMapper.selectAllEmps(); for (Employee employee : employees) { System.out.println("employee = " + employee); } //提交事務 sqlSession.commit(); } catch (IOException e) { e.printStackTrace(); } } }