填写Name、Group 、package name和 Artifact 信息,选择对应的开发语言,打包方式,Java 版本等 ,点击 Next;
选择需要用到的依赖 和 Spring Boot 版本号,点击Finish;
创建成功
pom.xml
<dependencies>
<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>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置文件application.yml
数据库配置文件
server:
port: 8080
#配置数据源
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/zz2202
username: root
password: root
#连接池配置
initial-size: 6
min-idle: 7
max-active: 20
max-wait: 20000
controller
@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping("/hello")
public List<Employee> getAll(){
return employeeService.getAll();
}
}
pojo
@Data
public class Employee {
private int id;
private String ename;
private int age;
private String address;
private int salary;
}
service
public interface EmployeeService {
List<Employee> getAll();
}
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
public List<Employee> getAll() {
return employeeMapper.getAll();
}
}
mapper
@Mapper
public interface EmployeeMapper {
List<Employee> getAll();
}
EmployeeMapper.xml
需要在pom.xml中添加
<build> <resources> <!-- mapper.xml文件在java目录下 --> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>才能扫描到mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.wrs.mapper.EmployeeMapper">
<select id="getAll" resultType="com.wrs.pojo.Employee">
select * from employee
</select>
</mapper>
启动器
@SpringBootApplication
public class Springboot00Application {
public static void main(String[] args) {
SpringApplication.run(Springboot00Application.class, args);
}
}
使用maven创建springboot项目时需要在pom.xml中添加
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.6.10</version>
</parent>
访问地址:localhost:8080/hell