其主要用到MyBatis jar包,也可以搜索其官网进行下载,这里我给一个我自己用的MyBatis版本MyBatis-3.5.7提取码为"fs33"以及相应的配置文件,如有不懂请看配置文件注释MyBatis.xml文件,其提取码为"nn8b"
以下代码是和数据库对接的具体案例
(1).包结构

(2).其项目的具体案例代码

package cn.flower.entity;/*** @version 1.0* @author: 小原* @date: 2021-06-17 13:32*/
public class Flower {private String id;private String name;private String anpthenmname;private String property;private String price;private String production;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAnpthenmname() {return anpthenmname;}public void setAnpthenmname(String anpthenmname) {this.anpthenmname = anpthenmname;}public String getProperty() {return property;}public void setProperty(String property) {this.property = property;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;}public String getProduction() {return production;}public void setProduction(String production) {this.production = production;}@Overridepublic String toString() {return "Flower{" +"id='" + id + '\'' +", name='" + name + '\'' +", anpthenmname='" + anpthenmname + '\'' +", property='" + property + '\'' +", price='" + price + '\'' +", production='" + production + '\'' +'}';}public Flower() {}/*** flower有参构造器* @param id 编号* @param name 花名* @param anpthenmname 花的别名* @param property 花的科属* @param price 花的价格* @param production 花的生产地*/public Flower(String id, String name, String anpthenmname, String property, String price, String production) {this.id = id;this.name = name;this.anpthenmname = anpthenmname;this.property = property;this.price = price;this.production = production;}
}

以下是接口中需要实现的方法

package cn.flower.dao;import cn.flower.entity.Flower;import java.util.List;/*** @version 1.0* @author: 小原* @date: 2021-06-17 14:30*/
public interface FlowerDao {public List<Flower> selectAll();public List<Flower> selectAllById(String id);public int insertFlower(Flower f);public int deleteById(String id);public int updateById(Flower f);
}

以下是实现类中具体的实现,切记在我们进行增删改的时间一定要调用一下commit()方法,不然不会报错也不会出现在你的数据库中产生受影响的行数

package cn.flower.dao.impl;import cn.flower.dao.BaseMapper;
import cn.flower.dao.FlowerDao;
import cn.flower.entity.Flower;
import org.apache.ibatis.session.SqlSession;import java.util.List;/*** @version 1.0* @author: 小原* @date: 2021-06-17 14:30*/
public class FlowerDaoImpl implements FlowerDao {private SqlSession conn = BaseMapper.getConn();FlowerDao mapper = conn.getMapper(FlowerDao.class);@Overridepublic List<Flower> selectAll() {List<Flower> flowers = mapper.selectAll();return flowers;}@Overridepublic List<Flower> selectAllById(String id) {List<Flower> flowers = mapper.selectAllById(id);return flowers;}@Overridepublic int insertFlower(Flower f) {int num = mapper.insertFlower(f);conn.commit();return num;}@Overridepublic int deleteById(String id) {int num = mapper.deleteById(id);conn.commit();return num;}@Overridepublic int updateById(Flower f) {int num = mapper.updateById(f);conn.commit();return num;}
}

看了这样的多是不是有些迷茫了?有配置文件,但是我们貌似没有读取,其下就是我们的一个简单BaseMapper类,主要帮助我们建立连接等等

