您的位置:首页 > 编程语言 > Java开发

spring boot入门-2

2017-12-24 12:41 357 查看
时光荏苒,岁月如梭,距离我上次写的spring-boot入门1已经过去了好长的时间了,就这么一段时间里面,spring boot有发生了很多的新的变化.在配置方面更加的简化.

使用spring boot能更加简单的进行快速的开发.

下面我们以开发Resultful风格的API为例子,说明.

首先配置.推荐使用.yml文件进行项目的配置,把开发环境和正式环境的配置分开.

1.application.yml中配置使用的配置文件

spring:
  profiles:
#active: dev  开发
active: product


数据库的连接

datasource:
  driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mydb
username: root
password: 123456


jpa的配置

jpa:
   hibernate:
     ddl-auto: update  #存在就不管他,表不存在就创建
#create是项目启动时创建,如果存在就删除再建
show-sql: true


2.书写一个Controller类

/**
* Created by zk on 2017/12/20.
* 作用: com.example.zhaojun.
*/
@RestController
public class Hello {

//获取配置文件的内容,application.yml中的内容
/*  @Value("${cupSize}")
private String cupSize;

@Value("${age}")
private int age;

@Value("${content}")
private String content;*/

// 要加上@Component  这个注解才能注入
@Autowired
private ZhaojunProperties zhaojunProperties;

@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
/*   System.out.println(cupSize);
System.out.println(age);
System.out.println(content);*/
//return "hello zhaojun,i love you very much";
//return cupSize;
//return content;
System.out.println(zhaojunProperties);
return zhaojunProperties.toString();
}
}


@RestController  相当于直接返回页面json格式的数据,因为现在的前后端分离比较多.所以这里就直接使用了,RestController.也可以上继续使用@Controller 的.返回到jsp或者使用模板引擎,对应的html上面去.

3.第二个controller

/**
* Created by zk on 2017/12/20.
* 作用: com.example.zhaojun.
*/
@Controller
@RequestMapping(value = "/h")  //整个方法的访问路径
public class HelloController {

@Autowired
private ZhaojunProperties zhaojunProperties;

//配置多个url
@RequestMapping(value = {"/hello2","/hello3"},method = RequestMethod.GET)
public String hello(){
System.out.println(zhaojunProperties);
return "zhaojun";  //到zhaojun.html
}
}


如果不写method是默认post和get方式都能够访问得到.

多提一句,建议使用Postman来模拟Http的请求的发送,比用chrome上的工具更方便...就是装个软件.很方便

4.关于请求参数的获取:

@RestController
public class HelloController3 {

//请求参数的获取,还可以写成/{id}/hello4
@RequestMapping(value = "/hello4/{id}",method = RequestMethod.GET)
public String hello(@PathVariable("id") Integer id){
return id+"";
}

//id 和url上是一样的
//设置默认值,为0,只能是字符0,"0",非必须的
//@RequestMapping(value = "/hello5",method = RequestMethod.GET)
@GetMapping(value = "/hello5")  //和上面一样的,还有PostMapping 就是Post
public String hello2(@RequestParam(value = "id",required = false,defaultValue = "0") int myId){
System.out.println(myId);
return "id:"+myId;
}
}


这里更加的推荐使用GetMapping 或者使用PostMapping  能够少些一句代码就少些一句,相比较与 RequestMapping同时更加的清晰明了.

5.实体类的注解,进行实体类的注入

@Entity
public class Girl {

@Id
    @GeneratedValue
private int id;
private int age;
private String cupSize;

public Girl() {
}

}


6.这里我们使用spring的JPA的实现进行数据库的查询

public interface GirlRepository extends JpaRepository<Girl,Integer>{
//通过年纪查询
public List<Girl> findByAge(int age);
}


7.service方法的实现

@Service
public class GirlService{

@Autowired
private GirlRepository girlRepository;

@Transactional  //事务
public void insertTwo(){
Girl g1=new Girl();
Girl g2=new Girl();
//同时成功或者失败
girlRepository.save(g1);
int i=1/0;
girlRepository.save(g2);
}
}


其中的@Transaction 是事务的注解.同时成功或者同时失败

@Component


用于把类注入到Spring中进行管理

这就是基本的springt boot 的使用了.其他的以后可以慢慢看,一边看,一边学,也没有什么的难的.都是以前Spring的东西,只是进行了整合.

以为现在公司是用的play framework.所以主流的spring ,spring mvc,mybatis 框架用的比较少了.现在整合后用起来还是很方便的.哈哈.

想想进这家公司钱我都可以手搭配出SSM的框架,现在已经都忘记的差不多了,哈哈哈.

现在在研究play2的框架,以及使用sbt进行项目的搭建,还有点弄,准备在研究下java怎么方便的调用scala 方法的....

昭君下午应该去保养车子了.

今天就写这么多了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: