一、新建一个spring-boot项目 demo
项目结构与文件的含义:
二、项目编译入口代码 DemoApplication.java
@SpringBootApplication
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
三、配置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.7.5</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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>6.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
四、连接MySQL数据库的配置
spring:
datasource:
#url: jdbc:mysql://[ip]:[port]/[name]?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
url: jdbc:mysql://localhost:3306/exmaple?serverTimezone=UTC&characterEncoding=UTF-8
username: root
password:
driver-class-name: com.mysql.cj.jdbc.Driver
五、使用JdbcTemplate 获取数据库数据
1、exmaple_userinfo表的数据
2、实现查表获取数据
@Repository
public class ClassInfoBean implements UserInfoDao {
@Autowired
JdbcTemplate jdbcTemplate ;
@Override
public List<Map<String, Object>> userInfo() {
List<Map<String, Object>> list =new ArrayList<>() ;
String sql = "select * from exmaple_userinfo";
List<Map<String, Object>> l = jdbcTemplate.queryForList(sql);
if (l!=null && l.size()>0){
list.addAll(l);
}
return list;
}
}
六、定义一个post请求接口 http:localhost:8000/login/pwd2
/**
* post 对象
*
* @return
*/
@ResponseBody
@PostMapping("/pwd2")
public Map loginByPwd1(@RequestBody PostUserBean postUserBean){
Map map = new HashMap() ;
if (postUserBean == null || postUserBean.getName() == null ||!postUserBean.getName().equals("邱工")){
map.put("msg","请传入{“Name:邱工”}");
} else {
map.put("classInfo",classInfoBean.userInfo());
}
return map;
}
七、使用postman测试接口
八、在编译的时候可能出现端口被占用,那么以下是解决办法的一种 ,当然还有其他方法解决此问题,如直接更换端口号就行了。
1.查看该端口的进程(我的是8080):netstat -ano|findstr 8080
2.kill该进程:taskkill /pid 14376 -f