package cn.flower.dao;import cn.flower.dao.impl.FlowerDaoImpl;
import cn.flower.entity.Flower;
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.jetbrains.annotations.Contract;import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;/*** @version 1.0* @author: 小原* @date: 2021-06-17 13:37*/
public class BaseMapper {private static final String resource = "mybatis_config.xml";private static SqlSessionFactory sqlSessionFactory;static {InputStream inputStream = null;try {inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}private static SqlSession conn;/*** 开启数据库的链接* @return*/public static SqlSession getConn() {return conn = sqlSessionFactory.openSession();}public static boolean close() {if (conn != null) {conn.close();return true;} else {return false;}}public static int executeUpdate(String sql) {return 0;}//    @Contract(pure = true)
//    public static <T> List<T> executeQuery(Class<T> clz, String methodName) {//        conn.getMapper(clz);
//        try {//            T t = clz.getConstructor().newInstance();
//            Method method = clz.getMethod(methodName);
//            conn.getMapper(clz);
//            //System.out.println(((List<T>) invoke).size());
//        } catch (InstantiationException e) {//            e.printStackTrace();
//        } catch (IllegalAccessException e) {//            e.printStackTrace();
//        } catch (InvocationTargetException e) {//            e.printStackTrace();
//        } catch (NoSuchMethodException e) {//            e.printStackTrace();
//        }
//        //FlowerDao mapper = conn.getMapper(FlowerDao.class);
//
//        return null;
//        //return (List<T>) mapper.selectAll();
//        //return (List<T>) flowers;
//    }
}

紧接着就是我们的测试类中就需要调用这些方法啦

package cn.flower.test;import cn.flower.dao.BaseMapper;
import cn.flower.dao.FlowerDao;
import cn.flower.dao.impl.FlowerDaoImpl;
import cn.flower.entity.Flower;/*** @version 1.0* @author: 小原* @date: 2021-06-17 13:31*/
public class Test {/*** 程序的入口* @param args 入口参数*/public static void main(String[] args) {FlowerDao fd=new FlowerDaoImpl();
//        name='矮牵牛', anpthenmname='碧冬茄',
//        property='茄科碧冬茹属',
//        price='2.5', production='南美阿根廷'//fd.insertFlower(new Flower(null,"小原同志","小原","人类","10000","中国第一帅"));Flower f=new Flower();f.setId("9");f.setName("王泽轩");fd.updateById(f);
//        fd.deleteById("6");
//        fd.deleteById("7");
//        fd.deleteById("8");fd.selectAll().forEach(temp-> System.out.println(temp.toString()));//fd.selectAllById("1").forEach(temp-> System.out.println(temp.toString()));}
}

其下是效果:这里我们就只演示一个查询的效果了,剩下的小伙伴们运行代码即可看到了呢,嗯哼

这里呢我们顺带演示出了修改后的效果了,emmm
补充一下哈,另外我们要进行增删改查的xml文件忘了发了,嘻嘻!

<?xml version="1.0" encoding="UTF-8" ?>
<!--注意事项
(1).增删改查的标签中,id必须和类中的方法名相同
(2).resultType为实体类的全路径,有一点儿是例外的就是当是查询多少条记录的时间这个值为int 等等
-->
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.flower.dao.FlowerDao"><select id="selectAll" resultType="cn.flower.entity.Flower">select * from flower</select><select id="selectAllById" resultType="cn.flower.entity.Flower">select * from flower where id=#{id}</select><insert id="insertFlower" parameterType="cn.flower.entity.Flower">INSERT INTO flower VALUES(NULL,#{name},#{anpthenmname},#{property},#{price},#{production})</insert><delete id="deleteById" parameterType="cn.flower.entity.Flower">delete from flower where id=#{id}</delete><update id="updateById" parameterType="cn.flower.entity.Flower">update flower set name =#{name} where id =#{id}</update>
</mapper>

MyBatis框架的基础用法(增删改查)相关推荐

  1. mybatis从零基础到增删改查数据库

    本文是mybatis框架一个初步的入门总结,最全的最好的资料应该参考这个:http://mybatis.github.io/mybatis-3/zh/index.html 本文在Eclipse下搭建一 ...

  2. 如何使用mybatis框架对数据库进行增删改查?

    1.配置mybatis 1.1 引入依赖 mybatis依赖 <dependency><groupId>org.mybatis</groupId><artif ...

  3. Mybatis基础:增删改查、模糊查询、多条件查询

    Mybatis基础:增删改查.模糊查询.多条件查询http://www.bieryun.com/3132.html 1.新建测试数据库,根据实体类属性创建 2.实体类 [java] view plai ...

  4. Mybatis实现简单的数据库增删改查操作

    简介: MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Ma ...

  5. neo4j图数据库安装(mac)+neo4j集成springboot实现基础的增删改查

    目录 第一部分 mac安装neo4j 第二部分 neo4j集成springboot实现基础的增删改查 一.图数据库相关配置 二.业务逻辑 实体类 持久层 业务层 表现层 启动类 三.测试 附录: 第一 ...

  6. Flask框架——数据库操作命令(增删改查)

    目录 创建数据表 添加数据 插入单条数据 插入多条数据 查询数据 全部查询 精确查询 模糊查询 主键查询 排序 修改数据 删除数据 删除数据表 上篇文章我们学习了Flask框架--数据库配置及迁移同步 ...

  7. MyBatis的Mapper 代理的增删改查操作(三)

    沉迷于黑与白世界中的人,无论怎么挣扎,都逃不过被同化的命运.前世看见了什么,那么今世便是什么. 上一章简单介绍了MyBatis的命名空间方式的增删改查操作(二),如果没有看过,请观看上一章. 一. M ...

  8. mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数

    mybatis复习02,简单的增删改查 创建数据表 user_info 在项目中创建数据表对应的实体类 UserInfo.java 在pom.xml文件中引入依赖 创建核心配置文件mybatis-co ...

  9. Springboot使用JPA框架对数据库实现增删改查(附详细代码)

    前言 1.本文将详细阐述如何使用JPA框架对数据库实现增删改查操作,业务中比较常见的应用场景几乎在这里都能看到,并且有详尽的代码可供直观演示,其中遇到的坑也进行了实时标注. 2.JPA的环境配置在前面 ...

最新文章

  1. 元宇宙,分三层!香港中文大学再现「校园元宇宙」原型
  2. 学长毕业日记 :本科毕业论文写成博士论文的神操作20170324
  3. 飞畅科技-工业交换机电源故障初探
  4. Flask爱家租房--房屋管理(获取房东发布的房源信息条目)
  5. MySQL视图的应用
  6. Apriori进行关联分析
  7. 分页查询为什么会报数组越界错误_Java Note-数据结构(1)数组
  8. 通过机房工作看软工之软工总结
  9. 华为p40pro如何升级鸿蒙,可以升级到鸿蒙OS的四款华为手机,相信都没有后悔入手!...
  10. 如何卸载FileZilla的Ftp服务
  11. [病毒木马] LSP劫持
  12. CSS display属性 – 不显示、显示表格、内联块等
  13. 京津冀辽迎入汛以来最强降雨,四川盆地西部形成暴雨结界
  14. 三星设备如何打开开发者模式、如何下载安装play store以便进行正常的Android开发中的google购买测试
  15. chm文件打不开:提示已取消到该网页的导航的解决办法
  16. C++随机设置壁纸小软件
  17. 2019全国c语言二级考试题库,2019年全国计算机二级考试试题题库(附答案)【精选】.docx...
  18. esp32 Micropython驱动ST7735 1.8寸TFT屏幕 中文显示;时间显示、网络network实时时间获取utptime;urequests、upip等包安装
  19. 数学笔记(二项式定理)
  20. android 4.4 电池电量管理底层分析(C\C++层)

热门文章

  1. vxe-input vue 日期选择组件带农历节日、小圆点提醒
  2. 2021届毕业生还没找到Android开发工作,看这一篇就够了!
  3. allegro ARTWORK设置笔记
  4. 2021运维岗位现在薪资行情-大家运维都在薪资范围?
  5. 单片机 数码管0~F显示
  6. android 屏幕截图检测,Android 屏幕截图
  7. python 批量修改/替换数据
  8. 使用awk 统计分析游戏后台日志中的数据
  9. TMS320C6678开发笔记---IBL编译与分析2
  10. 新手学Python之学习官网教程(五: Data Structures)