一、 IDEA快速创建SpringBoot项目
方法 1. File->New->project…->第二块 的Spring Initiallizer, 名称需要改成自己的项目名称
方法 2. https://start.spring.io/ 在网页生成项目jar包然后倒入idea
配置说明
Group: 是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构。
Artifact: 是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。
type: 项目类型
packaging: 打包类型,有jar和war 如果要放在tomcat中运行请选择war ,选择jar则会嵌入tomcat于项目中。
java Version: jdk版本 建议选择1.7及以上
Language: 语言类型
Version: 项目版本通常使用 x.x.x 项目重构增加第一位,功能添加增加第二位,bug修复细微修改增加第三位(本例写法不可取)
Name: 项目名 通常与Artifact相同,影响idea中项目名称显示
Description: 项目描述
Package: 包名 通常与Group相同
开始选这五个即可, Web选择Spring Web; Template Englines勾选Thymeleaf; SQL勾选:MySQL Driver,JDBC API 和 MyBatis Framework,点击next
点击Finish
二、项目结构
1、目录结构
1、可以把一些文件删掉:mvnm、mvnm.md、.mvn
SpringBoot spring-boot-maven-plugin 报红解决方法可以参考: https://editor.csdn.net/md/?articleId=124397001
2、 maven配置这边看到plugins 下面导入有包就表示成功了
2、配置web端口与数据源
(1) 在 application.properties 中配置数据库连接的相关信息
# 修改端口号
server.port=8888
#mysql 4要素
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:///test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
#设置SQL打印日志
logging.level.com.gxsgbd.shouhouserver.mapper = trace
#一般我们会做3个配置,其余默认即可
#暴露session对象的属性
spring.freemarker.expose-session-attributes=true
#配置为传统模式,空值自动处理
spring.freemarker.settings.classic_compatible=true
#重新指定模板文件后缀 springboot 2.2.x 后 默认后缀为 .ftlh
spring.freemarker.suffix=.ftl
(2) mysql提前准备好数据,创建test数据库、创建employee_t表:
CREATE DATABASE test;
USE test;
CREATE TABLE `employee_t` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
INSERT INTO employee_t (id, username, password) VALUES (10, 'hzk', '123456');
INSERT INTO employee_t (id, username, password) VALUES (20, 'hh', '123456');
INSERT INTO employee_t (id, username, password) VALUES (30, 'lrj', '123456');
INSERT INTO employee_t (id, username, password) VALUES (40, 'cq', '123456');
INSERT INTO employee_t (id, username, password) VALUES (50, 'my', '123456');
3、代码文件
1. 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.6.7</version>
<relativePath/>
</parent>
<groupId>com.gxsgbd</groupId>
<artifactId>shouhouServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shouhouServer</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.7</version>
</plugin>
</plugins>
</build>
</project>
可以再导入lombok, json:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
启动类:
package com.gxsgbd.shouhouserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ShouhouServerApplication {
public static void main(String[] args) {
SpringApplication.run(ShouhouServerApplication.class, args);
}
}
2. DAO层实体类 Employee.java
简单的三个字段, ID、员工名字、登录密码,目前我们不需要做注册、删除等业务,就
先往数据库提前插入数据
这里的实体类成员的名字要跟数据库的字段名一样,不然Mapper那边执行sql得出结果,但是传不到service那边,service那边得到的是null的返回值,Orz
package com.gxsgbd.shouhouserver.dao;
import lombok.Data;
@Data
public class Employee {
private Long id;
private String account;
private String username;
private String password;
}
3、创建和数据库交互联系的映射关系类
@Mapper表明该类是一个Mapper接口,使用@Select、@Insert等注解我们可以直接在类中书写sql语句来实现我们的目的
package com.gxsgbd.shouhouserver.mapper;
import com.gxsgbd.shouhouserver.dao.Employee;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface EmployeeMapper {
@Select(" select * from employee_t where id=#{id}")
Employee getEmployeeById(Long id);
@Select("select * from employee_t")
List<Employee> getAllEmployees();
}
4、创建业务接口和实现类
1、接口类:
package com.gxsgbd.shouhouserver.service;
import com.gxsgbd.shouhouserver.dao.Employee;
import java.util.List;
public interface IShouhouService {
Employee getEmployeeById(Long id);
List<Employee> getAllEmployees();
}
2、实现类: @Service注解表明它是一个服务类Bean,可以被SpringBoot识别使用,相当于以前在xml里配置的bean。
package com.gxsgbd.shouhouserver.service;
import com.gxsgbd.shouhouserver.dao.Employee;
import com.gxsgbd.shouhouserver.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ShouhouServiceImpl implements IShouhouService {
@Autowired
private EmployeeMapper m_EmployeeMapper;
@Override
public Employee getEmployeeById(Long id) {
return m_EmployeeMapper.getEmployeeById(id);
}
@Override
public List<Employee> getAllEmployees() {
return m_EmployeeMapper.getAllEmployees();
}
}
5、创建控制器类
package com.gxsgbd.shouhouserver.controller;
import com.gxsgbd.shouhouserver.dao.Employee;
import com.gxsgbd.shouhouserver.service.IShouhouService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class SgbdShouhouLoginController {
@Autowired
private IShouhouService m_ShouhouService;
@GetMapping("/testapi")
public String test(){
return "JustTestingApi";
}
@RequestMapping(value = "/all")
public List<Employee> getAllEmployees() {
List<Employee> all_employees = m_ShouhouService.getAllEmployees();
return all_employees;
}
}
三、测试运行
右键 ShouhouServerApplication.java 运行
浏览器输入 http://127.0.0.1:8888/testapi
|