1.参考教程: http://412887952-qq-com.iteye.com/category/356333?page=8
8. 使用JPA保存数据【从零开始学Spring Boot】
注意问题:
:问题1:需要安装一个mysql
问题2:mysql5.7以上版本,必须配置ssl
spring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=yes
问题3:mysql5.7以上,naming-strategy配置也改变了
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
问题4:application.properties配置项,记住删掉末尾的空格(巨坑)
问题5: spring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=yes
test是数据库名,你要自己在mysql中创建一个数据库
解决方法:
1. ctrl+R 打开cmd命令行
2. mysql -u root -p 进入mysql输入密码
3. create database test;
问题6:access denied(数据库不允许远程访问)
解决方法: 1.登录数据库, mysql -u root -p
2.输入你想授权的账号名(替换myuser)和密码(替换mypassword):
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
3.输入命令: FLUSH PRIVILEGES; 然后就能访问了
application.properties配置示例:
spring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=yes
spring.datasource.username = admin
spring.datasource.password = admin
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
POM.XML配置示例:
4.0.0
com.example
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.springframework.boot
spring-boot-maven-plugin
28. SpringBoot启动时的Banner设置【从零开始学Spring Boot】
注意问题
可以在代码中配置关闭banner,也可以在properties中配置
spring.main.banner-mode=off
29. Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】
注意问题
文件上传到指定路径,一定要是文件,如果是目录就会出现拒绝访问。
String path="F:/test/";
File savedir = new File(path);
if (!savedir.exists()) {
savedir.mkdirs();
}
File myFile=new File(path+"1.txt");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(myFile));
33. Spring Boot 监控和管理生产环境【从零开始学Spring Boot】
配置actuator需要security权限
可以再properties文件中设置关闭
management.security.enabled=false