springboot gradle mybatis mysql配置(注解)
时间:2022-07-25
本文章向大家介绍springboot gradle mybatis mysql配置(注解),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
package com.future.finance;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@MapperScan("com.future.finance.mapper")
public class FinanceApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(FinanceApplication.class, args);
}
}
package com.future.finance.entity;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.util.Date;
@Getter
@Setter
public class Information implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String content;
private Date publishDate;
}
package com.future.finance.mapper;
import com.future.finance.entity.Information;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface InformationMapper {
@Select("select * from information")
@Results({
@Result(property = "content", column = "content"),
@Result(property = "publishDate", column = "publish_date")
})
List<Information> getAll();
}
package com.future.finance.controller;
import com.future.finance.entity.Information;
import com.future.finance.mapper.InformationMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class InformationController {
@Autowired
private InformationMapper informationMapper;
private Gson gson = new Gson();
@RequestMapping("/getAll")
public String getAll() {
List<Information> result = informationMapper.getAll();
return gson.toJson(result);
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/finance?characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8
spring.datasource.username=jack
spring.datasource.password=jack123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
build.gradle
plugins {
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.future'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
// 1. Use Guava in your implementation only:
implementation 'com.google.guava:guava:29.0-jre'
implementation 'com.google.code.gson:gson:2.8.5'
compile("org.springframework.boot:spring-boot-devtools") // 热部署
compileOnly 'org.projectlombok:lombok:1.18.12'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
runtimeOnly 'mysql:mysql-connector-java:8.0.17'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
// debug
//bootRun {
// jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
//}
然后postman访问
localhost:8080/getAll
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解
- 记录一次有意思的溯源
- 个人博客图片的解决方案
- types 和 @types 是什么?
- 一次绕过waf进行xss的经历
- opencv+python制作硬核七夕礼物
- 身份验证器是如何验证我们的身份?
- 谷歌开源NLP模型可视化工具LIT,模型训练不再「黑箱」
- MongoDB 案例:Document failed validation 错误
- 利用GoogleAppsScript自动回复短信实现保号
- 用php来查询graphql
- 利用树莓派的摄像模块实现“扫码枪”
- n ../../node_modules/@storybook/channels/dist/index.d.ts:25:9 - error TS1086: An accessor cannot ...
- 要不来重新认识Spring事务?三歪又学到了
- 读者问:学完SSM,该学什么呢?
- go-zero 微服务框架介绍