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接口,程序运行时࿱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半截詩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值