SpringBoot入门:(2)jpa方式操作数据库
2017-09-26 18:13
441 查看
导入jar ,在pom.xml中添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
在appilication.yml中添加数据库配置:
spring: profiles: active: dev datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/dbvc?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8 username: root password: 123 jpa: hibernate: ddl-auto: create show-sql: true
注:ddl_auto: create 代表在数据库创建表,update 代表更新,首次启动需要create ,如果你想通过hibernate 注解的方式创建数据库的表的话,之后需要改为 update.
spring:
profiles:
active: 用来表示多配置文件时,选择哪一个配置文件
创建一个实体
package com.vc.im.model.entity; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class ThirdPartyUser { @Id @GeneratedValue private Integer id; private String name; private String openid; private String accessToken; private String refreshToken; private String mediaType; private Date createTime; private Date updateTime; 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 == null ? null : name.trim(); } public String getOpenid() { return openid; } public void setOpenid(String openid) { this.openid = openid == null ? null : openid.trim(); } public String getAccessToken() { return accessToken; } public void setAccessToken(String accessToken) { this.accessToken = accessToken == null ? null : accessToken.trim(); } public String getRefreshToken() { return refreshToken; } public void setRefreshToken(String refreshToken) { this.refreshToken = refreshToken == null ? null : refreshToken.trim(); } public String getMediaType() { return mediaType; } public void setMediaType(String mediaType) { this.mediaType = mediaType == null ? null : mediaType.trim(); } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } }
创建Dao接口, springboot 将接口类会自动注解到spring容器中,不需要我吗做任何配置,只需要继承JpaRepository 即可
package com.vc.im.dao; import org.springframework.data.jpa.repository.JpaRepository; import com 4000 .vc.im.model.entity.ThirdPartyUser; public interface ThirdPartyUserDao extends JpaRepository<ThirdPartyUser, Integer> { }
创建一个service层,接口和实现类.这里省略,就是把Dao 封装一下。在实现类上 加上 @Service 注解
如果需要事务的话,在service层加@Transaction注解即可。……
Controller 层
package com.vc.im.model.controller; import java.util.List; 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.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.vc.im.model.entity.ThirdPartyUser; import com.vc.im.model.service.IThirdPartyUserService; @RestController @RequestMapping("/") public class ThirdPartyUserController { @Autowired private IThirdPartyUserService thirdPartyUserService; /** * * @return */ @RequestMapping("getList") @ResponseBody public List<ThirdPartyUser> getList() { System.out.println("11111111111"); List<ThirdPartyUser> thirdPartyUsrtList = thirdPartyUserService.findAll(); return thirdPartyUsrtList; } /** * * @return */ @RequestMapping(value = "/add", method = RequestMethod.POST) public ThirdPartyUser add(ThirdPartyUser user) { return thirdPartyUserService.save(user); } }
启动类
package com.vc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @SpringBootApplication public class TestController { @ResponseBody @RequestMapping(value = "/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(TestController.class, args); } }
参考:2小时学会springboot
相关文章推荐
- SpringBoot入门(三)--数据库操作&&Spring-data-jpa的使用
- • Spring Boot 进 阶- 数 据 访 问-使用spring-data-jpa 操作数据库
- 02_Spring Boot 数据库操作 [基于Spring Boot、Mysql、Jpa]
- springboot快速入门(四)——数据库操作
- springboot JPA操作数据库
- Spring Boot - JPA操作数据库
- SpringBoot入门-6(利用jpa连接hibernate,并进行生成表,对表的增加,删除,查询操作)
- SpringBoot-创建RESTful风格的 http接口访问jpa 来操作数据库
- Spring Boot快速入门(五):使用MyBatis(注解形式)进行数据库操作
- springBoot+Jpa(hibernate)数据库基本操作
- SpringBoot学习之路:03.Spring Boot使用Jpa操作数据库
- 170622、springboot编程之JPA操作数据库
- 在spring Boot中使用Spring-data-jpa操作数据库
- SpringBoot学习(五)操作数据库Spring-Data-JPA
- 在spring Boot中使用Spring-data-jpa操作数据库
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- SpringBoot 使用JPA操作数据库
- Spring Boot入门(3)-数据库操作
- SpringBoot使用JPA操作数据库
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作