摘要:本文主要向大家介绍了JAVA语言之spring boot 整合JPA,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
本文主要向大家介绍了JAVA语言之spring boot 整合JPA,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
代码实现
1. 修改pom,引入依赖
<!-- 引入jpa 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2. 修改application.properties,配置相关信息
#修改tomcat默认端口号
server.port=8090
#修改context path
server.context-path=/test
#配置数据源信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
#配置jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true
3. 创建实体类
package com.study.entity;
import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;
@Entity
@Table(name="t_user")public class User {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String userName;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4. 创建repository接口并继承CrudRepository
package com.study.repository;
import org.springframework.data.jpa.repository.Query;import org.springframework.data.repository.CrudRepository;import org.springframework.data.repository.query.Param;
import com.study.entity.User;
/**
* 注意:
* 1.这里这里是interface,不是class
*
* 2.CrudRepository里面的泛型,第一个是实体类,第二个是主键的类型
*
* 3.由于crudRepository 里面已经有一些接口了,如deleteAll,findOne等, 我们直接调用即可
*
* 4.当然,我们也可以根据自己的情况来实现自己的接口,如下面的getUser()方法,jpql语句和hql语句差不多
*
* */public interface UserRepository extends CrudRepository<User, Integer> {
/**
* 我们这里只需要写接口,不需要写实现,spring boot会帮忙自动实现
*
* */
@Query("from User where id =:id ")
public User getUser(@Param("id") Integer id);
}
5. 创建service
1. 接口
package com.study.service;
import com.study.entity.User;
public interface UserService {
public User getUser(Integer id);
}
2. 实现
package com.study.service.impl;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;
import com.study.entity.User;import com.study.repository.UserRepository;import com.study.service.UserService;
@Servicepublic class UserServiceImpl implements UserService {
@Autowired
UserRepository repository;
@Override
public User getUser(Integer id) {
//有两种方式:
//1.调用crudRepository的接口// return repository.findOne(id);
//2.调用我们自己写的接口
return repository.getUser(id);
}
}
6. 创建controller
package com.study.controller;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;
import com.study.entity.User;import com.study.service.UserService;
@RestControllerpublic class UserController {
@Autowired
UserService service;
@RequestMapping("/getUser/{id}")
public User getUser(@PathVariable("id") Integer id){
return service.getUser(id);
}
}
7. 测试,页面以json格式显示数据库值
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号