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配置示例:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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