使用IDEA 搭建一个 SpringBoot + Hibernate + Gradle 项目
现在创建个项目:


勾上 自已 需要东西。(这里作为演示)



maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
关闭项目,重新打开。

等待,依赖下载完成。

在 templates 文件夹 中 加入 一个 index.html 的文件


到这里,还要配置一下 数据库连接(刚刚加了 jpa),我这里作为演示使用的是 Mariadb数据库

增加,依赖...

implementation('org.springframework.boot:spring-boot-starter-actuator')
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-web')
testImplementation('org.springframework.boot:spring-boot-starter-test')
implementation('org.springframework.boot:spring-boot-starter-thymeleaf')//视图引擎
compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.3.0'

# 配置 Tomcat 端口号
server.port=8881
# 数据库驱动
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
# 连接数据库
spring.datasource.url=jdbc:mariadb://localhost:3306/test
# 用户名
spring.datasource.username=oukele
# 密码
spring.datasource.password=oukele # 要标注它是那个一个数据库,如果不标注它,它会使用MySQL的,因为我们是创建MySQL数据
spring.jpa.database-platform=org.hibernate.dialect.MariaDB102Dialect
在 templates 文件夹 中 新建一个 index.html 页面

然后,启动。。

启动,成功


OK啦。
现在,我们去访问数据库,拿到数据吧。
项目结构:

entity包中的User 类
package com.oukele.springboot.springboot_demo2.entity; import javax.persistence.*; @Entity
@Table(name = "user")//数据库的表名
public class User { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//自动增长主键
private int id; @Column(name = "username")//数据库的字段名,数据库 不区分大小写 这个 要注意
private String name; private String password; 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 getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}
dao 包中的 UserMapper 接口
package com.oukele.springboot.springboot_demo2.dao; import com.oukele.springboot.springboot_demo2.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; public interface UserMapper extends JpaRepository<User,Integer> { }
service包中的 UserService 接口
package com.oukele.springboot.springboot_demo2.service;
import com.oukele.springboot.springboot_demo2.entity.User;
import java.util.List;
public interface UserService {
List<User> listAll();
}
serviceImp 包中的 UserServiceImp 类
package com.oukele.springboot.springboot_demo2.serviceImp; import com.oukele.springboot.springboot_demo2.dao.UserMapper;
import com.oukele.springboot.springboot_demo2.entity.User;
import com.oukele.springboot.springboot_demo2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class UserServiceImp implements UserService { @Autowired
private UserMapper userMapper; @Override
public List<User> listAll() {
return userMapper.findAll();
}
}
controller 包 中 的 UserController 类
package com.oukele.springboot.springboot_demo2.controller; import com.oukele.springboot.springboot_demo2.entity.User;
import com.oukele.springboot.springboot_demo2.serviceImp.UserServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
public class UserController { @Autowired
private UserServiceImp userServiceImp; @RequestMapping(path = "list",method = RequestMethod.GET)
public List<User> getList(){
return userServiceImp.listAll();
} }
重新启动,运行结果:

。这样就快速完成了一个 SpringBoot项目。
示例源码下载地址:https://github.com/oukele/SpringBoot-demo1
使用IDEA 搭建一个 SpringBoot + Hibernate + Gradle 项目的更多相关文章
- 使用IDEA 搭建一个SpringBoot + Hibernate + Gradle
---恢复内容开始--- 打开IDEA创建一个新项目: 第一步: 第二步: 第三步: 最后一步: 如果下载的时候时间太久.可以找到build.gradle文件,添加以下代码.如下图 maven{ ur ...
- 手把手搭建一个完整的javaweb项目
手把手搭建一个完整的javaweb项目 本案例使用Servlet+jsp制作,用MyEclipse和Mysql数据库进行搭建,详细介绍了搭建过程及知识点. 下载地址:http://download.c ...
- 小记如何有顺序的搭建一个Spring的web项目
如何有顺序的搭建一个Spring的web项目 一.新建一个简单的maven,war工程 eclipse下如有报错,右键 Deployment 单击 Generate 生成web.xml后可解决报错 二 ...
- 快速搭建一个基于react的项目
最近在学习react,快速搭建一个基于react的项目 1.创建一个放项目文件夹,用编辑器打开 2.打开集成终端输入命令: npm install -g create-react-app 3. cre ...
- react全家桶从0搭建一个完整的react项目(react-router4、redux、redux-saga)
react全家桶从0到1(最新) 本文从零开始,逐步讲解如何用react全家桶搭建一个完整的react项目.文中针对react.webpack.babel.react-route.redux.redu ...
- 5分钟快速搭建一个springboot的项目
现在开发中90%的人都在使用springboot进行开发,你有没有这样的苦恼,如果让你新建一个springboot开发环境的项目,总是很苦恼,需要花费很长时间去调试.今天来分享下如何快速搭建. 一 ...
- 用Eclipse 搭建一个Maven Spring SpringMVC 项目
1: 先创建一个maven web 项目: 可以参照之前的文章: 用Maven 创建一个 简单的 JavaWeb 项目 创建好之后的目录是这样的; 2: 先配置maven 修改pom.xml & ...
- springboot入门(一)--快速搭建一个springboot框架
原文出处 前言在开始之前先简单介绍一下springboot,springboot作为一个微框架,它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架 ...
- 带你搭建一个简单的mybatis项目:IDEA+spring+springMVC+mybatis+Mysql
最近小编有点闲,突发奇想想重温一下mybatis,然后在脑海中搜索了一下,纳尼,居然不太会用了,想到这里都是泪啊!!现在我所呆的的公司使用的是springboot+hebinate,编程都是使用的JP ...
随机推荐
- 【Python】【demo实验21】【练习实例】【求球反弹高度】
原题: 一球从100米高度自由落下,每次落地后反跳回原高度的一半:再落下,求它在第10次落地时,共经过多少米?第10次反弹多高? 我的源码: #!/usr/bin/python # encoding= ...
- Log4j2配置之Appender详解
Log4j2配置之Appender详解 Appender负责将日志事件传递到其目标.每个Appender都必须实现Appender接口.大多数Appender将扩展AbstractAppender,它 ...
- C++变量的声明和定义
1.变量的定义:变量的定义用于为变量分配存储控件,还可以为变量指定初始值.在一个程序中,变量有且仅有一个定义. 2.变量的声明:用于向程序表名变量的类型和名字.程序中变量可以声明多次,但只能定义一次. ...
- 2017Nowcoder Girl初赛重现赛 D(二进制枚举
链接:https://ac.nowcoder.com/acm/contest/315/D来源:牛客网 题目描述 妞妞参加完Google Girl Hackathon之后,打车回到了牛家庄. 妞妞需要支 ...
- 【xlwings】 wps 和 office 的excel creat_sheet区别
最近在学习 xlwings,参考学习的网址:https://www.jianshu.com/p/b534e0d465f7 写得很棒,学到了很多. 在新建sheet表单, 发现一个问题. import ...
- raspberrypi 树莓派 内核编译
相关版本信息 硬件:树莓派 2b 目标系统: linux 编译环境:ubuntu 14.4 32bit 用户路径:/home/hi/ 安装交叉编译链 cdmkdir pi/kernelcd pi/ke ...
- Django入门:操作数据库(Model)
Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表 基本结构 1 2 3 4 5 6 from django.db import model ...
- C#等比列放大缩小图片
public Bitmap ChangeImgSize(Image bit, double Multiple) { Bitmap newBitmap ...
- 使用SSH连接AWS服务器
使用SSH连接AWS服务器 一直有一台AWS云主机,但是之前在Windows平台都是使用Xshell连接的,换到Ubuntu环境之后还没有试,昨天试了一下,终于使用SSH连接成功了,这里记录一下步骤: ...
- 创建json对象
jQuery创建json对象 方法二: <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...