phper学习spring boot Les1 - hello world

202 阅读2分钟

本节开始规划项目和实现hello,world

项目规划

由于是一个学习性质的项目,所以1.0版本规划的功能会简单为主,让自己先熟悉java web开发和sprig boot.因此做了以下几点考虑:

  1. 产品尽可能简单,只含注册、登录、下单等模块
  2. 不进行前后端分离,不使用VUE那一套,专注java spring本省
  3. 使用Mybatis,这是国内较普遍的做法。

步骤

1. 初始化项目

使用Netbeans 初始化项目,选好所需组件.

为什么我还在用netbeans?

  1. PHP好长一段时间都使用netbeans。
  2. idea 破解越来越不容易,不想在折腾耗费精力了。
  3. 其实netbeans各方面还可以,有些东西已经习惯了。
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
	</dependencies>

2.配置数据库

如果这个时候启动project,会报数据库连接错误。此时需要配置上数据库信息

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/db_learn_mall
spring.datasource.username=root
spring.datasource.password=password

这里我还是使用properties格式进行项目配置,因为官方的例子默认是这个,便于直接copy

3.编写首页

project这个时候可以成功启动了,但是访问的时候仍然报错。因为缺少默认页的路由和输出。

  1. 新建controller名字的package
  2. 新建IndexController并新建index的action
  3. return string 类型的helloworld

这个时候访问,就可以看到hello world了

至此,les1结束。