为什么学习springboot
SpringBOOT的与spring相比配置简单,更加清爽,构建快速,支持热部署,不过到现在为止还是有些坑的,不过不影响学习。
编辑器
编辑器使用的IDEA,IDEA自带的SpringBoot支持可以快速构建SpringBOOT项目
构建工具
构建工具使用maven
maven配置
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connecter-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置
application.yml
server:
port: 8080
context-path: /demo
spring:
profiles:
active: dev
datasource:
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://127.0.0.1:3306/spring_boot
username: root
password: 123456789
jpa:
hibernate:
ddl-auto: update
show-sql: true
项目结构
- dao
package com.example.dao;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import com.example.model.Person;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by Coder on 2017/3/26.
*/
public interface PersonDao extends JpaRepository<Person, Integer> {
}
- service
package com.example.service;
import com.example.model.Person;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;
/**
* Created by Coder on 2017/3/26.
*/
@Service
public interface PersonService {
public List<Person> getAllPerson();
public void save(Person person);
}
- model
package com.example.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* Created by Coder on 2017/3/26.
*/
@Entity
public class Person {
@Id
@GeneratedValue
private Integer id;
private String name;
private String nickName;
public Person(){
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
}
- controller
package com.example.controller;
import com.example.dao.PersonDao;
import com.example.model.Person;
import com.example.service.PersonService;
import com.example.serviceImpl.PersonServiceImpl;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.transaction.Transactional;
import java.util.List;
/**
* Created by Coder on 2017/3/26.
*/
@RestController
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping(value = "/person", method = RequestMethod.GET)
public List<Person> getpersons(){
return personService.getAllPerson();
}
@Transactional
@RequestMapping(value = "/person/add", method = RequestMethod.GET)
public void addPerson(){
Person person1 = new Person();
person1.setName("liu1");
person1.setNickName("2");
Person person2 = new Person();
person2.setName("liu1");
person2.setNickName("213");
personService.save(person1);
personService.save(person2);
}
}
- serviceImpl
package com.example.serviceImpl;
import com.example.dao.PersonDao;
import com.example.model.Person;
import com.example.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
import java.util.List;
/**
* Created by Coder on 2017/3/26.
*/
@Component
public class PersonServiceImpl implements PersonService{
@Autowired
private PersonDao personDao;
@Override
public List<Person> getAllPerson() {
return personDao.findAll();
}
@Override
public void save(Person person) {
personDao.save(person);
}
}
启动
运行项目根目录下的Application即可