首页 > 其他分享 >MyBatis学习笔记03

MyBatis学习笔记03

时间:2022-08-25 11:13:51浏览次数:37  
标签:03 DepartmentMapper 笔记 mybatis Department department MyBatis import id

MyBatis单表操作

  1. mapper.xml修改 完成CRUD的操作
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tongjun.mybatis.mapper.DepartmentMapper">

    <insert id="insert" parameterType="com.tongjun.mybatis.entity.Department">
        insert into tbl_department (id, name, tel) values (#{id}, #{name}, #{tel})
    </insert>

    <update id="update" parameterType="com.tongjun.mybatis.entity.Department">
        update tbl_department set name = #{name}, tel = #{tel} where id = #{id}
    </update>

    <delete id="deleteById" parameterType="string">
        delete from tbl_department where id = #{id}
    </delete>

    <select id="findAll" resultType="com.tongjun.mybatis.entity.Department">
        select * from tbl_department
    </select>

    <select id="findById" parameterType="string" resultType="com.tongjun.mybatis.entity.Department">
        select * from tbl_department where id = #{id}
    </select>
    
</mapper>
  1. 动态代理的DepartmentMapper接口
package com.tongjun.mybatis.mapper;

import com.tongjun.mybatis.entity.Department;

import java.util.List;

/**
 * @author tj
 * @date 2022/8/25 10:47
 * @Version 1.0
 */
public interface DepartmentMapper {


    List<Department> findAll();

    int insert(Department department);

    int update(Department department);

    int deleteById(String id);

    Department findById(String id);

}

  1. 实体类没有区别
package com.tongjun.mybatis.entity;

import lombok.Data;

import java.io.Serializable;

/**
 * @author tj
 * @date 2022/8/25 10:26
 * @Version 1.0
 */
@Data
public class Department implements Serializable {

    private String id;

    private String deptName;

    private String phone;

}

  1. 编写测试程序
package com.tongjun.mybatis.app;

import com.tongjun.mybatis.entity.Department;
import com.tongjun.mybatis.mapper.DepartmentMapper;
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 java.io.InputStream;
import java.util.List;

/**
 * @author tj
 * @date 2022/8/25 10:28
 * @Version 1.0
 */
public class ApplicationTest {
    public static void main(String[] args) throws Exception{
        //拿到mybatis配置文件的流
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        //创建sqlSession工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //通过工厂拿到sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //获取Mapper接口的代理对象
        DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);

        List<Department> departmentList = departmentMapper.findAll();
        System.out.println("---------------departmentMapper.findAll()-----------------");
        departmentList.forEach(System.out::println);

        Department department = departmentMapper.findById("53e3803ebbf4f97968e0253e5ad4cc83");
        System.out.println("---------------departmentMapper.findById-----------------");
        System.out.println(department);
    }
}

  1. 查看控制台输出信息
---------------departmentMapper.findAll()-----------------
Department(id=18ec781fbefd727923b0d35740b177ab, deptName=开发部, phone=110)
Department(id=53e3803ebbf4f97968e0253e5ad4cc83, deptName=测试产品部, phone=111)
Department(id=ee0e342201004c1721e69a99ac0dc0df, deptName=运维部, phone=112)
2022-08-25 10:52:55,350 2431   [           main] DEBUG pper.DepartmentMapper.findById  - ==>  Preparing: select * from tbl_department where id = ? 
2022-08-25 10:52:55,350 2431   [           main] DEBUG pper.DepartmentMapper.findById  - ==> Parameters: 53e3803ebbf4f97968e0253e5ad4cc83(String) 
2022-08-25 10:52:55,366 2447   [           main] DEBUG pper.DepartmentMapper.findById  - <==      Total: 1 
---------------departmentMapper.findById-----------------
Department(id=53e3803ebbf4f97968e0253e5ad4cc83, deptName=测试产品部, phone=111)

标签:03,DepartmentMapper,笔记,mybatis,Department,department,MyBatis,import,id
From: https://www.cnblogs.com/tongjun/p/16623579.html

相关文章

  • 可信计算学习笔记 - 服务器可信支撑平台【GB/T 36639-2018】
    服务器可信支撑平台主要由物理可信根、可信基础组件和虚拟可信组件等部分组成根据服务器软硬件组成的不同,服务器可信支撑平台包含的部分也不同服务器硬件系统:应包......
  • MyBatis学习笔记02
    1.环境搭建1.1数据初始化//创建库CREATEDATABASEtj_mybatis_learning;//创建表CREATETABLEtbl_department(idvarchar(32)NOTNULL,deptNamevarchar(3......
  • MyBastis学习笔记01
    1.MyBatis概述MyBatis是一款优秀的持久层框架,它支持自定义SQL、存储过程以及高级映射。MyBatis免除了几乎所有的JDBC代码以及设置参数和获取结果集的工作。MyBati......
  • Dubbo/Zookeeper笔记
    分布式基础:Doubbo/Zookeeper分布式理论一、什么是分布式系统?分布式系统是若干个独立计算机的集合,这些计算机对于用户来说就像单个相关系统分布式系统是一组通过......
  • 2022-08-24 第五组 赖哲栋 学习笔记
    JavaScriptJavaScript脚本语言,解释型,主要用来给HTML网页增加动态功能通常的js是运行在浏览器环境下的JS的两种模型DOM:文档对象模型documentBOM:浏览器对象模型wind......
  • Java学习笔记5
    抽象类抽象类和其中抽象方法由abstract修饰,继承抽象类的所有方法必须由子类实现。Java的类是单继承,但是可以继承多个接口抽象类不能new实例化接口普通类:只有具体实......
  • 背包学习笔记
    ##前言最近学习了背包,来写篇学习笔记。如果你想认真看这篇笔记,可以参考配套题单,这些题目在下文练习题中也会提到。目录什么是背包01背包无优化空间优化......
  • HCIA学习笔记二十三:RSTP快速生成树的配置
    一、拓扑图• 在交换机拖出3台S5700,然后选择设备连线,点击Copper进行设备接线,完成后开启设备。二、RSTP模式配置[SW1]stpmoderstp[SW2]stpmoderstp[SW3]stpmod......
  • C学习笔记:memcpy与memmove函数探索
    #define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>voidmy_memcpy(void*des,constvoid*src,size_tk){ while(k--) { *(char*)des=*(char*)src; ++(cha......
  • 824笔记(闭包,递归,浅/深拷贝)
    闭包闭包:有权访问另一个函数作用域中变量的函数,一个作用域可以访问另外一个函数内部的局部变量作用:延伸了变量的作用范围特性:变量或者参数不会被垃圾回收机制回收函......