SpringDataJpa

本文介绍如何在SpringBoot中正确配置MySQL连接,包括不同版本的驱动配置细节,以及SpringData JPA的基本使用方法。

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

1.配置文件

springboot 2.x修改前端页面不能自动热更新,可以Ctrl+F9强制更新
com.mysql.jdbc.Driver和mysql-connector-java 5一起用。
com.mysql.cj.jdbc.Driver和mysql-connector-java 6 一起用。
在url上:

  • mysql5的是只需要设置Unicode,Encoding和SSL
    url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false

  • mysql6还需要设置serverTimezone的时区,必须设置为所在地区的,可以用Shanghai
    url=jdbc:mysql://localhost:3306/test?serverTimezone=Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false

spring.datasource.url=jdbc:mysql://localhost:3306/day11a
spring.datasource.username=root
spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#MySQL jdbc 6.0 版本以上必须配置serverTimezone
jdbc.url = jdbc:mysql://localhost:3306/数据库?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
# 自动建表 update:表示有表则直接使用,无表就新建
spring.jpa.hibernate.ddl-auto=update

# 表示在操作时,输出sql语句
spring.jpa.show-sql=true

2. 代码

@Repository
public interface IPersonDao extends JpaRepository<Person,Integer> {

   //自定义一个方法
   Person findByNameAndSex(String name,String sex);

   @Query("from Person  where  id= ?")
   Person selectPerson(int id);
}

3.SpringDataJpa方法名称命名规则

1.使用规则
findBy(关键字)+属性名称(属性名称的首字母大写)+查询条件(首字母大写)。
除了findBy外还有其他的前缀

4.自定义方法名

在以后执行多表查询的时候,才需要

  • Dao接口
public interface MyRepository extends Repository<User, Integer> {


	@Query("from User  where uid = ?1") //?1 表示取第一个参数uid 来替代这个?。
	User selectUser(int uid);

}

  • Service
public interface UserService {

	User selectUser(int uid);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值