springboot 是 springmvc 的升级,对于编码、配置、部署和监控,更加简单
微服务
微服务是一个新兴的软件架构,就是把一个大型的单个应用程序和服务拆分为数十个的支持微服务。一个微服务的策略可以让工作变得更为简便,它可扩展单个组件而不是整个的应用程序堆栈,从而满足服务等级协议。
spring 为 微服务提供了一整套的组件-springclound , spirngboot 就是该基础。
第一个springboot程序
这里使用的开发软件是intellij idea,和eclipse差不太多,界面更炫酷,功能更强大;android studio就是基于intellij 开发的,我之前使用过android studio,它俩界面几乎一样。
intellij idea官网:http://www.jetbrains.com/idea/
配置好 maven, tomcat, jdk 就可以使用了
maven配置的*仓库阿里云镜像,这个地址下载 jar 包的速度,谁用谁知道!
setting.xml
1
2
3
4
5
6
7
8
9
10
11
12
|
.
.
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http: //maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorof>central</mirrorof>
</mirror>
</mirrors>
.
.
|
使用idea创建springboot项目
我的idea版本:intellij idea 2016.3.1
项目结构为:
项目默认的 maven pom.xml文件
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<?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.jxust</groupid>
<artifactid>spirngbootdemo</artifactid>
<version> 0.0 . 1 -snapshot</version>
<packaging>jar</packaging>
<name>spirngbootdemo</name>
<description>demo project for spring boot</description>
<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version> 1.4 . 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>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
</plugin>
</plugins>
</build>
</project>
|
运行spirngbootdemoapplication的main方法,就能开始运行。
其他启动方式,请看视频教程 http://www.imooc.com/learn/767\
控制台输出:
1
2
3
4
5
6
7
8
9
10
11
|
"c:\program files\java\jdk1.8.0_91\bin\java" ....
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | ' _| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: spring boot :: (v1. 4.2 .release)
2016 - 12 - 16 14 : 56 : 52.083 info 15872 --- [ main] o.s.j.e.a.annotationmbeanexporter : registering beans for jmx exposure on startup
2016 - 12 - 16 14 : 56 : 52.215 info 15872 --- [ main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat started on port(s): 8080 (http)
2016 - 12 - 16 14 : 56 : 52.255 info 15872 --- [ main] com.jxust.spirngbootdemoapplication : started spirngbootdemoapplication in 7.795 seconds (jvm running for 9.177 )
|
从这里可以看到 tomcat 的端口号,因为还没有自定义controller,所以还没有视图,下面来创建一个输出hello springboot!的视图。
创建一个hellocontroller,位于controller包下
hellocontroller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.jxust.controller;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;
/**
* created by peng
* time: 2016/12/16 15:45
*/
@restcontroller
public class hellocontroller {
@requestmapping ( "/hello" )
public string say(){
return "hello springboot!" ;
}
}
|
@restcontroller spring4 之后新加的注解,原来返回json需要@responsebody配合@controller,现在一个顶俩
在浏览器中输入http://localhost:8080/hello就能输出hello springboot!这句话。
自定义属性配置
用到的是application.properties这个文件
配置端口号和访问前缀
1
2
3
|
application.properties
server.port= 8081
server.context-path=/springboot
|
除了使用.properties格式的文件,还可以使用.yml格式的配置文件(推荐),更加简便
application.yml
把原来的application.properties文件删除
注意格式,空格不能少
获取配置文件中的属性值
我们也可以在配置文件中,配置数据,在 controller 中获取,比如:
application.yml
1
2
3
4
|
server:
port: 8081
context-path: /springboot
name: 小胖
|
hellocontroller 获取配置文件中的值
hellocontroller.java
1
2
3
4
5
6
7
8
9
10
11
12
|
....
@restcontroller
public class hellocontroller {
@value ( "${name}" )
private string name;
@requestmapping (value = "/hello" ,method = requestmethod.get)
public string say(){
return name;
}
}
|
返回的为name的值
配置文件中值配置方式的多样化
配置文件的值可以是多个,也可以是组合,如:
application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
name: 小胖
age: 22
或者
name: 小胖
age: 22
content: "name: ${name},age: ${age}"
或者
server:
port: 8081
context-path: /springboot
person:
name: 小胖
age: 22
|
前两种配置获取值的方式都是一样的,但是对于这种方式,person 有相应的两个属性,需要这样处理
personproperties.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.jxust;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.stereotype.component;
/**
* created by peng
* time: 2016/12/16 16:34
*/
@component
@configurationproperties (prefix = "person" )
public class personproperties {
private string name;
private integer age;
public string getname() {
return name;
}
public void setname(string name) {
this .name = name;
}
public integer getage() {
return age;
}
public void setage(integer age) {
this .age = age;
}
}
|
alt+insert快捷键提示生成 getter and setter
pom.xml需要加入下面的依赖,处理警告
1
2
3
4
5
|
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-configuration-processor</artifactid>
<optional> true </optional>
</dependency>
|
hellocontroller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.jxust.controller;
import com.jxust.personproperties;
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.restcontroller;
/**
* created by peng
* time: 2016/12/15 20:55
*/
@restcontroller
public class hellocontroller {
@autowired
private personproperties personproperties;
@requestmapping (value = "/hello" ,method = requestmethod.get)
public string say(){
return personproperties.getname()+personproperties.getage();
}
}
|
关于配置文件application.yml的多套配置
类似 il8n 文件国际化的配置方式i18n_en_us.properties和i18n_zh_cn.properties
这样能解决,需要频繁修改配置的尴尬
由application.yml配置文件决定使用那套配置文件。
application.yml
1
2
3
|
spring:
profiles:
active: a
|
application-a.yml
1
2
3
4
5
6
|
server:
port: 8081
context-path: /springboot
person:
name: 小雷
age: 21
|
application-b.yml
1
2
3
4
5
6
|
server:
port: 8081
context-path: /springboot
person:
name: 小胖
age: 22
|
springboot增删改查实例
完整的项目结构
controller的使用
controller的使用
@controller chu处理http请求
@restcontroller spring4 之后新加的注解,原来返回json需要@responsebody配合@controller
@requestmapping 配置url映射
对于 rest 风格的请求
对于 controller 中的方法上的注解
1
|
@requestmapping (value = “/hello”,method = requestmethod.get) @requestmapping (value = “/hello”,method = requestmethod.post) @requestmapping (value = “/hello”,method = requestmethod.delete) @requestmapping (value = “/hello”,method = requestmethod.put)
|
springboot 对上面的注解进行了简化
1
|
@getmapping (value = “/girls”) @postmapping (value = “/girls”) @putmapping (value = “/girls/{id}”) @deletemapping (value = “/girls/{id}”)
|
浏览器需要发送不同方式的请求,可以安装httprequester插件,火狐浏览器可以直接搜索该组件安装。
spring-data-jpa
jpa全称java persistence api.jpa通过jdk 5.0注解或xml描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
hibernate3.2+、toplink 10.1.3以及openjpa都提供了jpa的实现。
利用jpa创建mysql数据库
pom.xml加入jpa和mysql的依赖
1
2
3
4
5
6
7
8
|
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-jpa</artifactid>
</dependency>
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
</dependency>
|
配置jpa和数据库
application.yml
1
2
3
4
5
6
7
8
9
10
11
12
|
spring:
profiles:
active: a
datasource:
driver- class -name: com.mysql.jdbc.driver
url: jdbc:mysql: //127.0.0.1:3306/db_person
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
|
格式很重要
需要自己手动去创建 db_person 数据库
创建与数据表对应的实体类person
person.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package com.jxust.entity;
import javax.persistence.entity;
import javax.persistence.generatedvalue;
import javax.persistence.id;
/**
* created by peng
* time: 2016/12/16 17:56
*/
@entity
public class person {
@id
@generatedvalue
private integer id;
private string name;
private integer age;
//必须要有构造函数
public person() {
}
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 integer getage() {
return age;
}
public void setage(integer age) {
this .age = age;
}
}
|
运行项目后,查看数据库,会自动创建表 person
1
2
3
4
5
6
7
8
9
10
11
|
mysql> use db_person;
database changed
mysql> desc person;
+-------+--------------+------+-----+---------+----------------+
| field | type | null | key | default | extra |
+-------+--------------+------+-----+---------+----------------+
| id | int ( 11 ) | no | pri | null | auto_increment |
| age | int ( 11 ) | yes | | null | |
| name | varchar( 255 ) | yes | | null | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set ( 0.09 sec)
|
接下来就可以进行person表的增删改查了
创建控制器personcontroller.java
首先创建一个接口personrepository,位于dao包下,personcontroller调用该接口继承自jparepository的方法,来实现和数据库交互
这个personrepository接口的功能,与ssm框架中 dao 层接口功能有异曲同工之妙;在ssm框架中,service层通过该接口,间接执行mybatis数据库映射文件(.xml)里的相应sql语句,执行数据库增删改查的操作。(mapper自动实现dao接口)
personrepository.java
1
2
3
4
5
6
7
8
9
|
package com.jxust.dao;
import com.jxust.entity.person;
import org.springframework.data.jpa.repository.jparepository;
/**
* created by peng
* time: 2016/12/16 18:07
*/
public interface personrepository extends jparepository<person,integer> {
}
|
personcontroller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.jxust.controller;
import com.jxust.dao.personrepository;
import com.jxust.entity.person;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;
import java.util.list;
/**
* created by peng
* time: 2016/12/16 18:04
*/
@restcontroller
public class personcontroller {
@autowired
personrepository personrepository;
@getmapping (value = "/person" )
private list<person> personlist() {
return personrepository.findall();
}
}
|
在数据库中添加两条数据
1
2
3
4
5
6
7
8
|
mysql> select * from person;
+----+------+--------+
| id | age | name |
+----+------+--------+
| 1 | 23 | 夏洛 |
| 2 | 21 | 马冬梅 |
+----+------+--------+
2 rows in set ( 0.04 sec)
|
启动项目执行请求 http://localhost:8081/springboot/person
控制台输出的sql语句:
1
|
hibernate: select person0_.id as id1_0_, person0_.age as age2_0_, person0_.name as name3_0_ from person person0_
|
其他增删改查的方法
personcontroller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
....
/**
* 添加一个人员
*
* @param name
* @param age
* @return
*/
@postmapping (value = "/person" )
public person personadd( @requestparam ( "name" ) string name,
@requestparam ( "age" ) integer age) {
person person = new person();
person.setname(name);
person.setage(age);
return personrepository.save(person);
}
/**
* 查询一个人员
*
* @param id
* @return
*/
@getmapping (value = "/person/{id}" )
public person personfindone( @pathvariable ( "id" ) integer id) {
return personrepository.findone(id);
}
/**
* 删除一个人员
*
* @param id
*/
@deletemapping (value = "/person/{id}" )
public void persondelete( @pathvariable ( "id" ) integer id) {
personrepository.delete(id);
}
/**
* 更新一个人员
*
* @param id
* @param name
* @param age
* @return
*/
@putmapping (value = "/person/{id}" )
public person personupdate( @pathvariable ( "id" ) integer id,
@requestparam ( "name" ) string name,
@requestparam ( "age" ) integer age) {
person person = new person();
person.setid(id);
person.setname(name);
person.setage(age);
return personrepository.save(person);
}
|
对应的请求方式为:
查询一个用户:
添加一个用户
删除一个用户(无返回值)
更新一个用户
那么根据年龄查询,可不可以呢。答案是此刻还不行
从控制台的语句可以看出,sql 语句都是根据id来查询的
1
|
hibernate: select person0_.id as id1_0_0_, person0_.age as age2_0_0_, person0_.name as name3_0_0_ from person person0_ where person0_.id=?
|
根据年龄查询
在personrepository增加一个方法findbyage(integer age)
1
2
3
4
5
6
7
8
9
|
public interface personrepository extends jparepository<person,integer> {
/**
* 通过年龄来查询
* 方法名固定findbyage
* @param age
* @return
*/
public list<person> findbyage(integer age);
}
|
在personcontroller中加入相应的查询方法
1
2
3
4
5
6
7
8
9
10
|
....
/**
* 通过年龄来查询
* @param age
* @return
*/
@getmapping (value = "/person/age/{age}" )
public list<person> personlistbyage( @pathvariable ( "age" ) integer age) {
return personrepository.findbyage(age);
}
|
输入请求http://localhost:8081/springboot/person/age/23,查询年龄为23的人员
控制台输出sql语句:
1
|
hibernate: select person0_.id as id1_0_, person0_.age as age2_0_, person0_.name as name3_0_ from person person0_ where person0_.age=?
|
事务管理
两条 sql 语句同时在一个方法中执行,为了防止一个 sql 语句执行成功而另一个 sql 语句执行失败,引入了事务管理,需要在方法上加 @transactional事务注解
事务确保了数据库数据的完整性和一致性
personservice.java
在personcontroll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.jxust.service;
import com.jxust.dao.personrepository;
import com.jxust.entity.person;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import javax.transaction.transactional;
/**
* created by peng
* time: 2016/12/16 19:30
*/
@service
public class personservice {
@autowired
private personrepository personrepository;
/**
* 事务管理测试
* 两条数据同时成功,或者同时不成功
* 保证数据库数据的完整性和一致性
*/
@transactional
public void inserttwo(){
person persona = new person();
persona.setname( "秋雅" );
persona.setage( 19 );
personrepository.save(persona);
system.out.print( 1 / 0 );
person personb = new person();
personb.setname( "梦特娇" );
personb.setage( 25 );
personrepository.save(personb);
}
}
|
er中测试
1
2
3
4
5
6
7
8
9
10
11
|
...
@autowired
private personservice personservice;
...
/**
* 事务测试
*/
@postmapping ( "/person/two" )
public void persontwo(){
personservice.inserttwo();
}
|
重新运行项目,执行请求 post方式http://localhost:8081/springboot/person/two
数据库并没有添加第一条数据,说明存在事务管理
完整的personcontroller.java、personrepository.java和pom.xml
personcontroller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
package com.jxust.controller;
import com.jxust.dao.personrepository;
import com.jxust.entity.person;
import com.jxust.service.personservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.*;
import java.util.list;
/**
* created by peng
* time: 2016/12/16 18:04
*/
@restcontroller
public class personcontroller {
@autowired
personrepository personrepository;
@autowired
private personservice personservice;
/**
* 查询所有人员列表
*
* @return
*/
@getmapping (value = "/person" )
private list<person> personlist() {
return personrepository.findall();
}
/**
* 添加一个人员
*
* @param name
* @param age
* @return
*/
@postmapping (value = "/person" )
public person personadd( @requestparam ( "name" ) string name,
@requestparam ( "age" ) integer age) {
person person = new person();
person.setname(name);
person.setage(age);
return personrepository.save(person);
}
/**
* 查询一个人员
*
* @param id
* @return
*/
@getmapping (value = "/person/{id}" )
public person personfindone( @pathvariable ( "id" ) integer id) {
return personrepository.findone(id);
}
/**
* 删除一个人员
*
* @param id
*/
@deletemapping (value = "/person/{id}" )
public void persondelete( @pathvariable ( "id" ) integer id) {
personrepository.delete(id);
}
/**
* 更新一个人员
*
* @param id
* @param name
* @param age
* @return
*/
@putmapping (value = "/person/{id}" )
public person personupdate( @pathvariable ( "id" ) integer id,
@requestparam ( "name" ) string name,
@requestparam ( "age" ) integer age) {
person person = new person();
person.setid(id);
person.setname(name);
person.setage(age);
return personrepository.save(person);
}
/**
* 通过年龄来查询
* @param age
* @return
*/
@getmapping (value = "/person/age/{age}" )
public list<person> personlistbyage( @pathvariable ( "age" ) integer age) {
return personrepository.findbyage(age);
}
/**
* 事务测试
*/
@postmapping ( "/person/two" )
public void persontwo(){
personservice.inserttwo();
}
}
|
personrepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.jxust.dao;
import com.jxust.entity.person;
import org.springframework.data.jpa.repository.jparepository;
import java.util.list;
/**
* created by peng
* time: 2016/12/16 18:07
*/
public interface personrepository extends jparepository<person,integer> {
/**
* 通过年龄来查询
* 方法名固定
* @param age
* @return
*/
public list<person> findbyage(integer age);
}
|
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<?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.jxust</groupid>
<artifactid>spirngbootdemo</artifactid>
<version> 0.0 . 1 -snapshot</version>
<packaging>jar</packaging>
<name>spirngbootdemo</name>
<description>demo project for spring boot</description>
<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version> 1.4 . 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-configuration-processor</artifactid>
<optional> true </optional>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
</plugin>
</plugins>
</build>
</project>
|
总结
以上所述是小编给大家介绍的intellij idea springboot 数据库增删改查实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.phpsoho.com/2018/02/07/intellij-idea-springboot-数据库增删改查实例/