使用IDEA 搭建一个 SpringBoot + Hibernate + Gradle 项目

现在创建个项目:

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

 

1 maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}

关闭项目,重新打开。

等待,依赖下载完成。

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

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

增加,依赖...

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

 

 1 # 配置 Tomcat 端口号
 2 server.port=8881
 3 # 数据库驱动
 4 spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
 5 # 连接数据库
 6 spring.datasource.url=jdbc:mariadb://localhost:3306/test
 7 # 用户名
 8 spring.datasource.username=oukele
 9 # 密码
10 spring.datasource.password=oukele
11 
12 # 要标注它是那个一个数据库,如果不标注它,它会使用MySQL的,因为我们是创建MySQL数据
13 spring.jpa.database-platform=org.hibernate.dialect.MariaDB102Dialect

 在 templates 文件夹 中 新建一个 index.html 页面

 

然后,启动。。

启动,成功

OK啦。

现在,我们去访问数据库,拿到数据吧。

项目结构:

entity包中的User 类

 1 package com.oukele.springboot.springboot_demo2.entity;
 2 
 3 import javax.persistence.*;
 4 
 5 @Entity
 6 @Table(name = "user")//数据库的表名
 7 public class User {
 8 
 9     @Id
10     @GeneratedValue(strategy = GenerationType.IDENTITY)//自动增长主键
11     private int id;
12 
13     @Column(name = "username")//数据库的字段名,数据库 不区分大小写 这个 要注意
14     private String name;
15 
16     private String password;
17 
18     public int getId() {
19         return id;
20     }
21 
22     public void setId(int id) {
23         this.id = id;
24     }
25 
26     public String getName() {
27         return name;
28     }
29 
30     public void setName(String name) {
31         this.name = name;
32     }
33 
34     public String getPassword() {
35         return password;
36     }
37 
38     public void setPassword(String password) {
39         this.password = password;
40     }
41 }

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 类

 1 package com.oukele.springboot.springboot_demo2.controller;
 2 
 3 import com.oukele.springboot.springboot_demo2.entity.User;
 4 import com.oukele.springboot.springboot_demo2.serviceImp.UserServiceImp;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 import java.util.List;
11 
12 @RestController
13 public class UserController {
14 
15     @Autowired
16     private UserServiceImp userServiceImp;
17 
18     @RequestMapping(path = "list",method = RequestMethod.GET)
19     public List<User> getList(){
20         return userServiceImp.listAll();
21     }
22 
23 }

重新启动,运行结果:

。这样就快速完成了一个 SpringBoot项目。

示例源码下载地址:https://github.com/oukele/SpringBoot-demo1

posted @ 2018-12-30 18:50  追梦滴小蜗牛  阅读(3932)  评论(0编辑  收藏  举报