数据准备

创建Docker

  1. docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

查看docker内容

  1. $ docker exec -it mysql mysql -uroot -p123456
  2. mysql> CREATE DATABASE test;
  3. mysql>DROP TABLE IF EXISTS `user`;
  4. mysql>CREATE TABLE `user` (
  5. `id` int(11) NOT NULL AUTO_INCREMENT,
  6. `name` varchar(128) NOT NULL,
  7. `password` varchar(128) NOT NULL,
  8. PRIMARY KEY (`id`)
  9. )
  10. mysql>INSERT INTO `user` VALUES (1, 'xiaoming', '123456');
  11. mysql>INSERT INTO `user` VALUES (2, 'xiaohong', '654321');

查看最终数据

  1. mysql> use test;
  2. mysql> SELECT id,name,password FROM user;
  3. +----+----------+----------+
  4. | id | name | password |
  5. +----+----------+----------+
  6. | 1 | xiaoming | 123456 |
  7. | 2 | xiaohong | 654321 |
  8. +----+----------+----------+
  9. 2 rows in set (0.01 sec)
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-jdbc</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>mysql</groupId>
  12. <artifactId>mysql-connector-java</artifactId>
  13. <scope>runtime</scope>
  14. </dependency>
  15. </dependencies>

配置菜单

  1. spring:
  2. datasource:
  3. username: root
  4. password: baxiang
  5. url: jdbc:mysql://127.0.0.1:3306/user?useSSL=false
  6. driver-class-name: com.mysql.jdbc.Driver
  7. schema:
  8. - classpath:department.sql

多数据源配置

JDBC连接池

druid Filter

JDBC Template

query
queryForObject
queryForList
update
插入修改删除
execute

事务抽象

一致的事务模型

JDBC/Hibernate/myBatis
DataSource/JTA

核心接口