• springboot整合mybatis入门程序


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

           

     创建数据表

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

     创建实体类

    1. /*
    2. * Copyright (c) 2020, 2023, All rights reserved.
    3. *
    4. */
    5. package com.example.springbootmybatis.pojo;
    6. /**
    7. *

      Project: spring-boot-mybatis - User

    8. *

      Powered by scl On 2023-10-07 10:12:33

    9. *

      描述:

    10. *
    11. * @author 孙臣龙 [1846080280@qq.com]
    12. * @version 1.0
    13. * @since 17
    14. */
    15. public class User {
    16. private Integer id;
    17. private String name;
    18. private short age;
    19. private short gender;
    20. private String phone;
    21. @Override
    22. public String toString() {
    23. return "User{" +
    24. "id=" + id +
    25. ", name='" + name + '\'' +
    26. ", age=" + age +
    27. ", gender=" + gender +
    28. ", phone='" + phone + '\'' +
    29. '}';
    30. }
    31. public Integer getId() {
    32. return id;
    33. }
    34. public void setId(Integer id) {
    35. this.id = id;
    36. }
    37. public String getName() {
    38. return name;
    39. }
    40. public void setName(String name) {
    41. this.name = name;
    42. }
    43. public short getAge() {
    44. return age;
    45. }
    46. public void setAge(short age) {
    47. this.age = age;
    48. }
    49. public short getGender() {
    50. return gender;
    51. }
    52. public void setGender(short gender) {
    53. this.gender = gender;
    54. }
    55. public String getPhone() {
    56. return phone;
    57. }
    58. public void setPhone(String phone) {
    59. this.phone = phone;
    60. }
    61. public User(Integer id, String name, short age, short gender, String phone) {
    62. this.id = id;
    63. this.name = name;
    64. this.age = age;
    65. this.gender = gender;
    66. this.phone = phone;
    67. }
    68. public User() {
    69. }
    70. }

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

     

     配置文件

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

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

    编写mapper接口:
    1. package com.example.springbootmybatis.mapper;
    2. import com.example.springbootmybatis.pojo.User;
    3. import org.apache.ibatis.annotations.Mapper;
    4. import org.apache.ibatis.annotations.Select;
    5. import java.util.List;
    6. /**
    7. *

      Project: spring-boot-mybatis - UserMapper

    8. *

      Powered by scl On 2023-10-07 10:17:12

    9. *

      描述:

    10. *
    11. * @author 孙臣龙 [1846080280@qq.com]
    12. * @version 1.0
    13. * @since 17
    14. */
    15. @Mapper //在运行时会自动生成实现类对像,并交给ioc容器管理
    16. public interface UserMapper {
    17. @Select("select * from user")
    18. public List list();
    19. }

    编写测试类:

    1. package com.example.springbootmybatis;
    2. import com.example.springbootmybatis.mapper.UserMapper;
    3. import com.example.springbootmybatis.pojo.User;
    4. import org.junit.jupiter.api.Test;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.boot.test.context.SpringBootTest;
    7. import java.util.List;
    8. @SpringBootTest
    9. class SpringBootMybatisApplicationTests {
    10. @Autowired
    11. private UserMapper userMapper;
    12. @Test
    13. public void testListUser(){
    14. List userList=userMapper.list();
    15. userList.stream().forEach(user -> {
    16. System.out.println(user);
    17. });
    18. }
    19. }

  • 相关阅读:
    软件测试面试中90%会遇到的问题:“你会搭建测试环境吗?”
    CSS利用定位+margin实现元素居中
    SpringBoot项目整合RabbitMQ
    Mining Association Rules between Sets of Items in Large Databases
    三级网站域名是什么意思?
    虚拟存储器-11.1
    Nginx的概述和配置
    计算机组成原理学习笔记(持续学习中)
    Koa 快速入门
    Composer更新所有依赖包
  • 原文地址:https://blog.csdn.net/qq_64847107/article/details/133634013