码迷,mamicode.com
首页 > 数据库 > 详细

【Spring boot】第一个项目 Springboot + mysql + hibernate

时间:2018-06-19 17:13:33      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:ddl   put   detail   stc   att   用法   character   persist   frame   

 

今天公司要做一个小项目,好久没碰项目了(刷题好累。。。),听说spring boot很火,决定试一试。暂时就从mysql里面读数据好了,使用hiberante。

1.获取jar包。

从http://start.spring.io/获取,当然对于使用eclipse(离不开。。。)的同学,有STS插件支持,关于插件用法自行百度,很简单。关键的是选择要支持的特性。这里是读数据,我记得只选择了web,jpa和mysql三个标签。

2.导入工程。

新建实体类:

[html] view plain copy
  1. package com.example.demo;  
  2.   
  3. import javax.persistence.Entity;  
  4. import javax.persistence.GeneratedValue;  
  5. import javax.persistence.Id;  
  6.   
  7. @Entity  
  8. public class Person {  
  9.   
  10.     @Id  
  11.     @GeneratedValue  
  12.     private Long id;  
  13.       
  14.     private String name;  
  15.       
  16.     private String address;  
  17.   
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.   
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.   
  26.     public String getAddress() {  
  27.         return address;  
  28.     }  
  29.   
  30.     public void setAddress(String address) {  
  31.         this.address = address;  
  32.     }  
  33.       
  34. }  
要注意@Entity来自javax包。

Dao类:

 

[html] view plain copy
  1. package com.example.demo;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.data.jpa.repository.JpaRepository;  
  6.   
  7. public interface Dao extends JpaRepository<Person, Long>{  
  8.   
  9.     List<Person> findByName(String name);  
  10. }  
只需要继承一个jpa的类即可。而且不需要实现,提供默认的查找方法。。。很方便。

controller

 

[html] view plain copy
  1. package com.example.demo;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.boot.SpringApplication;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8. @RestController  
  9. @SpringBootApplication  
  10. public class Demo2Application {  
  11.     @Autowired  
  12.     Dao dao;  
  13.       
  14.     @RequestMapping("/get")  
  15.     public Person getP(String name){  
  16.         Person person = dao.findByName(name).get(0);  
  17.         return person;  
  18.     }  
  19.     @RequestMapping("/")  
  20.     public String index(){  
  21.         return "Hello Spring Boot";  
  22.     }  
  23.     public static void main(String[] args) {  
  24.         SpringApplication.run(Demo2Application.class, args);  
  25.     }  
  26. }  

只添加一个查数据的url。

最后是配置文件:

[html] view plain copy
  1. spring.datasource.driver-class-name=com.mysql.jdbc.Driver  
  2. spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8  
  3. spring.datasource.username=root  
  4. spring.datasource.password=  
  5.   
  6. spring.jpa.hibernate.ddl-auto=update;  
  7. spring.jpa.show-sql=true  
  8. spring.jackson.serialization.indent-output=true  

访问之前,现在数据库中建表,插数据。

然后启动,浏览器访问。技术分享图片

 

浏览器:
技术分享图片

说明访问成功。

 

整体感觉就是,这个框架帮程序员实现了太多功能,原本光一个hibernate就需要N多配置,但是在springboot中完全没有感知hibernate的存在。有时间需要研究下源码,看看他是怎么做到的。

这个简单的demo就到此结束了。

 

【Spring boot】第一个项目 Springboot + mysql + hibernate

标签:ddl   put   detail   stc   att   用法   character   persist   frame   

原文地址:https://www.cnblogs.com/williamjie/p/9199306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!