Use idea to quickly build springboot to configure MySQL connection
The idea is as follows:
1. build.gradle==> configure system import package
2. application.priperties==> configure connection mysql database information
3. bean=> define data type
4, mapper==> define interface type
5, controller== >Call the interface, return to page
6, html==>page setting
Construct
idea–>file–>objdec–new Project–>spring Initializr–>View the following figure operation–>If there is no figure, then the next step
Use spring Initialiar to automatically build the project, use SDK1.8 to create the
configuration group and name (optional),
select the build method: Gradle Project,
select the information for project initialization
File: build.gradle
plugins {
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.spboot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
//加载的项目包文件
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
Two, connect to mysql configuration
The file structure after all construction is as follows:
Step 2, File: application.properties
#配置端口
server.port=8080
#配置数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://192.168.188.129:3307/mybatis?useSSL=false
Step 3 Data type: Department
package com.spboot.spbuildmysql.bean;
public class Department {
//定义数据类型,设定set,get方法
private Integer id;
private String departmentName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
Step 4 Define the interface: DepartmentMapper
package com.spboot.spbuildmysql.mapper;
import com.spboot.spbuildmysql.bean.Department;
import org.apache.ibatis.annotations.*;
@Mapper //扫描接口
public interface DepartmentMapper {
@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);
}
Step 5 Call the interface and return to the page. DepartController
package com.spboot.spbuildmysql.controller;
import com.spboot.spbuildmysql.bean.Department;
import com.spboot.spbuildmysql.mapper.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController //注解后可以使用浏览器直接访问。
public class DepartController {
//自动注入,接口导入
@Autowired
DepartmentMapper departmentMapper;
//构建返回页面,使用浏览器打开
@GetMapping("/depart/{id}")
public Department getDepartment(@PathVariable Integer id){
return departmentMapper.getDeptById(id);
}
}
Step 6 Page settings (omitted)
Use a browser to open the website: localhost:8080/depart/1
to read data from the database, and the page returns information