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

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.lryc.cn/news/535390.html

相关文章:

  • 软考高项(二十四)法律法规和标准规范 ★重点集萃★
  • Django中select_related 的作用
  • vscode无法ssh连接远程机器解决方案
  • 计算机组成原理——中央处理器(九)
  • 网页版贪吃蛇小游戏开发HTML实现附源码!
  • 基于java ssm springboot选课推荐交流平台系统设计和实现
  • Sigma-Aldrich化学品安全技术说明书(SDS)查询教程
  • 嵌入式实训室解决方案(2025年最新版)
  • Spring Cloud — 深入了解Eureka、Ribbon及Feign
  • 全排列(力扣46)
  • Mac部署Jenkins 一
  • 附录1:组维英文简写大全
  • SQL Server:查看内存使用情况
  • chrome-mojo C++ Bindings API
  • uniapp + vite + 使用多个 ui 库
  • Unity3D 制作动画的时间轴管理方案: Timeline编
  • 逻辑回归不能解决非线性问题,而svm可以解决
  • Prompt通用技巧1
  • C# 上位机--枚举
  • 01docker run
  • 易语言.飞扬特性展示2
  • FlashDecoding
  • 提示词生成新方法,用Make自动化生成
  • 每日一题——括号生成
  • 实操部署DeepSeek,添加私有知识库
  • 宜宾数字经济新标杆:树莓集团赋能区域产业转型升级
  • 8.大规模推荐系统的实现
  • 第三届通信网络与机器学习国际学术会议(CNML 2025)
  • MySQL两阶段提交策略
  • uniapp商城之购物车模块