用idea配置springboot+mybatis连接postersql数据库
从socket开始,我们就要开始部署前后端的交互了,所以今天带来一份热度比较高的框架springboot,并教大家如何连接数据库。
框架
先给大家看一下目录结构,因为有些需要调用文件路径:
创建项目:
新版本可以直接创建springboot框架,我这里用的是2023年的版本,更符合大众用户的方式
服务器建议改为阿里云的,否则加载会比较慢:https://start.aliyun.com
添加这五个依赖项后直接创建就可以了。
添加配置依赖:
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><groupId>com.example</groupId><artifactId>threes</artifactId><version>0.0.1-SNAPSHOT</version><name>threes</name><description>threes</description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.13</spring-boot.version></properties><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.1.4</version></dependency><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><version>42.6.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></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><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><configuration><mainClass>com.example.threes.ThreesApplication</mainClass><skip>true</skip></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
点击同步依赖即可。
application.properties的配置:
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres//这里端口默认为5432,如果你安装时修改了,以你的为准
spring.datasource.username=postgres//默认为postgres,如果修改,还是以你的为准
spring.datasource.password=你的数据库密码
spring.datasource.driver-class-name=org.postgresql.Driver
这是我的配置,上面部分是自带的,端口我当时安装时修改了,做完这些,配置就配好了。
主题代码:
FeatureLayer类(这里的命名最好以你要连接的表的名称命名),申明你表里的属性。
package com.example.threes;public class FeatureLayer {private int gid;private String name;private String type;private String beizhu;public int getId(){return gid;}public void setIds(int id) {this.gid = gid;}public String getName(){return name;}public void setName(String name) {this.name = name;}public String getType(){return type;}public void setType(String type) {this.type = type;}public String getBeizhu(){return beizhu;}public void setBeizhu(String type) {this.beizhu = beizhu;}}
FeatureLayerDao接口
package com.example.threes;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.example.threes.FeatureLayer;@Mapper
public interface FeatureLayerDao {List<FeatureLayer> selectAllFeatures() ;}
FeatureLayerService类:
package com.example.threes;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.threes.FeatureLayer;import java.util.List;@Service
public class FeatureLayerService {@Autowiredprivate FeatureLayerDao featlyrDao;public List<FeatureLayer> queryAllFeatures(){return featlyrDao.selectAllFeatures();}}
LayerController类:
package com.example.threes;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("/threes")
public class LayerController {@Autowiredprivate FeatureLayerService featLyrService;@GetMapping("/query/all")public List<FeatureLayer> queryAllFeatures() {return featLyrService.queryAllFeatures();}
}
ThreesApplication主题类,这部分应该时自己就创建好的,但是要修改一点:
package com.example.threes;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.example.threes")public class ThreesApplication {public static void main(String[] args) {SpringApplication.run(ThreesApplication.class, args);}}
在resource下创建mapper文件夹,并创建featurelayer.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.example.threes.FeatureLayerDao"><select id="selectAllFeatures" resultType="com.example.threes.FeatureLayer">select * from featurelayer</select></mapper>
select*from后的表要给你的数据库中的表的名称要完全一致,大小写都不能错
select id的名称也不能错,这里在FeatureLayerDao接口中
namespace的文件路径也不能错
配置后直接运行就可以了:
这里的threes替换为你的项目名称,query/all这个路径是你在LayerController 中设定的:
报错说明
如果这里有问题,说明的xml中的配置有问题
请检查这几部分的代码是否有问题,xml中的名称和路径是否没有差错,这个问题通常是这里有问题。
如果出现了这样的错误,通常是找不到对应的数据库:
请检查这里的数据库是否能在你的postersql中找的。
有时需要你在idea内部连接数据库,步骤如下:
点击右侧的数据库,点击+,数据源为postersql,输入密码和用户,保证端口一致就行,然后刷新框架即可。
尾言
本次的文章以后端为主,之后我会推出Android+spring boot+postersql的综合项目教程的,敬请期待。