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(); } } }
标签:Mybatis,标签,EmployeeMapper,mybatis,Employee,employeeMapper,employee,与子,import From: https://www.cnblogs.com/isDaHua/p/17038365.html