mybaits基础增删改查-------mybatis(四)

2024-05-17 16:32

本文主要是介绍mybaits基础增删改查-------mybatis(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Mybatis的增删改查

  • mybatis流程:
    1 创建实体类及接口方法
    2 创建全局配置文件 configuration.xml
    3 创建 sql 映射文件 BlogMapper.xml
    4 将全局文件中维护 sql映射文件配置
    5 调用java API 执行相关sql操作
    注意sqlSession是线程非安全的

    实体java类:

    package model;public class Bloger {private Integer id;private String name;//这里需要注意变量的命名规范,因为实体变量会和sql记录一一对应,// 所以不规范的命名方式会导致mybatis执行异常private String userName;private int age;private String email;public Bloger(){}public Bloger(Integer id, String name, String userName, int age, String email) {this.id = id;this.name = name;this.userName = userName;this.age = age;this.email = email;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Overridepublic String toString() {return "BlogId:"+ getId()+ "  BlogName:"+getName()+ "  username:" + getUserName();}
    }
    

接口方法:

	package dao;import model.Bloger;import org.apache.ibatis.annotations.Select;public interface BlogerMapper {//通过接口方法 getBlogerId(Integer id)返回一个实体对象public Bloger getBlogerId(Integer id);public void addBloger(Bloger bloger);public void updateBloger(Bloger bloger);public Boolean deleteBloger(Integer id);}

mapper 配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="dao.BlogerMapper"><!--model.Bloger --><select id="getBlogerId" resultType="Bloger">select id,name,user_name,age,email from mybatis_test where id = #{id}</select><!--添加博主 --><insert id="addBloger" parameterType="Bloger">INSERT  into mybatis_test(user_name,age,email,name)VALUES (#{user_name},#{age},#{email},#{name});</insert><update id="updateBloger" parameterType="model.Bloger">UPDATE mybatis_testSET user_name=#{user_name},age=#{age},email=#{email},name=#{name}WHERE id = #{id}</update><delete id="deleteBloger" parameterType="model.Bloger">DELETE  FROM mybatis_test where id =#{id}</delete>
</mapper>
package test;import dao.BlogerMapper;
import model.Bloger;
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.IOException;
import java.io.InputStream;
public class MybatisDriver {public static void main(String[] args) throws Exception{mybatits_select();}public static SqlSessionFactory getSqlSessionFactory() throws IOException {String resource = "source/configuration.xml";InputStream inputStream= Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);return sqlSessionFactory;}public static void mybatits_add() throws IOException{// 获取SqlSessionFactory对象SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();// 获取回话对象SqlSession sqlSession=sqlSessionFactory.openSession();// 这里MyBaitis帮我们创建了代理对象BlogerMapper blogerMapper =sqlSession.getMapper(BlogerMapper.class);Bloger bloger=new Bloger();bloger.setName("樊瑞8号");bloger.setEmail("wujianqinjian8@163.com");bloger.setAge(33);bloger.setUserName("wujianqinjian8");blogerMapper.addBloger(bloger);sqlSession.commit();sqlSession.close();}public static void mybatits_delete() throws IOException{// 获取SqlSessionFactory对象SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();// 获取回话对象SqlSession sqlSession=sqlSessionFactory.openSession();// 这里MyBaitis帮我们创建了代理对象BlogerMapper blogerMapper =sqlSession.getMapper(BlogerMapper.class);blogerMapper.deleteBloger(11);sqlSession.commit();sqlSession.close();}public static void mybatits_update() throws IOException{// 获取SqlSessionFactory对象SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();// 获取回话对象SqlSession sqlSession=sqlSessionFactory.openSession();// 这里MyBaitis帮我们创建了代理对象BlogerMapper blogerMapper =sqlSession.getMapper(BlogerMapper.class);Bloger bloger=blogerMapper.getBlogerId(9);bloger.setAge(33);bloger.setUserName("wujianqinjian09");blogerMapper.updateBloger(bloger);sqlSession.commit();sqlSession.close();}public static void mybatits_select() throws IOException{// 获取SqlSessionFactory对象SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();// 获取回话对象SqlSession sqlSession=sqlSessionFactory.openSession();// 这里MyBaitis帮我们创建了代理对象BlogerMapper blogerMapper =sqlSession.getMapper(BlogerMapper.class);Bloger bloger=blogerMapper.getBlogerId(3);System.out.println(bloger);sqlSession.close();}
}
总结:看过前几章节的小伙伴,一定觉得这篇博客实在没有啥技术含量,为了保证系列的完整性,写了这个实例,需要注意的实体变量的命名需要规范从而避免不必要的麻烦!

最后,建了一个微信群,本来想大家一起共同学习,但学习气氛好像目前还不算特别好,加群主微信进群!(由于广告太多,所以请先点赞,然后发博客名!)
在这里插入图片描述

创建了一个技术闲聊群:有兴趣可加我微信,拉你一起讨论杂七杂八的技术,虽然大家都不怎么活跃!
加好友备注:你的博客名 && 随便给我的任意文章点个赞或留言
在这里插入图片描述

这篇关于mybaits基础增删改查-------mybatis(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/995594

相关文章

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

mybatis中resultMap的association及collectio的使用详解

《mybatis中resultMap的association及collectio的使用详解》MyBatis的resultMap定义数据库结果到Java对象的映射规则,包含id、type等属性,子元素需... 目录1.reusltmap的说明2.association的使用3.collection的使用4.总

mybatis-plus QueryWrapper中or,and的使用及说明

《mybatis-plusQueryWrapper中or,and的使用及说明》使用MyBatisPlusQueryWrapper时,因同时添加角色权限固定条件和多字段模糊查询导致数据异常展示,排查发... 目录QueryWrapper中or,and使用列表中还要同时模糊查询多个字段经过排查这就导致只要whe

SpringBoot集成MyBatis实现SQL拦截器的实战指南

《SpringBoot集成MyBatis实现SQL拦截器的实战指南》这篇文章主要为大家详细介绍了SpringBoot集成MyBatis实现SQL拦截器的相关知识,文中的示例代码讲解详细,有需要的小伙伴... 目录一、为什么需要SQL拦截器?二、MyBATis拦截器基础2.1 核心接口:Interceptor

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推