Springboot结合Mybatis整合Postgresql
所用原料:Springboot 2.1.3MybatispostgresqlMaven首先是引入相关依赖包<dependencies><!-- 监控程序性能起步依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot
所用原料:
Springboot 2.1.3
Mybatis
postgresql
Maven
首先是引入相关依赖包
<dependencies>
<!-- 监控程序性能起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 测试起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- SpringMVC起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加热部署起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Spring AOP起步依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- jpa依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- postgresql依赖 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.12</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
接下来是application.yml文件配置:采用druid数据源。
server:
port: 8002
spring:
application:
name: pgsql
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: org.postgresql.Driver #pgsql的驱动类
url: jdbc:postgresql://localhost:5432/def?characterEncodeing=utf8 #本地的pgsql连接url
username: postgres #用户名
password: root
max-active: 50
initial-size: 10
max-wait: 60000
min-idle: 6
pool-prepared-statements: true
max-open-prepared-statements: 20
#mapper文件所在包路径
mybatis:
mapper-locations: classpath:mapper/*.xml
然后在项目的启动类上加扫描Mapper类的所在包路径,即是这样:
@SpringBootApplication
@MapperScan(basePackages = "com.jin.mapper.**")
public class PgsqlApplicationStartUp {
public static void main(String[] args) {
SpringApplication.run(PgsqlApplicationStartUp.class, args);
}
}
如果不想加@MapperScan注解,可以在每个Mapper类上加@mapper注解。即是这样:
@Mapper
public interface UserInfoMapper {
UserInfoDO getUserInfoById(int id);
}
接下来就写一个Controller类:
@RestController @RequestMapping(value = "/test") public class UserInfoController { @Autowired private UserInfoService userInfoService; @GetMapping(value = "getUserInfoById") public UserInfoDO getUserInfoById(@RequestParam int id) { UserInfoDO value = userInfoService.getUserInfoById(id); return value; } }
在浏览器上输入:http://localhost:8002/test/getUserInfoById?id=1就可调用该接口了
更多推荐
所有评论(0)