1.????????数据库? ? ?数据库名:brog_database? ?表名:comments? 字段看下图
2.????????留言板项目目录:? springboot+mysql+idea+postman
3.????????com.oy.demo.controller? ?MessageController.java
package com.oy.demo.controller;
import com.oy.demo.pojo.Message;
import com.oy.demo.service.MessageService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("message")
public class MessageController {
@Resource
private MessageService messageService;
/**
* 找到所有留言信息
* @return
*/
@PostMapping("find")
public List<Message> findAllMessage(){
return messageService.findAllMessage();
}
/**
* 发送一条留言信息
* @param message
* @return
*/
@PostMapping("add")
public Map<String, Object> addMessage(@RequestBody Message message){
// System.out.println("============================"+message);
return messageService.addMessage(message);
}
}
?
4.????????com.oy.demo.controller? ?UiController.java
package com.oy.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class UiController {
@GetMapping("/message")
public ModelAndView message(){
return new ModelAndView("message.html");
}
}
5.????????com.oy.demo.mapper? MessageMapper接口
package com.oy.demo.mapper;
import com.oy.demo.pojo.Message;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface MessageMapper {
/**
* 查询所有留言信息
* @return
*/
@Select("SELECT * FROM comments")
List<Message> selectAllMessage();
/**
* 新增一条留言信息
* @param message
* @return
*/
@Insert("INSERT INTO comments(name,iphone, words,time)"+
"VALUES(#{name}, #{iphone}, #{words}, #{time})")
int insertMessage(Message message);
}
6.????????com.oy.demo.pojo? Message.java
package com.oy.demo.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor //无参数的构建方法
@AllArgsConstructor //有参数的构建方法
public class Message {
private Integer id; //留言编号
private String name; //用户大名
private String iphone; // 手机号码
private String words; //留言内容
private LocalDateTime time; //留言时间
}
7.? ? ? com.oy.demo.service? ? MessageService.java
package com.oy.demo.service;
import com.oy.demo.mapper.MessageMapper;
import com.oy.demo.pojo.Message;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service //Service类的注解
public class MessageService {
@Resource //注入MessageMapper接口
private MessageMapper messageMapper;
/**
* 查找所有留言信息
* @return
*/
public List<Message> findAllMessage(){
//查找所有留言信息
List<Message> messageList = messageMapper.selectAllMessage();
//测试
for(Message message:messageList){
System.out.println(message.toString());
}
//返回留言信息
return messageList;
}
/**
* 插入留言信息
* @param message
* @return
*/
public Map<String, Object> addMessage(Message message){
Map<String, Object> resultMap = new HashMap<>();
System.out.println(message.toString());
//补充插入信息
message.setTime(LocalDateTime.now());
//插入留言信息
int result = messageMapper.insertMessage(message);
if(result > 0){
//留言信息插入成功
resultMap.put("code" ,200);
resultMap.put("message" , "信息发送成功!");
}else{
//留言信息插入失败
resultMap.put("code" ,400);
resultMap.put("message" , "!信息发送失败");
}
return resultMap;
}
}
8.? ? ?com.oy.demo.service? ? MessageService.java
package com.oy.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.oy.demo.mapper")
@SpringBootApplication
public class SpringbootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTestApplication.class, args);
}
}
9.? ?application.yml
server:
port: 9999
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/brog_database?characterEncoding=UTF8&useUnicode=true&useSSL=false
username: root
password: HDjava1702
mybatis:
configuration:
map-underscore-to-camel-case: true # 开启驼峰映射
10.? 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.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.4.RELEASE</version>
</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>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.2</version>
</plugin>
</plugins>
</build>
</project>
11?.postman测试结果
?
?
|