1.创建项目
New Project,选择Spring Initializr
输入基本信息
选择web,然后一路next下去,中间有设置项目路径的,想改就改
然后把结构稍微调整下,改一改
配置端口号,在application.yml里
#配置端口号
server:
port: 8080
然后在controller层里建个HelloController,加点代码测试下
IDEA直接run,或者右击启动类run(项目创建时候里面代码默认就有了,直接跑)
成功
2.简单使用
@Value注解可以直接从配置文件里获取值
@Value("${name}")
private String name;
@Value("${age}")
private Integer age;
@Value("${content}")
private String content;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String say() {
return content;
}
下面是直接输出content的
2.实体类获取配置文件值
创建Student实体类
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
在pom.xml文件配置加上,这个是防止实体类里报错的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
没加的话会这样
@Component 用于把对象注册成bean
@ConfigurationProperties(prefix = "student") 用于获取配置文件里对象叫student的值
3. @RequestMapping 和 @PathVariable 的作用,在URL地址栏中输入id,然后自己获取此id并且在浏览器上打印出来。
@RequestMapping(value = "/num/{id}", method = RequestMethod.GET)
public String num(@PathVariable("id") Integer id) {
return "id = " + id;
}
运行
4.通过 ? 来获取URL值,配合 @RequestParam 注解使用
@RequestMapping 可以用 @GetMapping 和 @PostMapping来代替
@GetMapping(value = "/num2")
public String num2(@RequestParam(value = "id", defaultValue = "0", required = false) Integer id) {
return "id = " + id;
}
3.连接数据库
application.yml中加入配置
#配置数据库
spring:
profiles:
active: a
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
username: root
password: root
#配置xml映射
mybatis:
mapper-locations: mapper/*.xml
sql
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `student` VALUES ('1', 'Lin', '24');
INSERT INTO `student` VALUES ('2', 'you', '23');
pom文件引入SQL的包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
启动器加入注解扫描
@ComponentScan 用于扫描 Controller层,service层
@MapperScan 用于扫描mapper层
在使用 @MapperScan 之前需要在pom引入
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
controller层:
@GetMapping(value = "/getStudent")
public List<Student> getStudent() {
return helloService.getStudent();
}
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.springboot.test.mapper.HelloRepository">
<select id="getStudent" resultType="com.springboot.test.entity.Student">
select * from student
</select>
</mapper>
运行:
下面是所有文件代码:
Student.java:
package com.springboot.test.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
HelloController.java:
package com.springboot.test.controller;
import com.springboot.test.entity.Student;
import com.springboot.test.service.HelloService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class HelloController {
@Resource
private Student student;
@Resource
private HelloService helloService;
@Value("${name}")
private String name;
@Value("${age}")
private Integer age;
@Value("${content}")
private String content;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String say() {
return content;
}
@RequestMapping(value = "/student", method = RequestMethod.GET)
public String stu() {
return student.getName();
}
@RequestMapping(value = "/num/{id}", method = RequestMethod.GET)
public String num(@PathVariable("id") Integer id) {
return "id = " + id;
}
@GetMapping(value = "/num2")
public String num2(@RequestParam(value = "id", defaultValue = "0", required = false) Integer id) {
return "id = " + id;
}
@GetMapping(value = "/getStudent")
public List<Student> getStudent() {
return helloService.getStudent();
}
}
HelloService.java:
package com.springboot.test.service;
import com.springboot.test.entity.Student;
import java.util.List;
public interface HelloService {
List<Student> getStudent();
}
HelloServiceImpl.java:
package com.springboot.test.service.impl;
import com.springboot.test.mapper.HelloRepository;
import com.springboot.test.entity.Student;
import com.springboot.test.service.HelloService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("HelloService")
public class HelloServiceImpl implements HelloService {
@Resource
private HelloRepository helloRepository;
@Override
public List<Student> getStudent() {
return helloRepository.getStudent();
}
}
HelloMapper.java:
package com.springboot.test.mapper;
import com.springboot.test.entity.Student;
import java.util.List;
public interface HelloMapper {
List<Student> getStudent();
}
HelloMapper.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.springboot.test.mapper.HelloMapper">
<select id="getStudent" resultType="com.springboot.test.entity.Student">
select * from student
</select>
</mapper>
Application.java:
package com.springboot.test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = {"com.springboot.test.*"})
@MapperScan("com.springboot.test.mapper")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.yml:
#配置端口号
server:
port: 8080
#配置数据库
spring:
profiles:
active: a
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
username: root
password: root
#配置xml映射
mybatis:
mapper-locations: mapper/*.xml
name: Jack
age: 18
content: "name: ${name}, age: ${age}"
student:
name: Michael
age: 20
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 http://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.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>