Spring Boot入門

1 Hello World

1.1 建立Maven Project

1.2 添加Spring Boot相關配置

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

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>

1.3 編寫Controller

@RestController
@EnableAutoConfiguration
public class SampleController {

	@RequestMapping("/sample")
	public String hello(){
		return "sample";
	}
	
	public static void main(String[] args) {
		SpringApplication.run(SampleController.class, args);
	}
	
}

啓動main函數後,能夠在控制檯看到啓動了一個Tomcat容器,經過瀏覽器訪問 sample便可看到頁面輸出了SampleController中hello方法的返回值了。java

samle

至此一個簡單的Rest風格應用已經搭建成功。mysql

2 鏈接數據庫

2.1 添加數據庫相關配置

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>

在resources文件夾下建立application.yml文件並添加如下數據庫配置web

spring:
  datasource:
    url: jdbc:mysql://localhost/test
    username: root
    password: ******
    driver-class: com.mysql.jdbc.Driver

2.2 建立測試數據

在mysql中執行如下sqlspring

use test;

create table user(
	id int primary key not null,
	username varchar(20),
	create_date date,
	last_accessed date,
	is_active tinyint(1)
);

insert into user values(1,'zhangsan',now(),now(),1);

insert into user values(2,'lisi',now(),now(),1);

insert into user values(3,'wangwu',now(),now(),0);

2.3 建立User實體

@Entity
@Table(name="user")
public class User {
	@Id
	@GeneratedValue
	private Long id;
	private String username;
	private Date createDate;
	private Date lastAccessed;
	boolean isActive;
	
	//getter

	//setter
}

2.4 建立UserRepository

@Repository
public interface UserRepository extends CrudRepository<User, Long>{

}

2.5 建立UserController

@EntityScan("com.rft.springboot.entity")
@EnableJpaRepositories("com.rft.springboot.repository")
@RestController
@EnableAutoConfiguration
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserRepository userRepository;
	
	@RequestMapping("/{id}")
	public User view(@PathVariable("id") Long id){
		return userRepository.findOne(id);
	}
	
	@RequestMapping("/all")
	@ResponseBody
	Iterable<User> get(){
		return userRepository.findAll();
	}
	public static void main(String[] args) {
		SpringApplication.run(UserController.class, args);
	}
}

運行main方法,在瀏覽器地址欄輸入 http://localhost:8080/user/1http://localhost:8080/user/all便可查詢到數據庫中的數據sql

user1

4 Security

Spring Boot支持功能強大的Spring Security,而且提供了自動配置的功能,以快速簡便地啓用安全層。數據庫

4.1 添加Security相關配置

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>

4.2 開啓Basic認證

在application.yml中配置Basic認證瀏覽器

security:
   basic:
     enabled: true
   user:
     name: test
     password: test

運行UserController中的main方法,再次訪問 http://localhost:8080/user/1,頁面會彈出Basic認證框tomcat

basic認證

5 部署到Tomcat

5.1 添加相關配置

<packaging>war</packaging>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
	<scope>provided</scope>
</dependency>

其中scope爲provided表示由環境提供安全

5.2 建立Application

@RestController
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
	
	@RequestMapping("/index")
	public String index(){
		return "index";
	}
	
	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Application.class, args);
		String[] beanNames = ctx.getBeanDefinitionNames();
		Arrays.sort(beanNames);
		for(String beanName : beanNames){
			System.out.println(beanName);
		}
	}
	
	
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(Application.class);
	}
}

運行main方法,控制檯會打印出全部beanspringboot

5.3 打包

使用命令提示符在工程所在目錄下運行

mvn package

5.4 部署

將打包好的war包拷貝到Tomcat下,啓動Tomcat服務,經過瀏覽器訪問http://localhost:8080/springboot/user/1,結果正常。

springboot/user/1

參考資料:

Spring Boot——開發新一代Spring Java應用

深刻學習微框架:Spring Boot

相關文章
相關標籤/搜索