[实习笔记]第一个SpringBoot项目

前言

入职网易在线教育部,菜鸡一枚。在nice guider的鼓励下,从点滴做起,一步一步,沉淀所学知识。一为记录心得;二为后来者做个指引,使其少走弯路,把时间用在急需的事情上。

1  目标

完成Windows系统j2ee开发环境搭建,构建第一个springboot项目,跑通从数据库到前端页面的流程。

2  环境搭建

2.1  安装jdk8

点击直接安装即可,注意使用64位安装包。

安装完成后注意配置系统环境变量。

JAVA_HOME

Your path to jdk

Path

%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin

完成后,cmd运行java –version命令,若显示如下,表明运行成功。

注意,如果之前安装过java之前的版本,需要进行如下操作。

a) 修改注册表: HKEY_LOCAL_MACHINE  -----> SOFTWARE   -----> JavaSoft  -----> Java Runtime Environment  -----> CurrentVersion, 把这个键值改成你需要的jdk版本号。

b) 到WINDOWS“system32下, 把这里边的java.exe. javaw.exe javaws.exe 这三个文件备份, 然后从java_home“bin下把你需要的版本的对应的这三个文件copy过来然后:重启或者注销一下  搞定!

参考(http://blog.csdn.net/u013256622/article/details/49096875)

 

2.2     安装Maven

解压。

解压完成后,配置环境变量。

M2_HOME

D:\Software\maven\apache-maven-3.5.0

Path

%M2_HOME%\bin;

完成后,测试下。

没问题后,更改respository目录。

新建目录作为本地仓库,修改安装目录/conf/settings.xml

2.3     安装Intellij IDEA

直接使用安装包安装,安装好之后,注意修改设置。

注意,需要激活码。学生邮箱可以免费激活,感谢本科学校,没有关闭我的邮箱。分享一个激活码,激活日期至2018.6.14。过期可再次通过邮箱激活。

DVKXF9HOS7-eyJsaWNlbnNlSWQiOiJEVktYRjlIT1M3IiwibGljZW5zZWVOYW1lIjoiWmhpQ2hlbmcgR0UiLCJhc3NpZ25lZU5hbWUiOiIiLCJhc3NpZ25lZUVtYWlsIjoiIiwibGljZW5zZVJlc3RyaWN0aW9uIjoiRm9yIGVkdWNhdGlvbmFsIHVzZSBvbmx5IiwiY2hlY2tDb25jdXJyZW50VXNlIjpmYWxzZSwicHJvZHVjdHMiOlt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Dup2gwKdL81ua8EIcGNExHe82kjF4zwfadHk3bQVvbfdAwxcDy4xBjs3L4raPLU3yenSzr/OEur1+jfOxnQSmEcMXKXgrAQ9U55gwjcOFKrgOxEdek/Sk1VfOjvS+nuM4eyEruFMfaZHzoQiuw4IqgGc45ohFH0UUyjYcuFxxDSU9lMCv8qdHKm+wnPRb0l9l5vXsCBDuhAGYD6ss+Ga+aDY6f/qXZuUCEUOH3QUNbbCUlviSz6+GiRnt1kA9N2Qachl+2yBfaqUqr8h7Z2gsx5LcIf5kYNsqJ0GavXTVyWh7PYiKX4bs354ZQLUwwa/cG++2+wNWP+HtBhVxMRNTdVhSm38AknZlD+PTAsWGu9GyLmhti2EnVwGybSD2Dxmhxk3IPCkhKAK+pl0eWYGZWG3tJ9mZ7SowcXLWDFAk0lRJnKGFMTggrWjV8GYpw5bq23VmIqqDLgkNzuoog==

激活后,新建一个工程。目的是为了进入配置界面。

配置字体大小等。


配置JDK。File->Other Settings->Default Project Structure

配置Maven。File-> Other Settings -> Default Settings

修改自动注释。

2.4     安装mysql

请自行安装,我安装的是xampp。

 

3      开始coding

3.1     新建maven工程

新建maven工程,选择webapp模板。


修改pom.xml文件


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>per.gzc</groupId>
  <artifactId>my-first-springboot</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>my-first-springboot</name>
  <url>http://maven.apache.org</url>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
  </dependencies>
  <build>
    <finalName>my-first-springboot</finalName>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.5.4.RELEASE</version>
        <dependencies>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.5.RELEASE</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

  <properties>
    <java.version>1.8</java.version>
  </properties>
</project>

新建源文件SampleController.java


/**
 * @author gezhicheng
 * @since 2017/6/17
 */


import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

@Controller
@EnableAutoConfiguration
public class SampleController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/")
    @ResponseBody
    public String home() {
        return "Hello World";
    }

    @RequestMapping("/getUser/{id}")
    @ResponseBody
    public String getUser(@PathVariable String id) {
        String sql = "select id,username from user where id=?";
        RowMapper rowMapper = new BeanPropertyRowMapper(User.class);
        User user = jdbcTemplate.queryForObject(sql,rowMapper,id);
        return user.toString();
    }

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

新建源文件User.java

/**
 * @author gezhicheng
 * @since 2017/6/17
 */
public class User {private Integer id;
    private String username;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                '}';
    }

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

新建资源文件application.properties

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=

工程结构如下:

3.2     数据库建表

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

写入数据。

3.3     Mavn打包


3.4     运行jar包


3.5     浏览器访问测试


4      结语

从头开始搭建一个工程并不容易,操作很琐碎。Hello成功运行后,可以在边工作之余,边深入研习原理了。


网易云新用户大礼包:https://www.163yun.com/gift

本文来自网易实践者社区,经作者葛志诚授权发布。