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
- 【AngularJS】—— 5 表单
- 【AngularJS】—— 1 初识AngularJs
- 【AngularJS】—— 2 初识AngularJs(续)
- 【AngularJS】—— 3 我的第一个AngularJS小程序
- 【Spring实战】—— 2 构造注入
- 共享单车运维“朋友圈”越来越宽,乱停乱放现象有望改善吗?
- 【Spring实战】—— 15 Spring JDBC模板使用
- 前端开发总览
- 【Spring实战】—— 16 基于JDBC持久化的事务管理
- 【Spring实战】—— 4 Spring中bean的init和destroy方法讲解
- 基于AngularJS的过滤与排序
- 【Spring实战】—— 5 设值注入
- 科学家预测:未来100万年人类将变成半机械人类
- 【Spring实战】—— 8 自动装配
- 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 实例讲解
- 设计模式(4)[JS版]-JavaScript如何实现建造者模式?
- 前端基础:HTML
- SignalR 2.x入门(一):SignalR简单例子
- 设计模式(5)[JS版]-JavaScript如何实现工厂方法模式?
- 前端基础:CSS
- 实现一个分布式调用链路追踪Java探针你可能会遇到的问题
- 设计模式(6)[JS版]-JavaScript如何实现抽象工厂模式?
- 前端基础:JavaScript
- 设计模式(7)[JS版]-JavaScript设计模式之原型模式如何实现???
- SignalR 2.x入门(二):SignalR在MVC5中的使用
- SignalR简介
- 前端基础:jQuery
- Web API--入门--(一)ASP.NET Web API 2(C#)入门
- C# 关键字之 decimal
- 设计模式(8)[JS版]-JavaScript设计模式之如何实现适配器模式???