springboot集成 mysql快速入门demo

一、mysql环境搭建 

采用docker-compose搭建,配置如下:

docker-compose.yml

version: '3'
services:
  mysql:
    image: registry.cn-hangzhou.aliyuncs.com/zhengqing/mysql:5.7  # 原镜像`mysql:5.7`
    container_name: mysql_3306                                    # 容器名为'mysql_3306'
    restart: unless-stopped                                       # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
    volumes:                                                      # 数据卷挂载路径设置,将本机目录映射到容器目录
      - "./mysql/my.cnf:/etc/mysql/my.cnf"
      - "./mysql/init-file.sql:/etc/mysql/init-file.sql"
      - "./mysql/data:/var/lib/mysql"
#      - "./mysql/conf.d:/etc/mysql/conf.d"
      - "./mysql/log/mysql/error.log:/var/log/mysql/error.log"
      - "./mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d" # 可执行初始化sql脚本的目录 -- tips:`/var/lib/mysql`目录下无数据的时候才会执行(即第一次启动的时候才会执行)
    environment:                        # 设置环境变量,相当于docker run命令中的-e
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
      MYSQL_ROOT_PASSWORD: root         # 设置root用户密码
      MYSQL_DATABASE: demo              # 初始化的数据库名称
    ports:                              # 映射端口
      - "3306:3306"

启动mysql容器

docker-compose -f docker-compose.yml -p mysql5.7 up -d

创建user表和插入测试数据

CREATE TABLE `user` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(100) DEFAULT NULL,
 `age` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCRE
INSERT INTO demo.`user`(id, name, age)VALUES(1, 'jack', 18);
INSERT INTO demo.`user`(id, name, age)VALUES(2, 'alyssa', 19);

二、代码工程搭建

2.1 pom.xml



    
        springboot-demo
        com.et
        1.0-SNAPSHOT
    
    4.0.0


    mysql


    
        8
        8
    
    


        
            org.springframework.boot
            spring-boot-starter-web
        


        
            org.springframework.boot
            spring-boot-autoconfigure
        
        
        
            mysql
            mysql-connector-java
            8.0.29
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.1
        
        
            org.projectlombok
            lombok
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        


    

2.2 entity

package com.et.mysql.entity;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
public class UserPO {
    @TableId(value = "id",type = IdType.AUTO)
    private int id;
    @TableField("name")
    private String name;
    @TableField("age")
    private int age;
}

2.3 mapper

package com.et.mysql.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.et.mysql.entity.UserPO;
import org.springframework.stereotype.Repository;


@Repository
public interface UserMapper extends BaseMapper {
}

2.4 application.yaml

server:
  port: 8088


spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root

2.5 验证服务

package com.et.mysql;


import com.et.mysql.entity.UserPO;
import com.et.mysql.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class MysqlTests {
    @Autowired
    private UserMapper userMapper;




    @Test
    public void testmysql(){
        for (UserPO userPO : userMapper.selectList(null)) {
            System.out.println(userPO.toString());
        }
    }
}

结果如预期一样

2024-01-26 13:29:22.950 INFO 30100 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2024-01-26 13:29:23.160 INFO 30100 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
UserPO(id=1, name=jack, age=18)
UserPO(id=2, name=alyssa, age=19)

三、测试代码仓库

  • https://github.com/Harries/springboot-demo/

四、参考资料

  • https://spring.io/projects/spring-data-jdbc/#overview

springboot集成 mysql快速入门demo_第1张图片

你可能感兴趣的:(spring,boot,mysql,后端,java,spring)