Przeglądaj źródła

提出dao层
修改bug

lijilei 3 lat temu
rodzic
commit
09cac5ffb9

+ 48 - 0
book-dao/pom.xml

@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>book-store</artifactId>
+        <groupId>com.bookstore</groupId>
+        <version>1.0</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>book-dao</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+            <version>2.1.5.RELEASE</version>
+        </dependency>
+
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mybatis.spring.boot</groupId>
+            <artifactId>mybatis-spring-boot-starter</artifactId>
+            <version>1.3.2</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.junit.vintage</groupId>
+                    <artifactId>junit-vintage-engine</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+    </dependencies>
+</project>

+ 50 - 0
book-dao/src/main/java/com/book/dao/config/CpsDataSourceConfig.java

@@ -0,0 +1,50 @@
+package com.book.dao.config;
+
+
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.mybatis.spring.SqlSessionFactoryBean;
+import org.mybatis.spring.SqlSessionTemplate;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.jdbc.DataSourceBuilder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+
+import javax.sql.DataSource;
+
+@Configuration
+@MapperScan(basePackages = "com.book.dao.cps.mapper", sqlSessionTemplateRef = "cpsSqlSessionTemplate")
+public class CpsDataSourceConfig {
+
+    @Bean(name = "cpsDataSource")
+    @ConfigurationProperties(prefix = "spring.datasource.cps")
+    @Primary
+    public DataSource statementDataSource() {
+        return DataSourceBuilder.create().build();
+    }
+
+    @Bean(name = "cpsSqlSessionFactory")
+    @Primary
+    public SqlSessionFactory statementSqlSessionFactory(@Qualifier("cpsDataSource") DataSource dataSource) throws Exception {
+        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
+        bean.setDataSource(dataSource);
+        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/cps/*.xml"));
+        return bean.getObject();
+    }
+
+    @Bean(name = "cpsTransactionManager")
+    @Primary
+    public DataSourceTransactionManager statementTransactionManager(@Qualifier("cpsDataSource") DataSource dataSource) {
+        return new DataSourceTransactionManager(dataSource);
+    }
+
+    @Bean(name = "cpsSqlSessionTemplate")
+    @Primary
+    public SqlSessionTemplate statementSqlSessionTemplate(@Qualifier("cpsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
+        return new SqlSessionTemplate(sqlSessionFactory);
+    }
+}

+ 49 - 0
book-dao/src/main/java/com/book/dao/config/PolarDataSourceConfig.java

@@ -0,0 +1,49 @@
+package com.book.dao.config;
+
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.mybatis.spring.SqlSessionFactoryBean;
+import org.mybatis.spring.SqlSessionTemplate;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.jdbc.DataSourceBuilder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+
+import javax.sql.DataSource;
+
+@Configuration
+@MapperScan(basePackages = "com.book.dao.palordb.mapper", sqlSessionTemplateRef  = "mainSqlSessionTemplate")
+public class PolarDataSourceConfig {
+
+    @Bean(name = "polarDataSource")
+    @ConfigurationProperties(prefix = "spring.datasource.polar")
+    @Primary
+    public DataSource mainDataSource() {
+        return DataSourceBuilder.create().build();
+    }
+
+    @Bean(name = "polarSqlSessionFactory")
+    @Primary
+    public SqlSessionFactory mainSqlSessionFactory(@Qualifier("polarDataSource") DataSource dataSource) throws Exception {
+        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
+        bean.setDataSource(dataSource);
+         bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/polar/*.xml"));
+       return bean.getObject();
+    }
+
+    @Bean(name = "mainTransactionManager")
+    @Primary
+    public DataSourceTransactionManager mainTransactionManager(@Qualifier("polarDataSource") DataSource dataSource) {
+        return new DataSourceTransactionManager(dataSource);
+    }
+
+    @Bean(name = "mainSqlSessionTemplate")
+    @Primary
+    public SqlSessionTemplate mainSqlSessionTemplate(@Qualifier("polarSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
+        return new SqlSessionTemplate(sqlSessionFactory);
+    }
+}

+ 73 - 0
book-dao/src/main/resources/application-dev.yml

@@ -0,0 +1,73 @@
+spring:
+  datasource:
+    cps:
+      type: com.alibaba.druid.pool.DruidDataSource
+      driver-class-name: com.mysql.cj.jdbc.Driver
+      url: jdbc:mysql://121.41.100.198:3306/test_cps?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
+      username: root
+      password: root
+      #下面为连接池补充设置
+      druid:
+        initial-size: 5 # 初始化
+        max-active: 5 # 最大
+        min-idle: 5 # 最小
+        max-wait: 6000 # 超时时间
+        time-between-eviction-runs-millis: 60000 # 每分钟检查一次空闲链接
+        min-evictable-idle-time-millis: 300000 # 空闲链接可以保持多久而不被驱逐
+        # 检测链接是否有效的query
+        validation-query: SELECT 1 FROM DUAL
+        test-while-idle: true # 检测到链接空闲时,验证是否有效
+        test-on-borrow: false # 申请链接时,不检测
+        test-on-return: false # 返回链接时,不检测
+        pool-prepared-statements: false # 是否缓存preparedStatement,oracle打开,mysql关闭
+        # 如果上面开启了游标,这里要设置一下大小,例如 50
+        max-pool-prepared-statement-per-connection-size: -1
+        # 统计、监控配置
+        filters: stat,wall # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
+        # 合并执行的相同sql,避免因为参数不同而统计多条sql语句;开启慢sql记录
+        connect-properties: config.stat.mergeSql=true;config.stat.slowSqlMillis=500
+        use-global-data-source-stat: true # 合并多个DruidDataSource的监控数据
+        stat-view-servlet:
+          enabled: true
+          login-username: tianyun
+          login-password: tianyunperfect
+          allow: # 默认运行所有
+          deny: # 默认即可
+          reset-enable: true
+    polar:
+      type: com.alibaba.druid.pool.DruidDataSource
+      driver-class-name: com.mysql.cj.jdbc.Driver
+      url: jdbc:mysql://121.41.100.198:3306/test_cps?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
+      username: root
+      password: root
+      #下面为连接池补充设置
+      druid:
+        initial-size: 5 # 初始化
+        max-active: 5 # 最大
+        min-idle: 5 # 最小
+        max-wait: 6000 # 超时时间
+        time-between-eviction-runs-millis: 60000 # 每分钟检查一次空闲链接
+        min-evictable-idle-time-millis: 300000 # 空闲链接可以保持多久而不被驱逐
+        # 检测链接是否有效的query
+        validation-query: SELECT 1 FROM DUAL
+        test-while-idle: true # 检测到链接空闲时,验证是否有效
+        test-on-borrow: false # 申请链接时,不检测
+        test-on-return: false # 返回链接时,不检测
+        pool-prepared-statements: false # 是否缓存preparedStatement,oracle打开,mysql关闭
+        # 如果上面开启了游标,这里要设置一下大小,例如 50
+        max-pool-prepared-statement-per-connection-size: -1
+        # 统计、监控配置
+        filters: stat,wall # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
+        # 合并执行的相同sql,避免因为参数不同而统计多条sql语句;开启慢sql记录
+        connect-properties: config.stat.mergeSql=true;config.stat.slowSqlMillis=500
+        use-global-data-source-stat: true # 合并多个DruidDataSource的监控数据
+        stat-view-servlet:
+          enabled: true
+          login-username: tianyun
+          login-password: tianyunperfect
+          allow: # 默认运行所有
+          deny: # 默认即可
+          reset-enable: true
+
+
+

+ 52 - 0
book-dao/src/main/resources/application-test.yml

@@ -0,0 +1,52 @@
+server:
+  port: 9999
+
+spring:
+  datasource:
+    type: com.alibaba.druid.pool.DruidDataSource
+    driver-class-name: com.mysql.cj.jdbc.Driver
+    url: jdbc:mysql://www.tianyunperfect.cn:3306/test_cps?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
+    username: test_cps
+    password: xKysDECnJLXkMYdJ
+    #下面为连接池补充设置
+    druid:
+      initial-size: 5 # 初始化
+      max-active: 5 # 最大
+      min-idle: 5 # 最小
+      max-wait: 6000 # 超时时间
+      time-between-eviction-runs-millis: 60000 # 每分钟检查一次空闲链接
+      min-evictable-idle-time-millis: 300000 # 空闲链接可以保持多久而不被驱逐
+      # 检测链接是否有效的query
+      validation-query: SELECT 1 FROM DUAL
+      test-while-idle: true # 检测到链接空闲时,验证是否有效
+      test-on-borrow: false # 申请链接时,不检测
+      test-on-return: false # 返回链接时,不检测
+      pool-prepared-statements: false # 是否缓存preparedStatement,oracle打开,mysql关闭
+      # 如果上面开启了游标,这里要设置一下大小,例如 50
+      max-pool-prepared-statement-per-connection-size: -1
+      # 统计、监控配置
+      filters: stat,wall # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
+      # 合并执行的相同sql,避免因为参数不同而统计多条sql语句;开启慢sql记录
+      connect-properties: config.stat.mergeSql=true;config.stat.slowSqlMillis=500
+      use-global-data-source-stat: true # 合并多个DruidDataSource的监控数据
+      stat-view-servlet:
+        enabled: true
+        login-username: tianyun
+        login-password: tianyunperfect
+        allow: # 默认运行所有
+        deny: # 默认即可
+        reset-enable: true
+
+
+mybatis:
+  type-aliases-package: com.book.server.entity
+  mapper-locations: classpath:mapper/*Mapper.xml
+
+
+# 设置debug模式下打印mysql
+logging:
+  level:
+    com:
+      book:
+        server:
+          mapper: debug

+ 4 - 0
book-dao/src/main/resources/application.yml

@@ -0,0 +1,4 @@
+spring:
+  # 环境 dev:开发环境|test:测试环境|prod:生产环境
+  profiles:
+    active: dev #激活的配置文件

+ 1 - 1
book-server/src/main/java/com/book/server/model/VO/BookContent.java

@@ -12,9 +12,9 @@ import lombok.Data;
 public class BookContent {
 
  private String bookId;
- private String contentId;
  private String chapterId;
  private String content;
+ private String name;
  private String isPay;
 
 

+ 14 - 13
book-server/src/main/java/com/book/server/service/impl/BookServiceImpl.java

@@ -209,7 +209,7 @@ public class BookServiceImpl implements BookService {
     @Override
     public List<Chapter> getChaptersByBookId(String bookId) {
         List<Chapter> list = new ArrayList<>();
-        try {
+
             if (StringUtils.isEmpty(bookId)) {
                 throw new IllegalArgumentException("bookId cannot be null or empty");
             }
@@ -221,8 +221,14 @@ public class BookServiceImpl implements BookService {
             JsonParser parser = new JsonParser();
             JsonArray jsonArray = parser.parse(result).getAsJsonArray();
             Book book = bookMapper.selectByPrimaryKey(Long.parseLong(bookId));
+            if (book == null) {
+                return list;
+            }
             Integer freeChapterNum = book.getFreeChapterNum();
 
+            if (freeChapterNum==null){
+                freeChapterNum = 20;
+            }
             for (int i = 0; i < jsonArray.size(); i++) {
                 JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
                 Chapter chapter = new Chapter();
@@ -235,17 +241,11 @@ public class BookServiceImpl implements BookService {
                 chapter.setType(jsonObject.get("type").getAsString());
                 chapter.setVolumeId(jsonObject.get("volume_id").getAsString());
                 chapter.setWords(jsonObject.get("words").getAsString());
-                list.add(chapter);
-                if (i+1 == freeChapterNum){
+                if (i + 1 == freeChapterNum) {
                     chapter.setReadAble("0");
                 }
+                list.add(chapter);
             }
-        } catch (Exception e) {
-            logger.error(e.getMessage());
-            e.printStackTrace();
-
-            return list;
-        }
 
         return list;
     }
@@ -273,12 +273,12 @@ public class BookServiceImpl implements BookService {
     }
 
     @Override
-    public BookContent getContentByContentId(String bookId,String contentId) {
+    public BookContent getContentByContentId(String bookId, String contentId) {
         String contentUrl = "http://new.emeixs.com/index.php/Home/Interface/content/bid/%s/cid/%s";
-        if (StringUtils.isEmpty(bookId) || StringUtils.isEmpty(contentId)){
+        if (StringUtils.isEmpty(bookId) || StringUtils.isEmpty(contentId)) {
             return null;
         }
-        BookContent content =null;
+        BookContent content = null;
         try {
             content = new BookContent();
             String url = String.format(contentUrl, bookId, contentId);
@@ -287,9 +287,10 @@ public class BookServiceImpl implements BookService {
 
             content.setContent(jsonObject.get("content").getAsString());
             content.setBookId(jsonObject.get("book_id").getAsString());
+            content.setName(jsonObject.get("name").getAsString());
             content.setChapterId(jsonObject.get("CONTENT_ID").getAsString());
             content.setIsPay(jsonObject.get("ispay").getAsString());
-        }catch (Exception e){
+        } catch (Exception e) {
             logger.info(e.getMessage());
             return null;
         }

+ 1 - 5
pom.xml

@@ -42,11 +42,7 @@
     <dependencyManagement>
         <dependencies>
 
-            <dependency>
-                <groupId>com.alvin.springboot</groupId>
-                <artifactId>springboot-main</artifactId>
-                <version>${project.version}</version>
-            </dependency>
+
 
             <dependency>
                 <groupId>org.springframework.cloud</groupId>