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

JavaWeb学习-Mybatis(增删改查)

(一)Mybatis入门程序

1.创建springboot工程,并导入 mybatis的起步依赖、mysql的驱动包。(项目工程创建完成后,自动在pom.xml文件中,导入Mybatis依赖和MySQL驱动依赖)

<dependencies>

        <!-- mybatis起步依赖 -->

        <dependency>

            <groupId>org.mybatis.spring.boot</groupId>

            <artifactId>mybatis-spring-boot-starter</artifactId>

            <version>2.3.0</version>

        </dependency>

        <!-- mysql驱动包依赖 -->

        <dependency>

            <groupId>com.mysql</groupId>

            <artifactId>mysql-connector-j</artifactId>

            <scope>runtime</scope>

        </dependency>

       

        <!-- spring单元测试 (集成了junit) -->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

</dependencies>

2. 准备数据(Mysql):创建用户表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');

3. 在Idea中创建实体类:实体类的属性名与表中的字段名一一对应。

public class User {

    private Integer id;   //id(主键)

    private String name;  //姓名

    private Short age;    //年龄

    private Short gender; //性别

    private String phone; //手机号

   

    //省略GET, SET方法

}

4. 在之前使用图形化客户端工具,连接MySQL数据库时,需要配置参数:

 连接数据库的四大参数:

 - MySQL驱动类

 - 登录名

 - 密码

 - 数据库连接字符串

在springboot项目中,可以编写application.properties文件,配置数据库连接信息。我们要连接数据库,就需要配置数据库连接的基本信息,包括:driver-class-name、url 、username,password。

application.properties:

#驱动类名称

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=1234

 5.在创建出来的springboot工程中,在引导类所在包下,在创建一个包 mapper。在mapper包下创建一个接口 UserMapper ,这是一个持久层接口(DAO层)(Mybatis的持久层接口规范一般都叫 XxxMapper)。

import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
//@Mapper注解:表示是mybatis中的Mapper接口,程序运行时࿱

http://www.kler.cn/a/543458.html

相关文章:

  • gcc和g++的区别以及明明函数有定义为何链接找不到
  • C语言:目标文件和可执行文件里面都有什么?
  • 云服务器流量包用尽(中病毒)
  • 视频理解新篇章:Mamba模型的探索与应用
  • 深度学习入门--python入门1
  • 从MySQL优化到脑力健康:技术人与效率的双重提升
  • Windows 软件奔溃-dmp文件分析
  • 微信小程序网络请求封装
  • 【JavaEE进阶】Spring IoC
  • 【漫话机器学习系列】088.常见的输出层激活函数(Common Output Layer Activation Functions)
  • 堆、方法区、虚拟机栈、本地方法栈 和 程序计数器
  • HCIA项目实践--RIP相关原理知识面试问题总结回答
  • 从 0 开始本地部署 DeepSeek:详细步骤 + 避坑指南 + 构建可视化(安装在D盘)
  • Oracle数据库ADG日志丢失处理方法
  • js实现深拷贝
  • HDL Compiler:工具简介
  • C++病毒(^_^|)(2)
  • 多机器人系统的大语言模型:综述
  • es-head 正则查询和标准正则查询的差异
  • flutter 中 ReceivePort 的 first 和 listen
  • Nginx 中的HTTP2
  • 网络安全ids是什么意思
  • 【C++】26.unordered_map和unordered_set的使用
  • 代码随想录算法【Day43】
  • 面试真题 | B站C++渲染引擎
  • DeepSeek元学习(Meta-Learning)基础与实践