2019独角兽企业重金招聘Python工程师标准>>>

1.maven引入配置

 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot JPA --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- MYSQL --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency></dependencies>

2.prpperties配置

#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/ncdg
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#jpa
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop

3.写entity.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;/**** @author lili*/
@Entitypublic class User {@Id@GeneratedValueprivate long id;private String username;private String password;private String account;private String position;private String levels;private Long userId;/*** @return the id*/public long getId() {return id;}/*** @param id the id to set*/public void setId(long id) {this.id = id;}/*** @return the username*/public String getUsername() {return username;}/*** @param username the username to set*/public void setUsername(String username) {this.username = username;}/*** @return the password*/public String getPassword() {return password;}/*** @param password the password to set*/public void setPassword(String password) {this.password = password;}/*** @return the account*/public String getAccount() {return account;}/*** @param account the account to set*/public void setAccount(String account) {this.account = account;}/*** @return the position*/public String getPosition() {return position;}/*** @param position the position to set*/public void setPosition(String position) {this.position = position;}/*** @return the levels*/public String getLevels() {return levels;}/*** @param levels the levels to set*/public void setLevels(String levels) {this.levels = levels;}/*** @return the userId*/public Long getUserId() {return userId;}/*** @param userId the userId to set*/public void setUserId(Long userId) {this.userId = userId;}};

4.写接口repository接口

import com.mycompany.ncdg.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;/**** @author lili*/
public interface UserRepository extends JpaRepository<User, Long> {@Query("from User u where u.username=:username")User findUser(@Param("username") String username);}

5.写controller

import com.mycompany.ncdg.entity.User;
import com.mycompany.ncdg.repository.UserRepository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/**** @author lili*/
@RestController
public class UserController {@AutowiredUserRepository userRepository;@RequestMapping("/userall")public List<User> all(){return userRepository.findAll();};
}

6.启动springboot的主类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/**** @author lili*/
@SpringBootApplication
public class App {/*** @param args the command line arguments*/public static void main(String[] args) {// TODO code application logic hereSpringApplication.run(App.class, args);}}

7.启动成功后 ,访问localhost:8080/userall   得到数据格式如下

[{"id":1,"username":"超级管理员","password":"123456","account":"admin","position":"1","levels":"1","userId":1}]

转载于:https://my.oschina.net/u/2428630/blog/1612116

springboot . 配置jpa使用相关推荐

  1. SpringBoot 配置 JPA

    新建项目 在application.properties配置文件中进行配置(或者application.yaml中配置也行) spring.datasource.url=jdbc:mysql://lo ...

  2. 带你搭一个SpringBoot+SpringData JPA的环境

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 不知道大家对SpringBoot和Spring Da ...

  3. Java项目:嘟嘟图片展览馆管理系统(java+Springboot+SpringMVC+JPA+Jsp+maven+mysql)

    源码获取:博客首页 "资源" 里下载! 一.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclisp ...

  4. Java项目:考试管理系统(java+Springboot+Maven+Jpa+Vue+Mysql)

    源码获取:博客首页 "资源" 里下载! 一.项目简述 本系统功能包括: 支持单选题.多选题.判断题支持学生(student).教师(teacher).管理员(admin)三种角色学 ...

  5. SpringBoot配置属性之NOSQL

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...

  6. SpringBoot配置属性之DataSource

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...

  7. idea加入springboot插件_带你搭一个SpringBoot+SpringData JPA的环境

    前言 只有光头才能变强. 不知道大家对SpringBoot和Spring Data JPA了解多少,如果你已经学过Spring和Hibernate的话,那么SpringBoot和SpringData ...

  8. (二)SpringBoot 整合 JPA

    一.JPA简介 JPA(java persistence api)并不是一个框架,而是一组规范.Hibernate.TopLink.OpenJPA都实现了JPA规范,不过Hinernate最成功).所 ...

  9. SpringBoot与JPA

    SpringBoot与JPA集成: 简介 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对 ...

最新文章

  1. Python 判断当前数值的类型(比如x=10 获取x的类型)
  2. 二叉树的层次遍历 II[LeetCode-107]
  3. 判读一个对象不为空_LabVIEW面向对象编程_初窥门径(3):开发实践
  4. Tigase XMPP Server的安装
  5. 冬天了,麦克风/话筒 有杂音 的原因!
  6. 最近用到的几种前后端交互方式
  7. 导入jQuery库时应该注意的问题
  8. 仿京东左侧菜单弹出html代码,相仿京东左侧菜单
  9. java中日期加减计算(转)
  10. python url拼接_详解Python urlencode编码和url拼接方法
  11. Linux下启动、停止J2SE程序(脚本)
  12. 移动端常见兼容性问题及解决办法
  13. MPI集群安装、MPI安装
  14. 漫谈程序员系列 软件开发的十八般乐趣
  15. 小学生计算机课如何上,如何上好小学信息技术课
  16. Android Studio打造一个小说阅读App
  17. 怎么在小程序里开店铺?【小程序开店】
  18. 机器学习算法快速上手-python语言与numpy库
  19. 给定两个有着相同长度且都在字典内的单词,要求写一个方法来把一个单词变型成另一个单词。 一次只能转换一个字母,且每次生成的单词必须在字典内...
  20. oracle数据库ogg延迟,oracle goldengate ogg 源段传输进程lag延迟不断增加的原因?

热门文章

  1. bzoj千题计划219:bzoj1568: [JSOI2008]Blue Mary开公司
  2. 04 ffmpeg 从媒体文件解封装,输出YUV420图像
  3. mysql中Table is read only 的解决方法小结
  4. MySQL复制类型介绍
  5. Django在Win7下安装与创建项目hello word示例
  6. 未分配内存的指针导致段错误
  7. php 的adodb分页类
  8. 使用Response.ContentType 来控制下载文件的类型
  9. 【原拼】从/etc/inetd.conf学习服务(1)
  10. H3C交换机引发的奇葩故障