一、超喜欢这个,再也不用配置一堆蛋疼的xml了,哇嘎嘎,这个用maven更简单哦,有以下几种方法,1. eclipse安装 springtool插件;2. 下载spring tool suite(解压运行sts-bundle\sts-3.9.0.RELEASE/STS.exe)3. idea的Spring Initializr;
二、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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、试运行,直接找到demo里面的main方法,run
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);
}
}
控制台会输出
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.3.RELEASE)
2017-09-22 14:35:53.188 INFO 6448 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on EZ-20170302QXAD with PID 6448 (D:\my\test\target\classes started by Administrator in D:\my\test)
2017-09-22 14:35:53.191 INFO 6448 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2017-09-22 14:35:53.593 INFO 6448 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@49ec71f8: startup date [Fri Sep 22 14:35:53 CST 2017]; root of context hierarchy
2017-09-22 14:35:54.856 INFO 6448 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$e7a8c8a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-09-22 14:35:55.217 INFO 6448 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-09-22 14:35:55.226 INFO 6448 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-09-22 14:35:55.227 INFO 6448 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.6
2017-09-22 14:35:55.307 INFO 6448 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-09-22 14:35:55.307 INFO 6448 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1719 ms
2017-09-22 14:35:55.438 INFO 6448 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]…………
表示运行成功,打开浏览器输入127.0.0.1:8080就会看到有反应,springboot里面内嵌的tomcat默认端口号为8080,稍后介绍怎么修改
四、接下来写第一个程序看看效果吧方法放在/src/main/java下
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
//注解映射类
@RestController
public class test {
//映射方法,路径/hello 方法 get
@RequestMapping(value = "hello",method = RequestMethod.GET)
public String name() {
return "hello word";
}
}
重新启动程序浏览器输入http://127.0.0.1:8080/hello,就会看到hello word 字样
五、同样springboot也可以运行web程序
在/src/main/resource/templates下新建index.html
java方法如下
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class test {
@RequestMapping(value = "hello",method = RequestMethod.GET)
public String name() {
return "index";
}
}
重启项目就可以看到index.html的内容
六、连接数据库
你只要建个库就好了,其他都不用管,超爽
建domian包,Gril 类
package domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
//映射到数据库表,字段
@Entity
public class Gril {
//主键,自增
@Id
@GeneratedValue
private Integer id;
private String cup;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public Gril setAge(Integer age) {
this.age = age;
return this;
}
public String getCup() {
return cup;
}
public Gril setCup(String cup) {
this.cup = cup;
return this;
}
//不写会报错
public Gril(){}
}
service/GrilService 数据库事务
package service;
import controller.GrilRepository;
import domain.Gril;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class GrilService {
@Autowired
private GrilRepository grilRepository;
//事务
@Transactional
public void insertTwo(){
Gril gril1=new Gril();
gril1.setCup("c");
gril1.setAge(11);
grilRepository.save(gril1);
Gril gril2=new Gril();
//数据库吧字段限制成1,不成功都不插入
gril2.setCup("cccc");
gril2.setAge(11);
grilRepository.save(gril2);
}
}
controller/GrilController 增删改查 事务
package controller;
import domain.Gril;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.GrilService;
import java.util.List;
@RestController
public class GrilController {
@Autowired
private GrilRepository grilRepository;
@Autowired
private GrilService grilService;
//查询所有列表
@GetMapping(value = "/grils")
public List<Gril> grilList(){
return grilRepository.findAll();
}
//添加
@PostMapping(value = "/grils" )
public Gril grilAdd(@RequestParam("cup") String cup,
@RequestParam("age") Integer age){
Gril gril = new Gril();
gril.setAge(age);
gril.setCup(cup);
return grilRepository.save(gril);
}
//查询一个
@GetMapping(value = "/grils/{id}" )
public Gril grilSellect(@PathVariable("id") Integer id){
return grilRepository.findOne(id);
}
//修改(不知道为什么我这一直有问题,但是慕课教程就正常,写的一毛一样啊)
@PutMapping(value = "/grils/{id}" )
public Gril grilUp(@PathVariable("id") Integer id,
@RequestParam("cup") String cup,
@RequestParam("age") Integer age){
Gril gril=new Gril();
gril.setId(id);
gril.setAge(age);
gril.setCup(cup);
return grilRepository.save(gril);
}
//删除
@DeleteMapping (value = "/grils/{id}" )
public String grilDell(@PathVariable("id") Integer id) {
grilRepository.delete(id);
return "删除成功";
}
//事物
@PostMapping(value = "grils/two")
public String grilTwo(){
grilService.insertTwo();
return "事物成功";
}
}
controller/GrilRepository 接口
package controller;
import domain.Gril;
import org.springframework.data.jpa.repository.JpaRepository;
public interface GrilRepository extends JpaRepository <Gril,Integer>{
}
这样就可以测试了,下面是完整的工程图
七、配置文件
自带配置文件是application.properties,不怎么好用,换成了 .yml,这个用着比较爽
spring:
#数据库配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/pro
username: root
password:
jpa:
hibernate:
#更新,还有其他的可以试试
ddl-auto: update
#显示SQL语句
show-sql: true
#服务器设置,太多的请自行百度(反正我也不会,嘎嘎嘎)
server:
port: 80
#默认没有。127.0.0.1/hello,加上这个后就是127.0.0.1/spring/hello
display-name: spring
项目已经上传到github,地址为https://github.com/LONG729564606/springboot.git