java初入之spring boot
SpringBoot使用配置
pox.xml文件中添加
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<depedencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</depedencies>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
配置好pox.xml文件,就可以开始我们的SpringBoot之旅了
首先,声明一个Application作为程序的main类
@SpringBootApplication
public class CommonApplication {
public static void main(String[] args) {
SpringApplication.run(CommonApplication.class,args);
}
}
@SpringBootApplication注解等价于使用了@Configuration/@EnableAutoConfiguration/@ComponentScan
然后就可以声明Controller了
依照惯例,第一个就是HelloWorld了
@RestController
public class SayHelloController{
@RequestMapping(Value = "/sayHello" , method = RequestMethod.GET)
public String sayHello(){
return "Hello World!";
}
}
然后运行CommonApplication的main方法,SpringBoot内置的tomat就可以愉快的跑起来了,访问localhost:8080/sayHello 就可以看到返回值Hello World! 了;
配置文件篇
SpringBoot的配置文件放在resource目录下,通常为application.properties或是application.yml两种格式的文件,推荐使用的是.yml格式的文件,来对比一下
.properties
server.port=8888
server.context-path=/hugy
.yml
server :
port : 8888
context-path : /hugy
显然.yml格式的文件更加清晰简洁;
配置文件的使用
1、配置的属性的使用
ForE:配置文件申明一个属性 base-url : https//
如果要在其他类中拿到它,可以使用注解@Value
@Value("${base-url}")
public String baseUrl;
这样baseUrl的值就是https://了
2、但是如果我申明了几十个属性,那么一个一个写Value,不得疯掉么,别怕,我们可以将属性注入到一个bean的类实例中
ForE: student:
name : hugy
age : 18
sex : male
@Componet
@ConfigurationProperties(prefix="student")
public class Student{
public String name;
public int age;
public String sex;
public setXXX(Object xxx){
this.xxx=xxx;
}
public getXXX(){
return xxx;
}
}
这样,SpringBoot就会将student前缀开头的数据赋值给成员变量;
使用时使用@Autowired注解就ok了
@Autowired
private Student student;
是不是很简便呢!!!!!
3、多套配置文件
开发中常常遇到这样的问题,测试环境,我的配置如URL的域名可能是这个,但是生产环境的却又换成了另外一个,这样就需要我们每次都要手动的去更改配置,但是,万一忘了呢囧!!
好了,说一下SpringBoot的解决方法
新建两个配置文件
application-test.yml和application-prod.yml,根据名称很好理解-test是测试环境的配置文件,-prod是生产环境的配置文件
然后就可在application.yml文件中做如下申明
spring :
profile :
active : test/prod
置于取test还是prod就看你了,这样就避免了繁琐的配置更改;
Controller 注解
@PathVariable 获取url中的数据
@RequestMapping(value = "/say/{id}" ,method = RequestMethod.GET)
public void sayHello(@PathVariable("id") Integer id){
}
@RequestParams 获取请求中的数据
@RequestMapping(value = "/say" ,method = RequestMethod.GET)
public void sayHello(@RequestParams(value = "id" , requred = false , defaultValue = "0") Integer id){
}
@GetMapping(使用此注解不用申明RequestMethod,直接指定为GET,同理有@PostMapping)
@GetMapping("/say")
public void sayHello(){
}
数据库的操作(这里以mysql为例)
首先,添加配置
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
这里使用的控制数据库的方式是spring-boot-jpa
然后就是在资源文件中添加一些数据的配置了
spring :
datasource :
driver-class-name : com.mysql.jdbc.Driver
url : jdbc:mysql://localhost:3306/mytest
username : root
passworld : 123456
jpa :
hebernate :
ddl-auto : update
show-sql : true
显然,datasource是数据库的一些配置,driver-class-name表示连接数据库的方式是jdbc,下面三条很好理解,就不说了;
jpa就是对数据库的操作的一些配置了,ddl-auto表示数据库中的表的一些操作方式,如果是create的话,表示每回访问此表都是重新创建的,update的话表示每回访问此表都是在原有的基础上更新;
配置说完了,接下来就是代码的实现了
实体类
要操作数据库,首先要有一张与数据库表的字段一一对应的实体类,比如要建一张person表,有id,name,age,sex字段,其中id是主键,则对应的实体类
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Component
@Entity
public class Person {
@Id
@GeneratedValue
Integer id;
String p_name;
Integer age;
String sex;
public Person() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getP_name() {
return p_name;
}
public void setP_name(String p_name) {
this.p_name = p_name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
操作接口,有了实体类之后,还要有一个对数据中的数据进行操作的类,这个类要继承JpaRepository<Person, Integer>接口,第一个泛型参数表示实体类的类名,第二个表示主键的类型;
实现了这个接口之后就可以调用一些方法对数据库进行操作
如:findAll()查询数据库中的所有数据
save()存储(原表没有就添加,有就更新)
findOne(Obj)查询
delete(Obj)删除
自定义操作方式,如按年龄查询
在操作接口中定义方法:
public List<Person> findByAge(Integer age);
方法的名字是不能随便取的,要是操作+by+字段名;
事务管理
在进行数据库的操作时,首先都会开启一个事物,在不进行事物管理的时候,每次操作数据库(调用数据库操作方法)都会开启一个事物,这样的话,如果在一个方法中想要插入多条数据,但是其中有一条数据时不合法,插入时,只有这条不合法的数据开启的事物会回滚,其他的事物还是会照常执行;
但是如果开启了事物管理,Springboot中是使用@Transational进行注解,插入多条事物实际上是在一个事物中进行,如果其中一个出异常,其他数据也都无法插入;
浙公网安备 33010602011771号