SpringBoot 数据库建表

SpringBoot JPA

一 . 项目结构

[外链图片转存失败(img-qKyBwKgj-1565139028862)(C:\Users\zsj55\AppData\Roaming\Typora\typora-user-images\1565012462359.png)]

二 . application.properties配置

[外链图片转存失败(img-7qHVINoP-1565139028865)(C:\Users\zsj55\AppData\Roaming\Typora\typora-user-images\1565012666197.png)]

server.port:可以把默认8080端口改成任意你想要的端口

server.servlet.context-path:称为项目路径,是构成url地址的一部分,这个可以不写。

三 . CrudRepository

Repository: 仅仅是一个标识,表明任何继承它的均为仓库接口类,方便Spring自动扫描识别

CrudRepository: 继承Repository,实现了一组CRUD相关的方法

CrudRepository定义了一系列堆数据库进行基本操作的方法,如果需要额外方法,可以自己定义。

CrudRepository接口 源代码所提供的方法:

<S extends T> S save(S entity);
<S extends T> Iterable<S> save(Iterable<S> entities);
T findOne(ID id);
boolean exists(ID id);
Iterable<T> findAll();
Iterable<T> findAll(Iterable<ID> ids);
long count();
void delete(ID id);
void delete(T entity);
void delete(Iterable<? extends T> entities);
void deleteAll();

四 . 具体实现

entity

package com.zz.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.sql.Date;


/**
 * @author zsj55
 */
@Entity
public class OrderMaster {
    @Id
    @Column(length=50)
    private String id;
    private String name;
    private String address;
    private double total;
    private Date createTime;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

repository

package com.zz.repository;

import com.zz.entity.OrderMaster;
import org.springframework.data.repository.CrudRepository;

/**
 * @author zsj55
 */
 public interface OrderMasterRepository extends CrudRepository<OrderMaster,String>{
     
 }

service

package com.zz.service;

import com.zz.entity.OrderMaster;
import com.zz.repository.OrderMasterRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderService {

    @Resource
    OrderMasterRepository masterRepository;
    public OrderMaster addMaster(OrderMaster orderMaster){
        return masterRepository.save(orderMaster);

    }
}

controller

package com.zz.controller;

import com.zz.entity.OrderMaster;
import com.zz.service.OrderService;
import com.zz.utils.Keyutils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @RestController表示返回类型都是json格式
 */
@RestController
@RequestMapping("ordermaster")
public class OrderCOntroller{
    @Resource
    OrderService orderService;

    @RequestMapping("add")
    public OrderMaster save(HttpServletRequest request){
        String address = request.getParameter("address");
        String name = request.getParameter("name");
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setId(Keyutils.genUniqueKey());
        orderMaster.setAddress(address);
        orderMaster.setName(name);
        return orderService.addMaster(orderMaster);
    }
}

启动类

package com.zz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        /*SpringApplication.run(App.class,args);*/
        SpringApplication sa = new SpringApplication(App.class);
        sa.run(args);
    }
}

ic class App {
public static void main(String[] args) {
/SpringApplication.run(App.class,args);/
SpringApplication sa = new SpringApplication(App.class);
sa.run(args);
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值