美文网首页
搭建SpringBoot环境-超级详细

搭建SpringBoot环境-超级详细

作者: 任重而道元 | 来源:发表于2018-03-04 15:49 被阅读1640次

SpringBoot是SpringMVC的升级版,零配置,内置了tomcat,直接一行命令整个项目就可以跑起来。

首先看项目的结构

如下图所示,源码部分与springmvc是一致的,都是控制层,服务层,实体类层,这里主要是看配置文件,原来springmvc的那一大堆文件都没有了,只有一个application.properties文件。

文件内容如下,只配置了tomcat端口号和mysql以及jpa相关配置:

图片.png

项目的结构:

图片.png

这里附上我的github上的地址,https://github.com/lwaytogo/spring-boot-demo


运行整个项目

1.找到DemoApplication,然后run as 启动项目

在项目的根目录下有一个DemoApplication.java类,如下:

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

这里只有一个main方法,直接run as 整个项目就可以跑起来了。

2.查看启动日志

下面是输出的日志:

显示启动springboot的版本号

图片.png

如下所示,tomcat端口号为8080,启动成功。

图片.png

3.测试是否启动成功

图片.png

如何创建springboot项目

我这里使用的是spring sts ,是spring官网出的,基于eclipse的开发工具,eclipse有的功能他都有,eclipse没有的功能他也有,如果你是使用的spring的框架,可以尝试使用这个工具,开发过程会变得很简单。

1.创建spring项目

图片.png

2.添加相关的依赖

图片.png

3.添加配置application.properties

# tomcat 相关配置
server.port=8080
#server.context-path=/springboot
server.tomcat.uri-encoding=UTF-8

# MYSQ 相关配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

# Spring JPA 相关配置
spring.jpa.show-sql=true
spring.jpa.properties.jadira.usertype.autoRegisterUserTypes=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.generate-ddl=true
spring.jpa.hibernate.open-in-view=true

4.添加UserController

package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.bean.User;
import com.example.demo.service.UserService;

/**
 * 用户控制层
 * @author Lidy
 *
 */
@Controller
public class UserController {

    
    @Autowired
    private UserService userService;
    
    @GetMapping("/name")
    @ResponseBody
    public User getUserByName() {
        User user = userService.findOne("lidy");
        return user;
    }
    
    @RequestMapping("/daoge")
    @ResponseBody
    public User getUser() {
        User user = new User();
        user.setId(4);
        user.setUserAge(23);
        user.setUserName("daoge");
        return user;
    }

    @GetMapping("/user")
    @ResponseBody
    public List<User> getUserList() {
        List<User> usrList = userService.findUserAll();
        return usrList;
    }
    

    
}

5.添加UserService

package com.example.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.bean.User;
import com.example.demo.repository.UserRepository;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;
    
    
    /**
     * 根据用户名称获取该用户信息
     */
    public User findOne(String userName) {
        User user = userRepository.findOneByUserName(userName);
        return user;
    }
    
    /**
     * 获取所有用户
     * @return
     */
    public List<User> findUserAll() {
        List<User> userList = userRepository.findAll();
        return userList;
    }
    
    
}

6.添加UserRepository

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.bean.User;

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {

    
    /**
     * 根据用户名查询用户信息
     * @param userName
     * @return
     */
    User findOneByUserName(String userName);
    
}

7.添加User

package com.example.demo.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * 用户实体类
 * @author Lidy
 *
 */
@Entity
public class User {

    @Id
    @GeneratedValue
    private Integer id;
    
    /**
     * 用户姓名
     */
    private String userName;
    
    /**
     * 用户年龄
     */
    private Integer userAge;
    

    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 Integer getUserAge() {
        return userAge;
    }

    public void setUserAge(Integer userAge) {
        this.userAge = userAge;
    }
    
    
}

8.创建数据库

这里只需要创建数据库就可以,springbooot在启动的时候会根据user实体类自动创建表。

图片.png

至此完成,总结

至此简单的springboot项目就已经编写完成了,是最基础的springboot项目,有疑问可以私聊,springboot更快的提高开发效率,是以后发展的趋势。

相关文章

网友评论

      本文标题:搭建SpringBoot环境-超级详细

      本文链接:https://www.haomeiwen.com/subject/zbtqfftx.html