From befc5d96dd39928060e0f5d85f5314921a1094d3 Mon Sep 17 00:00:00 2001 From: yidao620 Date: Fri, 2 Mar 2018 18:49:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81=E7=A4=BA?= =?UTF-8?q?=E4=BE=8Bspringboot-hibernate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +- .../src/main/resources/application.yml | 4 +- app-manage/src/main/resources/application.yml | 4 +- .../src/main/resources/application.yml | 2 +- springboot-hibernate/.gitignore | 10 + springboot-hibernate/LICENSE | 20 ++ springboot-hibernate/README.md | 28 ++ springboot-hibernate/pom.xml | 101 +++++++ springboot-hibernate/run.sh | 72 +++++ .../java/com/xncoding/pos/Application.java | 12 + .../xncoding/pos/config/DataSourceConfig.java | 33 +++ .../config/properties/DruidProperties.java | 249 ++++++++++++++++++ .../com/xncoding/pos/dao/entity/Article.java | 54 ++++ .../pos/dao/repository/IArticleDAO.java | 26 ++ .../pos/dao/repository/impl/ArticleDAO.java | 62 +++++ .../xncoding/pos/service/ArticleService.java | 49 ++++ .../src/main/resources/application.yml | 62 +++++ .../src/main/resources/banner.txt | 23 ++ .../src/main/resources/sql/schema.sql | 18 ++ .../com/xncoding/pos/ApplicationTests.java | 57 ++++ .../src/main/resources/application.yml | 4 +- .../src/main/resources/application.yml | 2 +- .../xncoding/service/SenderServiceTest.java | 2 +- .../src/main/resources/application.yml | 2 +- .../src/main/resources/application.yml | 4 +- .../src/main/resources/application.yml | 2 +- 26 files changed, 892 insertions(+), 15 deletions(-) create mode 100644 springboot-hibernate/.gitignore create mode 100644 springboot-hibernate/LICENSE create mode 100644 springboot-hibernate/README.md create mode 100644 springboot-hibernate/pom.xml create mode 100644 springboot-hibernate/run.sh create mode 100644 springboot-hibernate/src/main/java/com/xncoding/pos/Application.java create mode 100644 springboot-hibernate/src/main/java/com/xncoding/pos/config/DataSourceConfig.java create mode 100644 springboot-hibernate/src/main/java/com/xncoding/pos/config/properties/DruidProperties.java create mode 100644 springboot-hibernate/src/main/java/com/xncoding/pos/dao/entity/Article.java create mode 100644 springboot-hibernate/src/main/java/com/xncoding/pos/dao/repository/IArticleDAO.java create mode 100644 springboot-hibernate/src/main/java/com/xncoding/pos/dao/repository/impl/ArticleDAO.java create mode 100644 springboot-hibernate/src/main/java/com/xncoding/pos/service/ArticleService.java create mode 100644 springboot-hibernate/src/main/resources/application.yml create mode 100644 springboot-hibernate/src/main/resources/banner.txt create mode 100644 springboot-hibernate/src/main/resources/sql/schema.sql create mode 100644 springboot-hibernate/src/test/java/com/xncoding/pos/ApplicationTests.java diff --git a/README.md b/README.md index 5ab333e..4de8619 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Spring Boot 现在已经成为Java 开发领域的一颗璀璨明珠,它本身 ## 子项目列表 -每个项目一篇博客文章的详细讲解,点击链接可以查看文章 :point_right: +每个子项目会配有一篇博客文章的详细讲解 :point_right: 项目名称 | 文章地址 ----------------------------|------------------------------------------------------------------------------------------ @@ -37,7 +37,7 @@ springboot-transaction | [声明式事务](https://www.xncoding.com/2017/07 springboot-cache | [使用缓存](https://www.xncoding.com/2017/07/28/spring/sb-cache.html) springboot-redis | [Redis数据库](https://www.xncoding.com/2017/07/30/spring/sb-redis.html) springboot-batch | [批处理](https://www.xncoding.com/2017/08/01/spring/sb-batch.html) -springboot-rabbitmq | [集成消息队列RabbitMQ](https://www.xncoding.com/2017/08/06/spring/sb-rabbitmq.html) +springboot-rabbitmq | [使用消息队列RabbitMQ](https://www.xncoding.com/2017/08/06/spring/sb-rabbitmq.html) springboot-echarts | [集成Echarts导出图片](https://www.xncoding.com/2017/08/19/spring/sb-echarts.html) app-manage | [一个完整的Web后台管理系统,组合使用了多种技术] app-manage-api | [实现需要认证授权访问的RESTful API接口和WebSocket接口] @@ -75,6 +75,7 @@ app-manage-api | [实现需要认证授权访问的RESTful API接 1. 集成OAuth2认证 1. 集成QQ、微信、新浪微博第三方登录,配合shiro使用 1. 集成网络爬虫框架 +1. 等SpringBoot 2稳定后全部升级至2 ## 问题反馈 diff --git a/app-manage-api/src/main/resources/application.yml b/app-manage-api/src/main/resources/application.yml index 88256ae..d7eb05c 100644 --- a/app-manage-api/src/main/resources/application.yml +++ b/app-manage-api/src/main/resources/application.yml @@ -55,7 +55,7 @@ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: ****** + password: 123456 thymeleaf: cache: false @@ -81,7 +81,7 @@ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: ****** + password: 123456 thymeleaf: cache: false diff --git a/app-manage/src/main/resources/application.yml b/app-manage/src/main/resources/application.yml index 92c9e41..58dade4 100644 --- a/app-manage/src/main/resources/application.yml +++ b/app-manage/src/main/resources/application.yml @@ -63,7 +63,7 @@ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: ****** + password: 123456 thymeleaf: cache: false @@ -94,7 +94,7 @@ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: ****** + password: 123456 thymeleaf: cache: false diff --git a/springboot-cache/src/main/resources/application.yml b/springboot-cache/src/main/resources/application.yml index 2842f84..18a7adc 100644 --- a/springboot-cache/src/main/resources/application.yml +++ b/springboot-cache/src/main/resources/application.yml @@ -12,7 +12,7 @@ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: ****** + password: 123456 ################### mybatis-plus配置 ################### mybatis-plus: diff --git a/springboot-hibernate/.gitignore b/springboot-hibernate/.gitignore new file mode 100644 index 0000000..9d8e7cc --- /dev/null +++ b/springboot-hibernate/.gitignore @@ -0,0 +1,10 @@ +.idea/ +target/ +*.iml +*.ipr +*.iws +*.log +.svn/ +.project +rebel.xml +.rebel-remote.xml.* diff --git a/springboot-hibernate/LICENSE b/springboot-hibernate/LICENSE new file mode 100644 index 0000000..83cd47d --- /dev/null +++ b/springboot-hibernate/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2018 Xiong Neng + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/springboot-hibernate/README.md b/springboot-hibernate/README.md new file mode 100644 index 0000000..4254d60 --- /dev/null +++ b/springboot-hibernate/README.md @@ -0,0 +1,28 @@ +## 集成Hibernate + +SpringBoot集成Hibernate做DAO层 + +## 安装MySQL数据库 + +数据库的安装教程网上非常多,版本最好是mysql5.5+ + +配置数据库的账号和密码。 + +## 修改application.yml + +修改配置文件,主要是mysql的账号和密码 + +## 数据库初始化 + +创建数据库pos,然后执行SQL文件`src/main/resources/sql/schema.sql`,创建表 + +## 运行测试用例 + +执行对用户表增/删/改/查的测试用例:`com.xncoding.pos.ApplicationTests.java` + + +## 许可证 + +Copyright (c) 2018 Xiong Neng + +基于 MIT 协议发布: diff --git a/springboot-hibernate/pom.xml b/springboot-hibernate/pom.xml new file mode 100644 index 0000000..7542a32 --- /dev/null +++ b/springboot-hibernate/pom.xml @@ -0,0 +1,101 @@ + + + 4.0.0 + + com.xncoding + springboot-hibernate + 1.0.0-SNAPSHOT + jar + + springboot-hibernate + 使用Hibernate + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + 1.1.2 + 8.0.7-dmr + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + ${mysql-connector.version} + runtime + + + com.alibaba + druid + ${druid.version} + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.hamcrest + hamcrest-all + 1.3 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.20 + + true + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + + src/main/resources + + + src/main/java + + **/*.xml + + + + + + \ No newline at end of file diff --git a/springboot-hibernate/run.sh b/springboot-hibernate/run.sh new file mode 100644 index 0000000..955efb1 --- /dev/null +++ b/springboot-hibernate/run.sh @@ -0,0 +1,72 @@ +#!/bin/bash +# 项目自动更新脚本 +# 先clone相应的分支下来: +# git clone ssh://git@120.24.173.142:7999/xxx.git +# 远程调试启动: +# nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 & + +function start { + profile="$1" + echo "启动环境profile=${profile}" + jarfile=$(ls target/*.jar) + if [[ "$?" == "0" ]]; then + stop $profile $jarfile + fi + branch=$(git branch |awk '{print $2}') + git pull origin ${branch} + echo "更新完代码开始重新打包" + mvn clean && mvn clean && mvn package -DskipTests=true + if [[ "$?" != "0" ]]; then + echo "编译出错,退出!" + exit 1 + fi + echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &" + nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 & + echo "启动应用中,请查看日志文件..." +} + +function stop { + profile="$1" + jarfile="$2" + ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null + if [[ "$?" == "0" ]]; then + echo "该应用还在跑,我先停了它" + pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}') + if [[ "$pid" != "" ]]; then + kill -9 $pid + fi + echo "停止应用成功..." + fi +} + +if [[ "$1" == "start" ]]; then + if [[ "$#" < 2 ]]; then + echo "请输入正确参数:./epay.sh start {profile}" + exit 1 + fi + profile="$2" + if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then + echo "参数错误,请输入正确的profile参数,使用方法:" + echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production" + exit 1 + fi + start "${profile}" +elif [[ "$1" == "stop" ]]; then + if [[ "$#" < 2 ]]; then + echo "请输入正确参数:./epay.sh stop {profile}" + exit 1 + fi + profile="$2" + if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then + echo "参数错误,请输入正确的profile参数,使用方法:" + echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production" + exit 1 + fi + jarfile=$(ls target/*.jar) + stop $profile $jarfile +else + echo "参数错误,使用方法:{}参数是必填的,[]参数可选" + echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production" + echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production" + exit 1 +fi diff --git a/springboot-hibernate/src/main/java/com/xncoding/pos/Application.java b/springboot-hibernate/src/main/java/com/xncoding/pos/Application.java new file mode 100644 index 0000000..adc4c48 --- /dev/null +++ b/springboot-hibernate/src/main/java/com/xncoding/pos/Application.java @@ -0,0 +1,12 @@ +package com.xncoding.pos; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/springboot-hibernate/src/main/java/com/xncoding/pos/config/DataSourceConfig.java b/springboot-hibernate/src/main/java/com/xncoding/pos/config/DataSourceConfig.java new file mode 100644 index 0000000..49a364e --- /dev/null +++ b/springboot-hibernate/src/main/java/com/xncoding/pos/config/DataSourceConfig.java @@ -0,0 +1,33 @@ +package com.xncoding.pos.config; + +import com.alibaba.druid.pool.DruidDataSource; +import com.xncoding.pos.config.properties.DruidProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.annotation.Resource; + +/** + * 数据源配置 + * + * @author xiongneng + * @since 2017/5/20 21:58 + */ +@Configuration +@EnableTransactionManagement(order = 2) +public class DataSourceConfig { + + @Resource + private DruidProperties druidProperties; + + /** + * 单数据源连接池配置 + */ + @Bean + public DruidDataSource singleDatasource() { + DruidDataSource dataSource = new DruidDataSource(); + druidProperties.config(dataSource); + return dataSource; + } +} diff --git a/springboot-hibernate/src/main/java/com/xncoding/pos/config/properties/DruidProperties.java b/springboot-hibernate/src/main/java/com/xncoding/pos/config/properties/DruidProperties.java new file mode 100644 index 0000000..7cb4424 --- /dev/null +++ b/springboot-hibernate/src/main/java/com/xncoding/pos/config/properties/DruidProperties.java @@ -0,0 +1,249 @@ +package com.xncoding.pos.config.properties; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.util.JdbcConstants; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import java.sql.SQLException; + +/** + *

