当前位置: 首页 > news >正文

MyBatis——创建与使用

概念

当我们使用传统的jdbc进行数据库与程序的连接时,每一个操作都需要写一条sql语句,并且没法调试和修改

jdbc连接数据库流程:

  1. 创建数据库连接池DataSource
  2. 获取数据库连接Connection
  3. 执行带占位符的sql语句
  4. 通过Connection创建操作对象Statement
  5. 指定替换占位符的字段类型,值
  6. 使用Statement执行sql语句
  7. 返回结果或更新的数量
  8. 处理返回的结果
  9. 释放资源

而MyBatis则是一个持久层框架,可以使用xml或者注解来方便的进行数据库的操作

创建MyBatis项目

在这里插入图片描述
创建spring项目时勾选上面五个依赖
如果使用的是oracle数据库,那么将MySQL Driver替换成Oracle Driver

在配置文件中配置数据库的连接信息

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/database?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

MyBatise是一个ORM框架,会将查询到的数据与java中的类进行互相转化

配置MyBatis中的XML路径

MyBatis中使用XML来保存数据库的sql语句,因此在配置文件中还要加上下面这条语句

mybatis.mapper-locations=classpath:包名/*Mapper.xml

例如:

mybatis.mapper-locations=classpath:mybatis/*Mapper.xml

业务代码

创建用户信息类

package com.example.demo.entity;
import lombok.Data;
import java.time.LocalDateTime;@Data
public class UserInfo {private int id;private String username;private String password;private String photo;private LocalDateTime createTime;private LocalDateTime updateTime;private int state;
}

创建Mapper接口

package com.example.demo.mapper;import org.apache.ibatis.annotations.Mapper;
import com.example.demo.entity.UserInfo;
import org.apache.ibatis.annotations.Param;@Mapper
public interface UserMapper {UserInfo getUserById(@Param("user_id") Integer id);
}

添加UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybati
s.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper"><select id="getUserById" resultType="com.example.demo.entity.UserInfo">select * from userinfo where id=${user_id}</select>
</mapper>

创建UserService

使用属性注入获取UserMapper对象,调用其getUserById方法

package com.example.demo.service;import com.example.demo.entity.UserInfo;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public UserInfo getUserById(Integer id){return userMapper.getUserById(id);}
}

创建UserController

使用属性注入,获取到UserService对象,然后调用其getUserById方法

package com.example.demo.controller;
import com.example.demo.entity.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/user")
public class UserController {@Autowiredpublic UserService userService;@RequestMapping("/get-user-by-id")public UserInfo getUserById(Integer id){if(id == null){return null;}return userService.getUserById(id);}
}

最终就可以在浏览器中获取到数据库中的数据了
在这里插入图片描述

http://www.lryc.cn/news/19760.html

相关文章:

  • 【涨薪技术】0到1学会性能测试 —— 参数化关联
  • go进阶(2) -深入理解Channel实现原理
  • 数组(二)-- LeetCode[303][304] 区域和检索 - 数组不可变
  • 22-基于分时电价条件下家庭能量管理策略研究MATLAB程序
  • “XXX.app 已损坏,打不开。您应该将它移到废纸篓”,Mac应用程序无法打开或文件损坏的处理方法(2)
  • flask入门-3.Flask操作数据库
  • STM32 使用microros与ROS2通信
  • 51单片机入门 - 测试:SDCC / Keil C51 会让没有调用的函数参与编译吗?
  • 【计算机网络】计算机网络
  • 【java web篇】项目管理构建工具Maven简介以及安装配置
  • springboot笔记
  • 【多线程与高并发】- 浅谈volatile
  • avro格式详解
  • 【涨薪技术】0到1学会性能测试 —— LR录制回放事务检查点
  • 卡尔曼滤波原理及代码实战
  • Jmeter使用教程
  • 论文笔记|固定效应的解释和使用
  • 数据集市与数据仓库的区别
  • Golang学习Day3
  • Python并发编程-事件驱动模型
  • 构建系统发育树简述
  • 这款 Python 调试神器推荐收藏
  • 金三银四吃透这份微服务笔记,面试保准涨10K+
  • 构建matter over Thread的演示系统-efr32
  • 【一天一门编程语言】Matlab 语言程序设计极简教程
  • 看似平平无奇的00后,居然一跃上岸字节,表示真的卷不过......
  • BZOJ2142 礼物
  • MySQL高级第一讲
  • 前端面试常用内容——基础积累
  • 跟着《代码随想录》刷题(三)——哈希表