Eclipse Springboot 入门(一)——环境搭建以及连接mysql
(1)数据库建表,工具workbench
如果使用远程数据库方法云服务器端打开数据库远程访问
可以用workbench远程登录查看。
(2)springboot实现
springboot配置pom.xml(依赖)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<!-- mp 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
apllication.yml连接mysql数据库配置
如果使用云数据库将127.0.0.1改为云服务器的ip
springboot 2.x以后
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test_db?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: root
springboot 2 .x以前
spring:
datasource:
driver-class-name: com.mrootysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test_db
username: root
password: root
driver-class-name:数据库驱动名
创建实体类(User.java)
ps.类名得和数据库表名对应
package com.example.demo.entity;
import lombok.Data;
@Data
//定义getID(),setID(),这里导入lombok进行简化
public class User {
private long id;
private String username;
private String password;
//以下内容 因为引入了lombox框架而不需要再添加成员变量的get/set方法。
// public String getId() {
// return id;
// }
//
// public void setId(String id) {
// this.id = id;
// }
// public String getUsername() {
// return username;
// }
//
// public void setUsername(String username) {
// this.username = username;
// }
//
// public String getPassword() {
// return password;
// }
//
// public void setPassword(String password) {
// this.password = password;
// }
}
创建mapper类(UserMapper.java)
package com.example.demo.mapper;
import org.springframework.stereotype.Repository;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
启动类(DemoApplication.java)
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//使启动的时候扫描到mapper类,否则报错
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
测试接口
package com.example.demo;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
@SpringBootTest
class UserMapperTests{
@Autowired
private UserMapper userMapper;
@Test
public void findAll() {
//调用selectList
List<User> users = userMapper.selectList(null);
System.out.println(users);
}
}
测试结果
运行可以查找出数据库中的数据,简单的数据库查找可以实现。