springboot 简单搭建

87 篇文章 1 订阅

springboot的入门请参考:https://blog.csdn.net/hanjun0612/article/details/81538449

 

这里就简单看下搭建:

一,看一下项目结构:

创建一个 Maven项目

App.class的代码如下:

package com.App;

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);
    }
}

 

二,创建application.properties

#修改tomcat默认端口号
server.port=8090

#修改context path
server.context-path=/test

#配置数据源信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=root

#配置jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true

三,model

package com.App.model;

import javax.persistence.*;


@Entity
@Table(name="user")
public class User {

    @Id @GeneratedValue(strategy= GenerationType.AUTO)
    private Integer id;
    private String userName;
    private String password;
    private Integer age;

    public Integer getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 

四,dao

package com.App.dao;

import com.App.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface UserDao extends JpaRepository<User, Integer> {
    @Query("from User where id =:id ")
    User getUser(@Param("id") Integer id);
}

 

五,

①  service

service文件夹下,还有一个imp文件夹,用来实现

package com.App.service;


import com.App.model.User;

/**
 * Created by Tyler on 2018/8/9
 */
public interface UserService {
    void save(User user);

    User getabc(Integer id);
}

② Impl

package com.App.service.Impl;

import com.App.dao.UserDao;
import com.App.model.User;
import com.App.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    public void save(User user){
        userDao.save(user);
    }

    @Override
    public User getabc(Integer id) {

        return userDao.getUser(id);
    }
}

 

六 controller

package com.App.controller;



import com.App.model.User;
import com.App.service.UserService;
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;


@RestController
@RequestMapping("hello")
public class HelloController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "say",method = RequestMethod.GET)
    public String sayHello(User user) {
//        user.setUserName("he");
//        user.setPassword("he1");
//        user.setAge(20);
//        userService.save(user);

        User u=userService.getabc(1);
        return "hello ,spring boot";
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值