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

springboot整合mybatis入门程序

1.准备工作(创建springboot工程、数据库表user、实体类User)

       

 创建数据表:


create table user(id int unsigned primary key auto_increment comment 'ID',name varchar(100) comment '姓名',age tinyint unsigned comment '年龄',gender tinyint unsigned comment '性别, 1:男, 2:女',phone varchar(11) comment '手机号'
) comment '用户表';insert into user(id, name, age, gender, phone) VALUES (null,'白眉鹰王',55,'1','18800000000');
insert into user(id, name, age, gender, phone) VALUES (null,'金毛狮王',45,'1','18800000001');
insert into user(id, name, age, gender, phone) VALUES (null,'青翼蝠王',38,'1','18800000002');
insert into user(id, name, age, gender, phone) VALUES (null,'紫衫龙王',42,'2','18800000003');
insert into user(id, name, age, gender, phone) VALUES (null,'光明左使',37,'1','18800000004');
insert into user(id, name, age, gender, phone) VALUES (null,'光明右使',48,'1','18800000005');

 创建实体类:

/** Copyright (c) 2020, 2023,  All rights reserved.**/
package com.example.springbootmybatis.pojo;/*** <p>Project: spring-boot-mybatis - User</p>* <p>Powered by scl On 2023-10-07 10:12:33</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
public class User {private Integer id;private String name;private short age;private short gender;private String phone;@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", gender=" + gender +", phone='" + phone + '\'' +'}';}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 short getAge() {return age;}public void setAge(short age) {this.age = age;}public short getGender() {return gender;}public void setGender(short gender) {this.gender = gender;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public User(Integer id, String name, short age, short gender, String phone) {this.id = id;this.name = name;this.age = age;this.gender = gender;this.phone = phone;}public User() {}
}

2.引入Mybatis的相关依赖,配置Mybatis(数据库连接信息)

 

 配置文件:

#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=

3.编写SQL语句(注解/XML)

编写mapper接口:
package com.example.springbootmybatis.mapper;import com.example.springbootmybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;/*** <p>Project: spring-boot-mybatis - UserMapper</p>* <p>Powered by scl On 2023-10-07 10:17:12</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
@Mapper //在运行时会自动生成实现类对像,并交给ioc容器管理
public interface UserMapper {@Select("select * from user")public List<User> list();
}

编写测试类:

package com.example.springbootmybatis;import com.example.springbootmybatis.mapper.UserMapper;
import com.example.springbootmybatis.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
class SpringBootMybatisApplicationTests {@Autowiredprivate UserMapper userMapper;@Testpublic void testListUser(){List<User> userList=userMapper.list();userList.stream().forEach(user -> {System.out.println(user);});}}

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

相关文章:

  • 【BI看板】Superset2.0+图表二次开发初探
  • 微服务学习--1入门
  • docker系列6:docker安装redis
  • 计算机网络(三):数据链路层
  • 【计算机组成 课程笔记】7.2 DRAM和SRAM
  • 1802_在Linux系统上开发ARM单机片机嵌入式软件
  • 【计算机网络-自顶向下方法】应用层(HTTP、FTP)
  • CSS文本超出显示小数点
  • 怎么把图片压缩小一点?4个简单的压缩办法
  • react嵌套路由
  • 代码随想录 单调栈 Ⅰ
  • C++返回引用
  • 010:连续跌3天,同时这三天收盘价都在20日均线下,第四天上涨的概率--以京泉华为例
  • MATLAB与Python:优势与挑战
  • CSP-J第二轮试题-2019年-1、2题
  • 深入理解 python 虚拟机:原来虚拟机是这么实现闭包的
  • 【数据结构-哈希表 一】【原地哈希】:缺失的第一个正整数
  • 【C++设计模式之迭代器模式】分析及示例
  • 【代码随想录】LC 27. 移除元素
  • crash工具分析dma设备内存踩踏(一)
  • C#上位机——根据命令发送
  • BEVFormer代码跑通
  • kafka安装
  • Mac上安装Java的JDK多版本管理软件jEnv
  • linux常见命令以及jdk,tomcat环境搭建
  • 将表情存入数据库
  • H桥级联型五电平三相逆变器Simulink仿真模型
  • 后端解决跨域(极速版)
  • 数据结构与算法-前缀树
  • DirectX12_Windows_GameDevelop_3:Direct3D的初始化