Database configuration
1. Configuration code
server: port: 8001 #spring configurationspring: datasource: type: #The data source is druid driver-class-name: #Database Driver url: jdbc:mysql://localhost:3306/spring_cloud?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: root
<!--Mysqldrive--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.24</version> <scope>runtime</scope> </dependency> <!--alibabaDatabase data source--> <dependency> <groupId></groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency>
- SSL is:Secure Sockets Layer Secure Sockets Protocol.
- useSSL=true:It is generally safe verification through certificates or tokens.
- useSSL=false:It is to connect through the account password.
Notice:
- Versions after MySQL 5.7 must be added with useSSL=false;
- It was not used before, the default is false.
=true&characterEncoding=UTF-8
After setting, no matter what encoding the database is, it will be converted to UTF-8 when reading, and it will still be stored according to the database encoding when accessing.
Time zone configuration, MySQL8.0 or above version, url needs to add time zones.
like:
#East District of BeijingserverTimezone=GMT%2B8 #Shanghai Time ZoneserverTimezone=Asia/Shanghai
Basic configuration and reading
Configuration file format
3 configuration file formats are provided
- properties (traditional format/default format)
- yml (mainstream format)
- yaml
Configuration File
1. After importing the corresponding starter in SpringBoot, provide the corresponding configuration properties
2. Writing SpringBoot configuration is written in keywords + prompt form
The loading order of configuration files
properties priority>yml priority>yaml priority>yaml priority
The same configurations in different configuration files are overwritten according to the load priority, and all different configurations in different configuration files are retained.
yml file content
#Simple configurationserver: port: 80 #Object Configurationusers: name: zhangsan id: 18 #Simple list configuration, - followed by 1 spacelikes: - swim - Playing ball - read #The object in the list is key: value, don't forget the space after:books: [{name: Three Kingdoms,id: 435},{name: kettle,id: 6599}] #In the configuration file, you can use attribute name reference method to refer to attributesbaseDir: /usr/local/fire center: dataDir: ${baseDir}/data logDir: ${baseDir}/log #If escaped characters appear in the attribute value, you need to use double quotes to wrap itlesson: "Spring\tboot\nlesson"
Read yml configuration
5.1 Use @Value to read a single data, the attribute name reference method: ${level one attribute name.level two attribute name...}
@Value("${books[1].name}") private String bookName;
5.2 Use Environment objects to encapsulate all configuration information
- Encapsulate all configuration information using Environment objects
- Automatically assemble data into Environment object using @Autowired
@Autowired private Environment env; (("books[1].name"));
5.3 Custom object encapsulates specified data
- yml file
datasource: driver-class-name: url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC username: root password: root
- Custom Objects
@Component //bean object, managed by spring@ConfigurationProperties(prefix = "datasource") //Get the datasource attributes in the yml file, and need the get and set methods, omitted herepublic class MyDataSource { private String driverClassName; private String url; private String userName; private String password; }
- Where to use, use @Autowired to auto-assemble
@Autowired private MyDataSource myDataSource;
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.