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);
}
本文介绍如何在SpringBoot中正确配置MySQL连接,包括不同版本的驱动配置细节,以及SpringData JPA的基本使用方法。

被折叠的 条评论
为什么被折叠?



