spring boot 入门
来源:互联网 发布:淘宝第四层级金额 编辑:程序博客网 时间:2024/06/10 23:09
一、超喜欢这个,再也不用配置一堆蛋疼的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>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;@SpringBootApplicationpublic 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: default2017-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 hierarchy2017-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 Tomcat2017-09-22 14:35:55.227 INFO 6448 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.62017-09-22 14:35:55.307 INFO 6448 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2017-09-22 14:35:55.307 INFO 6448 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1719 ms2017-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;//注解映射类@RestControllerpublic 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;@Controllerpublic 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;//映射到数据库表,字段@Entitypublic 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;@Servicepublic 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;@RestControllerpublic 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
阅读全文
0 0
- 【Spring Boot】Spring Boot 入门
- Spring Boot 快速入门
- spring boot 入门
- Spring Boot 入门
- Spring Boot 入门
- spring boot简易入门
- Spring boot入门
- Spring Boot 快速入门
- Spring boot快速入门
- Spring boot 入门 实例
- Spring Boot 快速入门
- spring boot入门(一)
- spring boot入门例
- Spring Boot入门例子
- spring boot 入门
- Spring Boot入门
- Spring Boot 快速入门
- Spring Boot 入门笔记
- 移动APP漏洞自动化检测平台建设
- python编码问题总结
- 当应用程序不是以UserInteractive 模式运行时显示模式对话框或窗体
- Fence Repair
- Javascript单例模式
- spring boot 入门
- Linux 守护进程创建
- rabbitmq一个节点无法加入集群的排查-(inconsistent_cluster)
- [iOS]网络之NSURLSession的文件下载
- 课堂练习3: 定义一个平面中的Circle类
- Tyvj3980(dp+priority_queue)
- SMSSync:Android手机短信猫
- java集合
- 剑指offer之查找