From 1befcc5141ec8c996770d6699bf7e8dd830dad42 Mon Sep 17 00:00:00 2001
From: "Yangkai.Shen" <237497819@qq.com>
Date: Fri, 11 May 2018 16:42:39 +0800
Subject: [PATCH] =?UTF-8?q?spring=20boot=20=E9=9B=86=E6=88=90=20jdbc=20tem?=
=?UTF-8?q?plate=20=E5=AE=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
spring-boot-demo-orm-jdbctemplate/README.md | 193 ++++++++++++++++++
spring-boot-demo-orm-jdbctemplate/pom.xml | 36 ++++
...ingBootDemoOrmJdbctemplateApplication.java | 12 ++
.../controller/UserController.java | 94 +++++++++
.../model/User.java | 27 +++
.../service/UserService.java | 34 +++
.../service/impl/UserServiceImpl.java | 84 ++++++++
.../springbootdemoormjdbctemplate/vo/R.java | 29 +++
.../src/main/resources/application.yml | 49 +++++
.../src/main/resources/init-sql/schema.sql | 13 ++
...otDemoOrmJdbctemplateApplicationTests.java | 16 ++
spring-boot-demo-parent/pom.xml | 1 +
12 files changed, 588 insertions(+)
create mode 100644 spring-boot-demo-orm-jdbctemplate/README.md
create mode 100644 spring-boot-demo-orm-jdbctemplate/pom.xml
create mode 100644 spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/springbootdemoormjdbctemplate/SpringBootDemoOrmJdbctemplateApplication.java
create mode 100644 spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/springbootdemoormjdbctemplate/controller/UserController.java
create mode 100644 spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/springbootdemoormjdbctemplate/model/User.java
create mode 100644 spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/springbootdemoormjdbctemplate/service/UserService.java
create mode 100644 spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/springbootdemoormjdbctemplate/service/impl/UserServiceImpl.java
create mode 100644 spring-boot-demo-orm-jdbctemplate/src/main/java/com/xkcoding/springbootdemoormjdbctemplate/vo/R.java
create mode 100644 spring-boot-demo-orm-jdbctemplate/src/main/resources/application.yml
create mode 100644 spring-boot-demo-orm-jdbctemplate/src/main/resources/init-sql/schema.sql
create mode 100644 spring-boot-demo-orm-jdbctemplate/src/test/java/com/xkcoding/springbootdemoormjdbctemplate/SpringBootDemoOrmJdbctemplateApplicationTests.java
diff --git a/spring-boot-demo-orm-jdbctemplate/README.md b/spring-boot-demo-orm-jdbctemplate/README.md
new file mode 100644
index 0000000..99ce53c
--- /dev/null
+++ b/spring-boot-demo-orm-jdbctemplate/README.md
@@ -0,0 +1,193 @@
+# spring-boot-demo-orm-jdbctemplate
+依赖 [spring-boot-demo-parent](../spring-boot-demo-parent)、spring-boot-starter-jdbc
+
+## pom.xml
+
+```xml
+
+
+ 4.0.0
+
+ spring-boot-demo-orm-jdbctemplate
+ 0.0.1-SNAPSHOT
+ jar
+
+ spring-boot-demo-orm-jdbctemplate
+ Demo project for Spring Boot
+
+
+ com.xkcoding
+ spring-boot-demo-parent
+ 0.0.1-SNAPSHOT
+ ../spring-boot-demo-parent/pom.xml
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-jdbc
+
+
+ com.alibaba
+ druid-spring-boot-starter
+ 1.1.9
+
+
+
+
+ spring-boot-demo-orm-jdbctemplate
+
+
+
+```
+
+## application.yml
+
+```yaml
+server:
+ port: 8080
+ context-path: /demo
+spring:
+ datasource:
+ # 启动时自动运行的 SQL 文件
+ schema: classpath:init-sql/schema.sql
+ continue-on-error: true
+ druid:
+ url: jdbc:mysql://localhost:3306/spring-boot-demo?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
+ username: root
+ password: root
+ driver-class-name: com.mysql.jdbc.Driver
+ # 连接池配置
+ # 初始化大小,最小,最大
+ initialSize: 5
+ minIdle: 5
+ maxActive: 20
+ # 配置获取连接等待超时的时间
+ maxWait: 60000
+ # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
+ timeBetweenEvictionRunsMillis: 60000
+ # 配置一个连接在池中最小生存的时间,单位是毫秒
+ minEvictableIdleTimeMillis: 300000
+ validationQuery: SELECT 1 FROM DUAL
+ testWhileIdle: true
+ testOnBorrow: false
+ testOnReturn: false
+ # 打开PSCache,并且指定每个连接上PSCache的大小
+ poolPreparedStatements: true
+ maxPoolPreparedStatementPerConnectionSize: 20
+ # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
+ filters: stat,wall,log4j
+ # 监控配置
+ web-stat-filter:
+ enabled: true
+ url-pattern: /*
+ exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
+ stat-view-servlet:
+ enabled: true
+ url-pattern: /sys/druid/*
+ reset-enable: fasle
+ login-username: xkcoding
+ login-password: 123456
+ filter:
+ stat:
+ log-slow-sql: true
+ slow-sql-millis: 5000
+ merge-sql: true
+```
+
+## schema.sql
+
+```mysql
+SET FOREIGN_KEY_CHECKS=0;
+
+DROP TABLE IF EXISTS `jdbctemplate_user`;
+CREATE TABLE `jdbctemplate_user` (
+ `id` int(11) NOT NULL AUTO_INCREMENT,
+ `name` varchar(32) DEFAULT NULL,
+ `tel` varchar(11) DEFAULT NULL,
+ `create_time` datetime DEFAULT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+INSERT INTO `jdbctemplate_user` VALUES (1, 'klay', '13799008800', '2017-11-13 16:04:39');
+INSERT INTO `jdbctemplate_user` VALUES (2, 'Tome', '18988991234', '2017-11-13 16:13:28');
+```
+
+## UserServiceImpl.java
+
+```java
+/**
+ *
+ * UserServiceImpl
+ *
+ *
+ * @package: com.xkcoding.springbootdemoormjdbctemplate.service.impl
+ * @description: UserServiceImpl
+ * @author: yangkai.shen
+ * @date: Created in 2018/5/11 下午3:27
+ * @copyright: Copyright (c) 2018
+ * @version: V1.0
+ * @modified: yangkai.shen
+ */
+@Service
+public class UserServiceImpl implements UserService {
+ @Autowired
+ private JdbcTemplate jdbcTemplate;
+
+ @Override
+ public Integer save(User user) {
+ String sql = "INSERT INTO jdbctemplate_user (name,tel,create_time) VALUES(?,?,?)";
+ return jdbcTemplate.update(sql, user.getName(), user.getTel(), user.getCreateTime());
+ }
+
+ @Override
+ public Integer update(User user) {
+ String sql = "UPDATE jdbctemplate_user SET name = ?,tel = ? where id = ?";
+ return jdbcTemplate.update(sql, user.getName(), user.getTel(), user.getId());
+ }
+
+ @Override
+ public Integer delete(User user) {
+ String sql = "DELETE FROM jdbctemplate_user where id = ?";
+ return jdbcTemplate.update(sql, user.getId());
+ }
+
+ @Override
+ public User findById(Integer id) {
+ String sql = "SELECT * FROM jdbctemplate_user where id = ?";
+ RowMapper rowMapper = new BeanPropertyRowMapper(User.class);
+ return jdbcTemplate.queryForObject(sql, new Object[]{id}, rowMapper);
+ }
+
+ @Override
+ public User findByName(String name) {
+ String sql = "SELECT * FROM jdbctemplate_user where name = ?";
+ RowMapper rowMapper = new BeanPropertyRowMapper(User.class);
+ return jdbcTemplate.queryForObject(sql, new Object[]{name}, rowMapper);
+ }
+
+ @Override
+ public List findAll() {
+ String sql = "SELECT * FROM jdbctemplate_user";
+ List