maven下搭建SpringBoot+SpringMvc+SringData

 

写下这篇文章的意义在于,想让像我一样的入门者有一个算不上权威但起码准确的搭建教程,少走一点弯路。

一、运行环境。

1.jdk

2.maven(要修改setting文件),网上很多教程,这里不缀述

3.使用idea

链接:https://pan.baidu.com/s/1dsvfv4uuoDQ5STkTCkRajw 密码:mhgv

4.idea要安装lombok插件(网上很多教程)

5.pgsql

二、环境搭建

1.新建project,然后选择Spring Initiallizr,然后选择Next。

 

2.填写项目相关信息。

(1)Group定义了项目属于哪个组。比如公司叫xxx,项目叫StudentSystem,那么Group一般叫com.xxx.StudentSystem。

(2)Artifact定义了项目在组中的唯一ID,比如Res,Bussiness。

(3)Version代表了版本。

(4)填写好之后点击next。

3.选择相应依赖,在这里我要新建一个Web项目,数据库使用pgsql,用到lombok、SpringData,所以选择引入如下依赖,选好后next。

 

 

 

 4.第一次新建项目,maven引用是不正确的,选择File->settinds->查找maven->

1.Maven home directory选择自己maven的安装路径。

2.点击User settings file和Local repository后面的Override然后将路径修改成自己安装的maven的settings.xml文件的路径,repository也同样修改。

改好之后点击ok。

5.

右下角弹出框,一定不要忘记,Import change,我选择以后也自动引入,选择第二个,然后我们就会发现左侧,Extern libraries下面的包变多了。

6.选择src->main->resources->application.properties配置我们的数据库

其中:

(1)server.port配置我们服务的端口号

(2)spring.datasource.url配置我们数据库的链接端口号和数据库名称

(3)spring.datasource.userName配置相应数据库链接的用户名

(4)spring.datasource.password配置密码

(5)在配置hibernate.cfg.xml时需指定使用数据库的方言。

关于方言,这篇很全

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
来配置postgressql的方言。

(6)

spring.jpa.properties.hibernate.hbm2ddl.auto=update

 在我们数据库存在的情况下,首次运行会根据我们的实体类新建表和相应字段。

 

server.port=9999
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/MyUser
spring.datasource.userName=postgres
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true
spring.thymeleaf.cache=false

在这里我们要修改pom.xml文件,引入新的依赖。

<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-entitymanafer</artifactId>
	<version></version>
</dependency>

7.然后我们在com.xxx.StudentSystem.demo下面新建这样几个文件夹。

(1)entity用来存放我们的实体类。

(2)repository下面用来存放extends JpaRepository的接口用来和数据库交互。

(3)service下面用来存放和表现层交互的接口。

(4)impl下面存放实现service接口的是实现类。

(5)controller下面用来存放和前端交互的类。

8.在entity下面新建我们的MyUser类,在pgsql中不能新建user表,因为user是pgsql的一张系统表。

id为们的主键,自增,数据类型为int。

id、name、password都不能为空

package com.xxx.studentsystem.demo.entity;

import lombok.Data;

import javax.persistence.*;

@Entity
@Data
public class MyUser {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(nullable = false,unique = true)
    private int id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String password;
}

9.repository下面新建我们的MyUserRepository接口

package com.xxx.studentsystem.demo.repository;

import com.xxx.studentsystem.demo.entity.MyUser;
import org.springframework.data.jpa.repository.JpaRepository;


public interface MyUserRepository extends JpaRepository<MyUser,Integer> {
        MyUser findById(int id);
}

10.service文件夹下新建接口MyUserService

package com.xxx.studentsystem.demo.service;

import com.xxx.studentsystem.demo.entity.MyUser;

public interface MyUserService {
    MyUser findMyUser(int id);
}

11.impl文件夹下新建MyUserServiceImpl实现MyUserService接口,不要忘记@Service注解。

@Service注解,不了解可以看这里

package com.xxx.studentsystem.demo.impl;

import com.xxx.studentsystem.demo.entity.MyUser;
import com.xxx.studentsystem.demo.repository.MyUserRepository;
import com.xxx.studentsystem.demo.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyUserServiceImpl implements MyUserService {
    @Autowired
    MyUserRepository myUserRepository;
    @Override
    public MyUser findMyUser(int id) {
        return myUserRepository.findById(id);
    }
}

12.最后在controller文件夹下新建myUserController类

@RequestMapping等注解,不了解可以看这里

package com.xxx.studentsystem.demo.controller;

import com.xxx.studentsystem.demo.entity.MyUser;
import com.xxx.studentsystem.demo.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyUserController {
    @Autowired
    MyUserService myUserService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    @ResponseBody
    public MyUser findById(@RequestParam("id")int id)
    {
        return  myUserService.findMyUser(id);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值