数据库数据源配置

+ *

说明:这个类中包含了许多默认配置,若这些配置符合您的情况,您可以不用管,若不符合,建议不要修改本类,建议直接在"application.yml"中配置即可

+ * + * @author xiongneng + * @since 2017-05-21 11:18 + */ +@Component +@ConfigurationProperties(prefix = "spring.datasource") +public class DruidProperties { + + private String url; + + private String username; + + private String password; + + private String driverClassName = "com.mysql.cj.jdbc.Driver"; + + private Integer initialSize = 10; + + private Integer minIdle = 3; + + private Integer maxActive = 60; + + private Integer maxWait = 60000; + + private Boolean removeAbandoned = true; + + private Integer removeAbandonedTimeout = 180; + + private Integer timeBetweenEvictionRunsMillis = 60000; + + private Integer minEvictableIdleTimeMillis = 300000; + + private String validationQuery = "SELECT 'x'"; + + private Boolean testWhileIdle = true; + + private Boolean testOnBorrow = false; + + private Boolean testOnReturn = false; + + private Boolean poolPreparedStatements = true; + + private Integer maxPoolPreparedStatementPerConnectionSize = 50; + + private String filters = "stat"; + + public void config(DruidDataSource dataSource) { + dataSource.setDbType(JdbcConstants.MYSQL); + dataSource.setUrl(url); + dataSource.setUsername(username); + dataSource.setPassword(password); + dataSource.setDriverClassName(driverClassName); + dataSource.setInitialSize(initialSize); // 定义初始连接数 + dataSource.setMinIdle(minIdle); // 最小空闲 + dataSource.setMaxActive(maxActive); // 定义最大连接数 + dataSource.setMaxWait(maxWait); // 获取连接等待超时的时间 + dataSource.setRemoveAbandoned(removeAbandoned); // 超过时间限制是否回收 + dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); // 超过时间限制多长 + + // 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 + dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); + // 配置一个连接在池中最小生存的时间,单位是毫秒 + dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); + // 用来检测连接是否有效的sql,要求是一个查询语句 + dataSource.setValidationQuery(validationQuery); + // 申请连接的时候检测 + dataSource.setTestWhileIdle(testWhileIdle); + // 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 + dataSource.setTestOnBorrow(testOnBorrow); + // 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 + dataSource.setTestOnReturn(testOnReturn); + // 打开PSCache,并且指定每个连接上PSCache的大小 + dataSource.setPoolPreparedStatements(poolPreparedStatements); + dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); + // 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: + // 监控统计用的filter:stat + // 日志用的filter:log4j + // 防御SQL注入的filter:wall + try { + dataSource.setFilters(filters); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getDriverClassName() { + return driverClassName; + } + + public void setDriverClassName(String driverClassName) { + this.driverClassName = driverClassName; + } + + public Integer getInitialSize() { + return initialSize; + } + + public void setInitialSize(Integer initialSize) { + this.initialSize = initialSize; + } + + public Integer getMinIdle() { + return minIdle; + } + + public void setMinIdle(Integer minIdle) { + this.minIdle = minIdle; + } + + public Integer getMaxActive() { + return maxActive; + } + + public void setMaxActive(Integer maxActive) { + this.maxActive = maxActive; + } + + public Integer getMaxWait() { + return maxWait; + } + + public void setMaxWait(Integer maxWait) { + this.maxWait = maxWait; + } + + public Integer getTimeBetweenEvictionRunsMillis() { + return timeBetweenEvictionRunsMillis; + } + + public void setTimeBetweenEvictionRunsMillis(Integer timeBetweenEvictionRunsMillis) { + this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; + } + + public Integer getMinEvictableIdleTimeMillis() { + return minEvictableIdleTimeMillis; + } + + public void setMinEvictableIdleTimeMillis(Integer minEvictableIdleTimeMillis) { + this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; + } + + public String getValidationQuery() { + return validationQuery; + } + + public void setValidationQuery(String validationQuery) { + this.validationQuery = validationQuery; + } + + public Boolean getTestWhileIdle() { + return testWhileIdle; + } + + public void setTestWhileIdle(Boolean testWhileIdle) { + this.testWhileIdle = testWhileIdle; + } + + public Boolean getTestOnBorrow() { + return testOnBorrow; + } + + public void setTestOnBorrow(Boolean testOnBorrow) { + this.testOnBorrow = testOnBorrow; + } + + public Boolean getTestOnReturn() { + return testOnReturn; + } + + public void setTestOnReturn(Boolean testOnReturn) { + this.testOnReturn = testOnReturn; + } + + public Boolean getPoolPreparedStatements() { + return poolPreparedStatements; + } + + public void setPoolPreparedStatements(Boolean poolPreparedStatements) { + this.poolPreparedStatements = poolPreparedStatements; + } + + public Integer getMaxPoolPreparedStatementPerConnectionSize() { + return maxPoolPreparedStatementPerConnectionSize; + } + + public void setMaxPoolPreparedStatementPerConnectionSize(Integer maxPoolPreparedStatementPerConnectionSize) { + this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize; + } + + public String getFilters() { + return filters; + } + + public void setFilters(String filters) { + this.filters = filters; + } + + public Boolean getRemoveAbandoned() { + return removeAbandoned; + } + + public void setRemoveAbandoned(Boolean removeAbandoned) { + this.removeAbandoned = removeAbandoned; + } + + public Integer getRemoveAbandonedTimeout() { + return removeAbandonedTimeout; + } + + public void setRemoveAbandonedTimeout(Integer removeAbandonedTimeout) { + this.removeAbandonedTimeout = removeAbandonedTimeout; + } +} diff --git a/springboot-hibernate/src/main/java/com/xncoding/pos/dao/entity/Article.java b/springboot-hibernate/src/main/java/com/xncoding/pos/dao/entity/Article.java new file mode 100644 index 0000000..992ebfd --- /dev/null +++ b/springboot-hibernate/src/main/java/com/xncoding/pos/dao/entity/Article.java @@ -0,0 +1,54 @@ +package com.xncoding.pos.dao.entity; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * Article + * + * @author XiongNeng + * @version 1.0 + * @since 2018/3/2 + */ +@Entity +@Table(name = "articles") +public class Article implements Serializable { + private static final long serialVersionUID = 1L; + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "article_id") + private int articleId; + @Column(name = "title") + private String title; + @Column(name = "category") + private String category; + + public int getArticleId() { + return articleId; + } + + public void setArticleId(int articleId) { + this.articleId = articleId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } +} diff --git a/springboot-hibernate/src/main/java/com/xncoding/pos/dao/repository/IArticleDAO.java b/springboot-hibernate/src/main/java/com/xncoding/pos/dao/repository/IArticleDAO.java new file mode 100644 index 0000000..b043c8b --- /dev/null +++ b/springboot-hibernate/src/main/java/com/xncoding/pos/dao/repository/IArticleDAO.java @@ -0,0 +1,26 @@ +package com.xncoding.pos.dao.repository; + +import com.xncoding.pos.dao.entity.Article; + +import java.util.List; + +/** + * IArticleDAO + * + * @author XiongNeng + * @version 1.0 + * @since 2018/3/2 + */ +public interface IArticleDAO { + List
getAllArticles(); + + Article getArticleById(int articleId); + + void addArticle(Article article); + + void updateArticle(Article article); + + void deleteArticle(int articleId); + + boolean articleExists(String title, String category); +} diff --git a/springboot-hibernate/src/main/java/com/xncoding/pos/dao/repository/impl/ArticleDAO.java b/springboot-hibernate/src/main/java/com/xncoding/pos/dao/repository/impl/ArticleDAO.java new file mode 100644 index 0000000..d598c92 --- /dev/null +++ b/springboot-hibernate/src/main/java/com/xncoding/pos/dao/repository/impl/ArticleDAO.java @@ -0,0 +1,62 @@ +package com.xncoding.pos.dao.repository.impl; + +import java.util.List; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import com.xncoding.pos.dao.entity.Article; +import com.xncoding.pos.dao.repository.IArticleDAO; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +/** + * ArticleDAO + * + * @author XiongNeng + * @version 1.0 + * @since 2018/3/2 + */ +@Transactional +@Repository +public class ArticleDAO implements IArticleDAO { + @PersistenceContext + private EntityManager entityManager; + + @Override + public Article getArticleById(int articleId) { + return entityManager.find(Article.class, articleId); + } + + @SuppressWarnings("unchecked") + @Override + public List
getAllArticles() { + String hql = "FROM Article as atcl ORDER BY atcl.articleId"; + return (List
) entityManager.createQuery(hql).getResultList(); + } + + @Override + public void addArticle(Article article) { + entityManager.persist(article); + } + + @Override + public void updateArticle(Article article) { + Article artcl = getArticleById(article.getArticleId()); + artcl.setTitle(article.getTitle()); + artcl.setCategory(article.getCategory()); + entityManager.flush(); + } + + @Override + public void deleteArticle(int articleId) { + entityManager.remove(getArticleById(articleId)); + } + + @Override + public boolean articleExists(String title, String category) { + String hql = "FROM Article as atcl WHERE atcl.title = ? and atcl.category = ?"; + int count = entityManager.createQuery(hql).setParameter(1, title) + .setParameter(2, category).getResultList().size(); + return count > 0; + } +} diff --git a/springboot-hibernate/src/main/java/com/xncoding/pos/service/ArticleService.java b/springboot-hibernate/src/main/java/com/xncoding/pos/service/ArticleService.java new file mode 100644 index 0000000..b592d51 --- /dev/null +++ b/springboot-hibernate/src/main/java/com/xncoding/pos/service/ArticleService.java @@ -0,0 +1,49 @@ +package com.xncoding.pos.service; + +import java.util.List; + +import com.xncoding.pos.dao.entity.Article; +import com.xncoding.pos.dao.repository.IArticleDAO; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +/** + * ArticleService + * + * @author XiongNeng + * @version 1.0 + * @since 2018/3/2 + */ +@Service +public class ArticleService { + + @Resource + private IArticleDAO articleDAO; + + public Article getArticleById(int articleId) { + Article obj = articleDAO.getArticleById(articleId); + return obj; + } + + public List
getAllArticles() { + return articleDAO.getAllArticles(); + } + + public synchronized boolean addArticle(Article article) { + if (articleDAO.articleExists(article.getTitle(), article.getCategory())) { + return false; + } else { + articleDAO.addArticle(article); + return true; + } + } + + public void updateArticle(Article article) { + articleDAO.updateArticle(article); + } + + public void deleteArticle(int articleId) { + articleDAO.deleteArticle(articleId); + } +} diff --git a/springboot-hibernate/src/main/resources/application.yml b/springboot-hibernate/src/main/resources/application.yml new file mode 100644 index 0000000..9a2d01e --- /dev/null +++ b/springboot-hibernate/src/main/resources/application.yml @@ -0,0 +1,62 @@ +########################################################## +################## 所有profile共有的配置 ################# +########################################################## + +################### spring配置 ################### +spring: + profiles: + active: dev + jpa: + properties: + hibernate: + dialect: org.hibernate.dialect.MySQLDialect + new_generator_mappings: false + format_sql: true + +logging: + level: + org.hibernate.SQL: DEBUG + org.hibernate.type.descriptor.sql.BasicBinder: TRACE + +--- + +##################################################################### +######################## 开发环境profile ########################## +##################################################################### +spring: + profiles: dev + datasource: + url: jdbc:mysql://127.0.0.1:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 + username: root + password: 123456 + thymeleaf: + cache: false + +logging: + level: + ROOT: INFO + com: + xncoding: DEBUG + file: E:/logs/app.log + +--- + +##################################################################### +######################## 测试环境profile ########################## +##################################################################### + +spring: + profiles: test + datasource: + url: jdbc:mysql://127.0.0.1:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 + username: root + password: 123456 + thymeleaf: + cache: false + +logging: + level: + ROOT: INFO + com: + xncoding: DEBUG + file: /var/logs/app.log diff --git a/springboot-hibernate/src/main/resources/banner.txt b/springboot-hibernate/src/main/resources/banner.txt new file mode 100644 index 0000000..859b78f --- /dev/null +++ b/springboot-hibernate/src/main/resources/banner.txt @@ -0,0 +1,23 @@ + + _____ _______ _____ _____ + /\ \ /::\ \ /\ \ /\ \ + /::\____\ /::::\ \ /::\____\ /::\ \ + /:::/ / /::::::\ \ /:::/ / /::::\ \ + /:::/ / /::::::::\ \ /:::/ / /::::::\ \ + /:::/ / /:::/~~\:::\ \ /:::/ / /:::/\:::\ \ + /:::/ / /:::/ \:::\ \ /:::/____/ /:::/__\:::\ \ + /:::/ / /:::/ / \:::\ \ |::| | /::::\ \:::\ \ + /:::/ / /:::/____/ \:::\____\ |::| | _____ /::::::\ \:::\ \ + /:::/ / |:::| | |:::| | |::| | /\ \ /:::/\:::\ \:::\ \ +/:::/____/ |:::|____| |:::| | |::| | /::\____\/:::/__\:::\ \:::\____\ +\:::\ \ \:::\ \ /:::/ / |::| | /:::/ /\:::\ \:::\ \::/ / + \:::\ \ \:::\ \ /:::/ / |::| | /:::/ / \:::\ \:::\ \/____/ + \:::\ \ \:::\ /:::/ / |::|____|/:::/ / \:::\ \:::\ \ + \:::\ \ \:::\__/:::/ / |:::::::::::/ / \:::\ \:::\____\ + \:::\ \ \::::::::/ / \::::::::::/____/ \:::\ \::/ / + \:::\ \ \::::::/ / ~~~~~~~~~~ \:::\ \/____/ + \:::\ \ \::::/ / \:::\ \ + \:::\____\ \::/____/ \:::\____\ + \::/ / ~~ \::/ / + \/____/ \/____/ + diff --git a/springboot-hibernate/src/main/resources/sql/schema.sql b/springboot-hibernate/src/main/resources/sql/schema.sql new file mode 100644 index 0000000..d05f38b --- /dev/null +++ b/springboot-hibernate/src/main/resources/sql/schema.sql @@ -0,0 +1,18 @@ +-- Dumping database structure for concretepage +CREATE DATABASE IF NOT EXISTS `pos` default charset utf8 COLLATE utf8_general_ci; +SET FOREIGN_KEY_CHECKS=0; +USE `pos`; +-- Dumping structure for table concretepage.articles +CREATE TABLE IF NOT EXISTS `articles` ( + `article_id` int(5) NOT NULL AUTO_INCREMENT, + `title` varchar(200) NOT NULL, + `category` varchar(100) NOT NULL, + PRIMARY KEY (`article_id`) +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='文章表'; + +-- Dumping data for table concretepage.articles: ~3 rows (approximately) +INSERT INTO `articles` (`article_id`, `title`, `category`) VALUES + (1, 'Java Concurrency', 'Java'), + (2, 'Hibernate HQL ', 'Hibernate'), + (3, 'Spring MVC with Hibernate', 'Spring'); + diff --git a/springboot-hibernate/src/test/java/com/xncoding/pos/ApplicationTests.java b/springboot-hibernate/src/test/java/com/xncoding/pos/ApplicationTests.java new file mode 100644 index 0000000..1a5408b --- /dev/null +++ b/springboot-hibernate/src/test/java/com/xncoding/pos/ApplicationTests.java @@ -0,0 +1,57 @@ +package com.xncoding.pos; + +import com.xncoding.pos.dao.entity.Article; +import com.xncoding.pos.service.ArticleService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; + +/** + * 测试 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApplicationTests { + private static final Logger log = LoggerFactory.getLogger(ApplicationTests.class); + + @Resource + private ArticleService articleService; + + /** + * 测试增删改查 + */ + @Test + public void test() { + Article article = articleService.getArticleById(1); + assertThat(article.getTitle(), is("Java Concurrency")); + + List
list = articleService.getAllArticles(); + assertThat(list, notNullValue()); + assertThat(list.size(), is(3)); + + boolean flag = articleService.addArticle(article); + assertThat(flag, is(false)); + + article.setTitle("Python Concurrency"); + articleService.updateArticle(article); + Article article1 = articleService.getArticleById(1); + assertThat(article1.getTitle(), is("Python Concurrency")); + + + articleService.deleteArticle(1); + Article article2 = articleService.getArticleById(1); + assertThat(article2, nullValue()); + } +} diff --git a/springboot-mybatis/src/main/resources/application.yml b/springboot-mybatis/src/main/resources/application.yml index ae9468f..8d633b2 100644 --- a/springboot-mybatis/src/main/resources/application.yml +++ b/springboot-mybatis/src/main/resources/application.yml @@ -36,7 +36,7 @@ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: ****** + password: 123456 thymeleaf: cache: false @@ -58,7 +58,7 @@ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: ****** + password: 123456 thymeleaf: cache: false diff --git a/springboot-rabbitmq/src/main/resources/application.yml b/springboot-rabbitmq/src/main/resources/application.yml index 6ac166a..d0c83af 100644 --- a/springboot-rabbitmq/src/main/resources/application.yml +++ b/springboot-rabbitmq/src/main/resources/application.yml @@ -15,7 +15,7 @@ spring: spring: profiles: dev rabbitmq: - host: 123.207.66.156 + host: 127.0.0.1 port: 5672 username: spring password: 123456 diff --git a/springboot-rabbitmq/src/test/java/com/xncoding/service/SenderServiceTest.java b/springboot-rabbitmq/src/test/java/com/xncoding/service/SenderServiceTest.java index 5a25ff8..554293e 100644 --- a/springboot-rabbitmq/src/test/java/com/xncoding/service/SenderServiceTest.java +++ b/springboot-rabbitmq/src/test/java/com/xncoding/service/SenderServiceTest.java @@ -13,7 +13,7 @@ import static org.junit.Assert.assertNull; /** - * UserServiceTest + * SenderServiceTest * * @author XiongNeng * @version 1.0 diff --git a/springboot-redis/src/main/resources/application.yml b/springboot-redis/src/main/resources/application.yml index 1890d9d..8238996 100644 --- a/springboot-redis/src/main/resources/application.yml +++ b/springboot-redis/src/main/resources/application.yml @@ -9,7 +9,7 @@ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: ****** + password: 123456 ################### mybatis-plus配置 ################### mybatis-plus: diff --git a/springboot-shiro/src/main/resources/application.yml b/springboot-shiro/src/main/resources/application.yml index 92c9e41..58dade4 100644 --- a/springboot-shiro/src/main/resources/application.yml +++ b/springboot-shiro/src/main/resources/application.yml @@ -63,7 +63,7 @@ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: ****** + password: 123456 thymeleaf: cache: false @@ -94,7 +94,7 @@ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/pos?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: ****** + password: 123456 thymeleaf: cache: false diff --git a/springboot-transaction/src/main/resources/application.yml b/springboot-transaction/src/main/resources/application.yml index cb1a0ee..4aaf66b 100644 --- a/springboot-transaction/src/main/resources/application.yml +++ b/springboot-transaction/src/main/resources/application.yml @@ -17,7 +17,7 @@ spring: datasource: url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: ****** + password: 123456 ################### mybatis-plus配置 ################### mybatis-plus: