最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

世界播报:学习笔记——Mybatis映射文件根标签与子标签

来源:博客园

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 selectAllEmps();}

(2)映射文件中的代码(day02_mybatis.src.main.resources.mapper下的EmployeeMapper.xml)

                INSERT INTO            tbl_employee(last_name,email,salary)        VALUES            (#{lastName},#{email},#{salary})                delete from            tbl_employee        where            id=#{empId}                update            tbl_employee        set            last_name = #{lastName},            email = #{email},            salary = #{salary}        where            id = #{id}        

(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","zs@163.com",50.0);            //测试添加员工信息//            employeeMapper.insertEmployee(wangwu);            //测试删除员工信息//            employeeMapper.deleteEmployeeById(2);//            Employee wangwu = new Employee(1,"zhangsan1","zs1@163.com",102.0);////            employeeMapper.updateEmployee(wangwu);            //查询所有员工信息            List employees = employeeMapper.selectAllEmps();            for (Employee employee : employees) {                System.out.println("employee = " + employee);            }            //提交事务            sqlSession.commit();        } catch (IOException e) {            e.printStackTrace();        }    }}

关键词: 命名空间 提交事务 查询语句