在 Spring Boot 中使用 JPA 和 MySQL

本文介绍了在Spring Boot项目中使用JPA和MySQL进行数据访问的步骤,包括添加依赖、配置数据库连接、创建实体类和CrudRepository,以及如何进行CRUD操作。通过实例展示了如何简化数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Spring Boot中使用JPA和MySQL

最近项目中需要使用到MySQL数据库,在此记录一下Spring Boot中使用JPA进行数据访问的基本过程。
本文的基本开发环境如下:spring-boot-1.4.2 & jdk-1.8 & spring-data-jpa-1.10.5 & mysql-connector-java-5.1.40

  1. pom.xml中加入JPAMySQL依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

2.配置MySQL连接及JPA参数
我的配置项是通过application.yml文件进行设置的,如下:

# yml里面支持的配置项参数还有很多,可以在yml文件中输入关键词后查看相关用法,这里就不解释了
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/demo
    username: root
    password: 123456
    dbcp:
      validation-query: SELECT 1
      test-while-idle: true
  jpa:
    hibernate:
      ddl-auto: create-drop
      naming:
        strategy: org.hibernate.cfg.ImprovedNamingStrategy
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5Dialect
    show-sql: true

Spring Boot应用在启动时会自动去解析此配置文件里面的内容,不需要我们再手动显式的在Java或者XML中进行配置了。
3. 创建实体类
创建一个简单的实体类用于演示:

@Entity
@Table(name = "label")
public class Label {
	
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
	private long id;
	
	/**
	 * 标签的内容
	 */
	private String label;

	public String getLabel() {
		return label;
	}

	public void setLabel(String id, String label) {
		this.labelId = id;
		this.label = label;
	}

}

4.创建实体类的CrudRepository

@Transactional
public interface LabelRepository extends MongoRepository<Label, String>{
	
	/**
	 * 根据标签文本查询标签是否已存在
	 * @param label 标签文本
	 * @return
	 */
	Label findByLabel(String label);

}

5.使用Repository进行基本的CRUD

@RestController
public class LabelController {

    @Autowired
    private LabelRepository labelRepository;

    /**
    * 根据id获取指定标签
    * @param id 标签id
    * @return Label
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public RestResponse<Label> getById(@PathVariable("id") String id){
        Label label = labelRepository.findOne(id);
        if(label == null){
            return RestResponse.bad(-10001, "label is not exsist ");
        }
        return RestResponse.good(label);	
    }

    /**
    * 根据标签内容label获取指定标签
    * @param label 标签内容label
    * @return Label
    */
    @RequestMapping(method = RequestMethod.GET, params = {"label"})
    public RestResponse<Label> getByLabel(@RequestPrama("label") String label){
        Label label = labelRepository.findByLabel(label);
        if(label == null){
            return RestResponse.bad(-10001, "label is not exsist ");
        }
        return RestResponse.good(label);
    }

}

基本使用流程就是这样的!当然,这里面还有很多很多细节,随便拿一个出来都可以研究半天的,比如说Repository接口里面使用@Query支持自定义查询语句,使用@AttributeOverride注解关联内部类属性等等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值