搭建第一个spring-boot程序

首先感谢csdn大神- 方志鹏 的博客,

当然我的石墨文档 : 

https://shimo.im/docs/XhiYlYAV1TYsrm34

 

首先 开始springboot 有两个方式。

1.在spring上面开发一个demo,然后下载zip文件然后解压导入开发工具即可

https://start.spring.io/

2. 也可以用idea构建。本案列采用idea.

@SpringBootApplication
public class FirstspringbootApplication {public static void main(String[] args) {SpringApplication.run(FirstspringbootApplication.class, args);}
}

然后在resource文件下面有一个application.yml文件里面,他是程序的配置文件,相当ssm里面的propertions。

server:port: 8080context-path: /springboot

然后便可编写demo文件了

@RestController     //等同于同时加上了@Controller和@ResponseBody
public class HelloController {//访问/hello或者/hi任何一个地址,都会返回一样的结果@RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)public String say(){return "hi you!!!";}
}

运行 Application的main(),呈现会启动,由于springboot自动内置了servlet容器,所以不需要类似传统的方式,先部署到容器再启动容器。只需要运行main()即可,这时打开浏览器输入网址:localhost:8080/项目名称/hi ,就可以在浏览器上看到: hi you!!!

 

测试属性

在yaml文件添加属性

server:port: 8080context-path: /springbootgirl:name: Bage: 18content: content:${name},age:${age}

在controller类添加value标签

@Value("${name}")private String name;

也可以通过ConfigurationProperties注解,将属性注入到bean中,通过Component注解将bean注解到spring容器中:

@ConfigurationProperties(prefix="girl")
@Component
public class GirlProperties {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

 

=================================================================================

通过jpa 方式操作数据库

导入jar包,在pom.xml中添加依赖

        org.springframework.bootspring-boot-starter-data-jpamysqlmysql-connector-java

在appilication.yml中添加数据库配置:

 datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/dbgirl?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8username: rootpassword: 123jpa:hibernate:ddl-auto: createshow-sql: true

请注意yaml文件标签的位置。因为相当严格。

这些都是数据库常见的一些配置没什么可说的,其中ddl_auto: create 代表在数据库创建表,update 代表更新,首次启动需要create ,如果你想通过hibernate 注解的方式创建数据库的表的话,之后需要改为 update.

创建一个实体girl,这是基于hibernate的:

@Entity
public class Girl {@Id@GeneratedValueprivate Integer id;private String cupSize;private Integer age;public Girl() {}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getCupSize() {return cupSize;}public void setCupSize(String cupSize) {this.cupSize = cupSize;}
}

创建Dao接口, springboot 将接口类会自动注解到spring容器中,不需要我吗做任何配置,只需要继承JpaRepository 即可:

//其中第二个参数为Id的类型
public interface GirlRep extends JpaRepository{}

创建一个GirlController,写一个获取所有girl的api和添加girl的api ,自己跑一下就可以了:


@RestController
public class GirlController {@Autowiredprivate GirlRep girlRep;/*** 查询所有女生列表* @return*/@RequestMapping(value = "/girls",method = RequestMethod.GET)public List getGirlList(){return girlRep.findAll();}/*** 添加一个女生* @param cupSize* @param age* @return*/@RequestMapping(value = "/girls",method = RequestMethod.POST)public Girl addGirl(@RequestParam("cupSize") String cupSize,@RequestParam("age") Integer age){Girl girl = new Girl();girl.setAge(age);girl.setCupSize(cupSize);return girlRep.save(girl);}} 

如果需要事务的话,在service层加@Transaction注解即可。

源码;http://download.csdn.net/detail/forezp/9778235

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部