学习笔记——Mybatis映射文件根标签与子标签(mybatis框架中映射查询结果的标签)

  本篇文章为你整理了学习笔记——Mybatis映射文件根标签与子标签(mybatis框架中映射查询结果的标签)的详细内容,包含有mybatisxml映射文件常见标签 mybatis框架中映射查询结果的标签 mybatis映射文件中元素属性 mybatis映射文件名 学习笔记——Mybatis映射文件根标签与子标签,希望能帮助你了解 学习笔记——Mybatis映射文件根标签与子标签。

  (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

   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

   last_name = #{lastName},

   email = #{email},

   salary = #{salary}

   where

   id = #{id}

   /update

   select id="selectAllEmps" resultType="employee"

   SELECT

   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框架中映射查询结果的标签)的详细内容,想要了解更多 学习笔记——Mybatis映射文件根标签与子标签的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: