java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Mybatis映射文件标签

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

作者:努力是一种常态

这篇文章主要介绍了Mybatis映射文件根标签与子标签,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、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","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映射文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文