返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis映射文件根标签与子标签示例讲解
  • 913
分享到

Mybatis映射文件根标签与子标签示例讲解

Python 官方文档:入门教程 => 点击学习

摘要

目录一、mybatis映射文件1、映射文件根标签2、映射文件子标签3、示例代码一、Mybatis映射文件 1、映射文件根标签 mapping标签: 该标签中的namespace要求与

一、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 {
    
    public Employee selectEmployee(int empId);

    
    public void insertEmployee(Employee employee);

    
    public void deleteEmployeeById(int empId);

    
    public void updateEmployee(Employee employee);

    
    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","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<Employee> employees = employeeMapper.selectAllEmps();
            for (Employee employee : employees) {
                System.out.println("employee = " + employee);
            }
            //提交事务
            sqlSession.commit();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

到此这篇关于Mybatis映射文件根标签与子标签的文章就介绍到这了,更多相关Mybatis映射文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Mybatis映射文件根标签与子标签示例讲解

本文链接: https://lsjlt.com/news/177235.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作