From 79eced21e7be8baa8a5f2c11a638e98c787bd2ae Mon Sep 17 00:00:00 2001 From: luffy9412 <1029559041@qq.com> Date: Tue, 19 Sep 2017 21:13:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9org/pqh/core/util/DBAction.ja?= =?UTF-8?q?va=E6=A0=87=E8=AF=86=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/pqh/core/config/HibernateConfig.java | 18 ++++++++++-- .../org/pqh/core/service/BaseService.java | 14 ++++----- .../main/java/org/pqh/core/util/DBAction.java | 29 ++++++++++++++++--- .../achieve/controller/BaseController.java | 2 +- ...hibernate.properties => config.properties} | 4 +-- 5 files changed, 50 insertions(+), 17 deletions(-) rename web/src/main/resources/{hibernate.properties => config.properties} (79%) diff --git a/core/src/main/java/org/pqh/core/config/HibernateConfig.java b/core/src/main/java/org/pqh/core/config/HibernateConfig.java index 6dc5a3e..ec55145 100644 --- a/core/src/main/java/org/pqh/core/config/HibernateConfig.java +++ b/core/src/main/java/org/pqh/core/config/HibernateConfig.java @@ -14,25 +14,32 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.annotation.PostConstruct; import java.io.IOException; +import java.util.Properties; /** * Created by reborn on 2017/7/28. */ @Configuration -@PropertySource("classpath:hibernate.properties") +@PropertySource("classpath:config.properties") @EnableTransactionManagement public class HibernateConfig{ private Logger log= LogManager.getLogger(); + @Value("${hibernate.connection.pool_size}") + private String pool_size; + @Value("${hibernate.jdbc.fetch_size}") + private String fetch_size; + @Value("${hibernate.dialect}") + private String dialect; + @Value("${hibernate.connection.driver_class}") + private String driver; @Value("${hibernate.connection.url}") private String url; @Value("${hibernate.connection.username}") private String username; @Value("${hibernate.connection.password}") private String password; - @Value("${hibernate.connection.driver_class}") - private String driver; private LocalSessionFactoryBean sessionFactoryBean; @@ -49,9 +56,14 @@ public class HibernateConfig{ public void init() throws IOException { log.info("数据库配置初始化\nurl="+url+"\nusername="+username+"\npassword="+password); sessionFactoryBean=new LocalSessionFactoryBean(); + Properties properties=new Properties(); + properties.setProperty("hibernate.connection.pool_size",pool_size); + properties.setProperty("hibernate.jdbc.fetch_size",fetch_size); + properties.setProperty("hibernate.dialect",dialect); sessionFactoryBean.setDataSource(basicDataSource()); sessionFactoryBean.setPackagesToScan("org.pqh.core.model"); sessionFactoryBean.afterPropertiesSet(); + } @Bean("transactionManager") diff --git a/core/src/main/java/org/pqh/core/service/BaseService.java b/core/src/main/java/org/pqh/core/service/BaseService.java index 4fcd70f..40a39a0 100644 --- a/core/src/main/java/org/pqh/core/service/BaseService.java +++ b/core/src/main/java/org/pqh/core/service/BaseService.java @@ -24,22 +24,22 @@ public class BaseService { Class tClass = (Class) formModel.getClass(); try { switch (action) { - case Insert: - case Update: + case C: + case U: baseDao.saveOrUpdate(formModel); break; - case Select: + case R: T model = baseDao.get(tClass, formModel.primaryKey()); return model == null ? "找不到主键=" + formModel.primaryKey() + "" : model.toString(); - case Delete: + case D: baseDao.delete(tClass, formModel.primaryKey()); break; } - return action + " success"; + return action.getEn() + " success"; } catch (Exception e) { - log.error(formModel.tableNote() + "进行" + action + "操作失败,原因是:" + e.getMessage()); + log.error(formModel.tableNote() + "进行" + action.getCh() + "操作失败,原因是:" + e.getMessage()); } - return action + " fail"; + return action.getEn() + " fail"; } } diff --git a/core/src/main/java/org/pqh/core/util/DBAction.java b/core/src/main/java/org/pqh/core/util/DBAction.java index 4ded1ec..033403c 100644 --- a/core/src/main/java/org/pqh/core/util/DBAction.java +++ b/core/src/main/java/org/pqh/core/util/DBAction.java @@ -4,8 +4,29 @@ package org.pqh.core.util; * Created by reborn on 2017/8/2. */ public enum DBAction { - Insert, - Update, - Delete, - Select + + C("创建","Create"), + + U("更新","Update"), + + R("读取","Retrieve"), + + D("删除","Delete"); + + private String ch; + + private String en; + + DBAction(String ch, String en) { + this.ch = ch; + this.en = en; + } + + public String getCh() { + return ch; + } + + public String getEn() { + return en; + } } diff --git a/web/src/main/java/org/pqh/achieve/controller/BaseController.java b/web/src/main/java/org/pqh/achieve/controller/BaseController.java index 864d980..0946c94 100644 --- a/web/src/main/java/org/pqh/achieve/controller/BaseController.java +++ b/web/src/main/java/org/pqh/achieve/controller/BaseController.java @@ -26,7 +26,7 @@ public class BaseController { @ResponseBody @RequestMapping(value = "{action}", produces = "text/html;charset=UTF-8") public String curd(@PathVariable DBAction action, T model) { - log.info(model.tableNote() + "进行" + action + "操作请求"); + log.info(model.tableNote() + "进行" + action.getCh() + "操作请求"); return baseService.curd(action, model); } diff --git a/web/src/main/resources/hibernate.properties b/web/src/main/resources/config.properties similarity index 79% rename from web/src/main/resources/hibernate.properties rename to web/src/main/resources/config.properties index c4d9886..160cf9f 100644 --- a/web/src/main/resources/hibernate.properties +++ b/web/src/main/resources/config.properties @@ -2,6 +2,6 @@ hibernate.connection.pool_size=5 hibernate.jdbc.fetch_size=50 hibernate.dialect=org.hibernate.dialect.MySQL57Dialect hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver +hibernate.connection.url=jdbc:mysql://localhost:3306/bilibili?serverTimezone=UTC&&useSSL=true hibernate.connection.username=root -hibernate.connection.password=123456 -hibernate.connection.url=jdbc:mysql://localhost:3306/bilibili?serverTimezone=UTC&&useSSL=true \ No newline at end of file +hibernate.connection.password=0d4h0a3a