开发者

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

开发者 https://www.devze.com 2023-01-10 10:40 出处:网络 作者: 努力是一种常态
目录一、MyBATis映射文件1、映射文件根标签2、映射文件子标签3、示例代码一、Mybatis映射文件
目录
  • 一、MyBATis映射文件
    • 1、映射文件根标签
    • 2、映射文件子标签
    • 3、示例代码

一、Mybatis映射文件

1、映射文件根标签

mapping标签:

该标签中的namespace要求与接口的全类名一致

2、映射文件子标签

(1)cache(该命名空间的缓冲配置)

(2)cache-ref(引用其他命名空间的缓存配置)

(3)resultMap(描述如何从数据库结果集中加载对象,是最复杂也是最强大的元素)

注意:result开发者_C入门Type解决不了的问题,交给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;

androidimport 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
            jsid=#{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 SqlSessionFactory编程客栈Builder().build(inputStream);
            //通过sqlSessionFactory获取sqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //获取EmployeeMapper的代理对象
            EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
            System.out.prin编程客栈tln("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 phpEmployee(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映射文件内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号