SpringBoot-Select
SpringBoot实现的查询数据库操作
(点击这里跳转到页面查看)
工具及环境
eclipse j2ee 2019、apache-maven-3.6.1、jdk1.8、Navicat for mysql 12、postman。
配置Maven环境
下载apache-maven-3.6.1压缩包,解压到某个文件夹,右键计算机-属性-高级系统设置-环境变量中新建MAVEN_HOME,设置值为maven解压的目录路径,编辑Path变量,新增%MAVEN_HOME%\bin。打开cmd.exe,输入命令mvn –version检测是否安装成功,出现Maven版本号字样则表示成功。
创建数据库
在Navicat中的test数据库新建test表并设计如下,其中id属性选择为自增:
并添加数据如下:
创建项目
Eclipse中创建Maven Project,输入相应的包名及项目名,这里的项目名是Select,包名为com,创建好的项目结构如下:
接着使用如下代码替换pom.xml里面的内容:
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">
4.0.0
1.0.0
dms-get-ecall
0.0.1-SNAPSHOT
jar
springboot-restful
http://maven.apache.org
UTF-8
1.7
1.2.0
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-starter-data-jpa
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis-spring-boot}
mysql
mysql-connector-java
org.springframework.boot
spring-boot-maven-plugin
true
在src/main下新建resources文件夹,新建application.properties文件,将如下内容复制:
banner.charset=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8
server.port=8083
spring.datasource.url=jdbc:mysql://数据库地址:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
将com.Select下的App.java代码更新如下:
package com.Select;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
System.out.println( "Hello World!" );
}
}
创建项目
entity层
完成之后右键项目,选择Maven/Update Projects更新项目。
接下来在src/main/java下新建entity包,创建Info实体类,该实体类封装的变量对应test数据库表中的属性,代码如下:
package com.Select.entity;
public class Info {
private int id;
private String name;
private String sex;
private String birthday;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
继续在entity包下创建InfoResult类,作用是将Info类的参数封装成列表形式,代码如下:
package com.Select.entity;
import java.util.ArrayList;
public class InfoResult {
private ArrayList info;
public ArrayList getInfo() {
return info;
}
public void setInfo(ArrayList info) {
this.info = info;
}
}
dao层
在src/main/java下新建dao包,新建InfoDao接口,实现对数据库的查询操作:
package com.Select.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.Select.entity.Info;
@Mapper
public interface InfoDao {
@Select("select\t\n"+
"id,\t\n"+
"name,\t\n"+
"sex,\t\n"+
"birthday,\t\n"+
"address\t\n"+
"from\t\n"+
"test\t\n"
)
List selectInfo();
}
service层
在src/main/java下新建service包,新建InfoService接口,添加要用到的方法:
package com.Select.service;
import com.Select.entity.InfoResult;
public interface InfoService {
InfoResult selectInfoResult();
}
在src/main/java下新建serviceimpl包,新建InfoServiceImpl类,重写InfoService中声明的方法:
package com.Select.serviceimpl;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.Select.dao.InfoDao;
import com.Select.entity.Info;
import com.Select.entity.InfoResult;
import com.Select.service.InfoService;
@Service
public class InfoServiceImpl implements InfoService{
@Autowired
private InfoDao infoDao;
@Override
public InfoResult selectInfoResult() {
InfoResult infoResult = new InfoResult();
infoResult.setInfo((ArrayList) infoDao.selectInfo());
return infoResult;
}
}
controller层
在src/main/java下新建controller包,新建InfoController类来处理请求返回数据:
package com.Select.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.Select.entity.InfoResult;
import com.Select.service.InfoService;
@RestController
@RequestMapping(value="/api")
public class InfoController {
@Autowired
private InfoService infoService;
@PostMapping("/select")
InfoResult selInfoResult() {
return infoService.selectInfoResult();
}
}
至此,整个项目已搭建完毕,整体的框架如下:
然后就可以运行程序了:右键App.java,选择运行方式-Java应用程序,如果不出意外控制台会出现Hello World!,然后打开postman设置如下信息:
点击send之后就可以成功获取到test数据库表的所有数据:
OVER!