admin 1 éve
szülő
commit
bb5bd953b1

+ 70 - 0
hikariCPSpringBoot/pom.xml

@@ -0,0 +1,70 @@
+<?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">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.example</groupId>
+    <artifactId>hikariCPSpringBoot</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.2.2.RELEASE</version>
+    </parent>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mybatis.spring.boot</groupId>
+            <artifactId>mybatis-spring-boot-starter</artifactId>
+            <version>1.3.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.zaxxer</groupId>
+            <artifactId>HikariCP</artifactId>
+            <version>3.3.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 41 - 0
hikariCPSpringBoot/src/main/java/org/example/Article.java

@@ -0,0 +1,41 @@
+package org.example;
+
+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;
+
+@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 long articleId;
+    @Column(name="title")
+    private String title;
+    @Column(name="category")
+    private String category;
+    public long getArticleId() {
+        return articleId;
+    }
+    public void setArticleId(long 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;
+    }
+}

+ 46 - 0
hikariCPSpringBoot/src/main/java/org/example/ArticleController.java

@@ -0,0 +1,46 @@
+package org.example;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.util.UriComponentsBuilder;
+
+@RestController
+@RequestMapping("user")
+public class ArticleController {
+    @Autowired
+    private IArticleService articleService;
+
+    //Fetches all articles
+    @GetMapping(value= "articles")
+    public ResponseEntity<List<ArticleInfo>> getAllArticles() {
+        List<ArticleInfo> responseArticleList = new ArrayList<>();
+        List<Article> articleList = articleService.getAllArticles();
+        for (int i = 0; i < articleList.size(); i++) {
+            ArticleInfo ob = new ArticleInfo();
+            BeanUtils.copyProperties(articleList.get(i), ob);
+            responseArticleList.add(ob);
+        }
+        return new ResponseEntity<List<ArticleInfo>>(responseArticleList, HttpStatus.OK);
+    }
+
+    //Creates a new article
+    @PostMapping(value= "article")
+    public ResponseEntity<Void> addArticle(@RequestBody ArticleInfo articleInfo, UriComponentsBuilder builder) {
+        Article article = new Article();
+        BeanUtils.copyProperties(articleInfo, article);
+        articleService.addArticle(article);
+        HttpHeaders headers = new HttpHeaders();
+        headers.setLocation(builder.path("/article/{id}").buildAndExpand(article.getArticleId()).toUri());
+        return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
+    }
+}

+ 31 - 0
hikariCPSpringBoot/src/main/java/org/example/ArticleInfo.java

@@ -0,0 +1,31 @@
+package org.example;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
+
+public class ArticleInfo {
+    @JsonInclude(Include.NON_NULL)
+    private long articleId;
+    @JsonInclude(Include.NON_NULL)
+    private String title;
+    @JsonInclude(Include.NON_NULL)
+    private String category;
+    public long getArticleId() {
+        return articleId;
+    }
+    public void setArticleId(long 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;
+    }
+}

+ 6 - 0
hikariCPSpringBoot/src/main/java/org/example/ArticleRepository.java

@@ -0,0 +1,6 @@
+package org.example;
+
+import org.springframework.data.repository.CrudRepository;
+
+public interface ArticleRepository extends CrudRepository<Article, Long>  {
+}

+ 24 - 0
hikariCPSpringBoot/src/main/java/org/example/ArticleService.java

@@ -0,0 +1,24 @@
+package org.example;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class ArticleService implements IArticleService {
+    @Autowired
+    private ArticleRepository articleRepository;
+
+    @Override
+    public List<Article> getAllArticles(){
+        List<Article> list = new ArrayList<>();
+        articleRepository.findAll().forEach(e -> list.add(e));
+        return list;
+    }
+    @Override
+    public void addArticle(Article article){
+        articleRepository.save(article);
+    }
+}
+

+ 8 - 0
hikariCPSpringBoot/src/main/java/org/example/IArticleService.java

@@ -0,0 +1,8 @@
+package org.example;
+
+import java.util.List;
+
+public interface IArticleService {
+    List<Article> getAllArticles();
+    void addArticle(Article article);
+}

+ 21 - 0
hikariCPSpringBoot/src/main/java/org/example/SpringBootAppStarter.java

@@ -0,0 +1,21 @@
+package org.example;
+
+import javax.sql.DataSource;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class SpringBootAppStarter implements CommandLineRunner {
+    @Autowired
+    DataSource dataSource;
+
+    public static void main(String[] args) throws Exception {
+        SpringApplication.run(SpringBootAppStarter.class, args);
+    }
+    @Override
+    public void run(String... args) throws Exception {
+        System.out.println("DataSource = " + dataSource);
+    }
+}

+ 17 - 0
hikariCPSpringBoot/src/main/resources/application.properties

@@ -0,0 +1,17 @@
+spring.datasource.url=jdbc:mysql://127.0.0.1:3306/hikariCP?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
+spring.datasource.username=root
+spring.datasource.password=5608095liyukun
+
+#Spring Boot 2.0 includes HikariDataSource by default
+#spring.datasource.type = com.zaxxer.hikari.HikariDataSource
+
+spring.datasource.hikari.connection-timeout=20000
+spring.datasource.hikari.minimum-idle=5
+spring.datasource.hikari.maximum-pool-size=12
+spring.datasource.hikari.idle-timeout=300000
+spring.datasource.hikari.max-lifetime=1200000
+spring.datasource.hikari.auto-commit=true
+
+spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
+spring.jpa.properties.hibernate.id.new_generator_mappings=false
+spring.jpa.properties.hibernate.format_sql=true

+ 42 - 0
hikariCPSpringBoot/src/test/java/clientTest.java

@@ -0,0 +1,42 @@
+
+import java.net.URI;
+
+import org.example.Article;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestTemplate;
+public class clientTest {
+    public void getAllArticlesDemo() {
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        RestTemplate restTemplate = new RestTemplate();
+        String url = "http://localhost:8080/user/articles";
+        HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
+        ResponseEntity<Article[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Article[].class);
+        Article[] articles = responseEntity.getBody();
+        for(Article article : articles) {
+            System.out.println("Id:"+article.getArticleId()+", Title:"+article.getTitle()
+                    +", Category: "+article.getCategory());
+        }
+    }
+    public void addArticleDemo() {
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        RestTemplate restTemplate = new RestTemplate();
+        String url = "http://localhost:8080/user/article";
+        Article objArticle = new Article();
+        objArticle.setTitle("Spring REST Security using Hibernate");
+        objArticle.setCategory("Spring");
+        HttpEntity<Article> requestEntity = new HttpEntity<Article>(objArticle, headers);
+        URI uri = restTemplate.postForLocation(url, requestEntity);
+        System.out.println(uri.getPath());
+    }
+    public static void main(String args[]) {
+        clientTest util = new clientTest();
+        util.addArticleDemo();
+        util.getAllArticlesDemo();
+    }
+}

+ 47 - 0
hikariCPSpringBoot/src/test/java/hikariTest.java

@@ -0,0 +1,47 @@
+import com.zaxxer.hikari.HikariConfig;
+import com.zaxxer.hikari.HikariDataSource;
+import org.junit.jupiter.api.Test;
+
+import java.sql.*;
+
+public class hikariTest {
+    @Test
+    public void hikari() throws SQLException {
+        HikariConfig config = new HikariConfig("/application.properties");
+        Connection conn = new HikariDataSource(config).getConnection();
+        PreparedStatement drop_table_data_test = conn.prepareStatement("drop table data_test");
+        Statement stmt = conn.createStatement();
+        try {
+            drop_table_data_test.execute();
+        } catch (SQLException e) {
+        }
+        conn.prepareStatement("create table data_test(test1 int)").execute();
+        for (int i = 0; i <= 10; i++) {
+            stmt.executeUpdate("insert into data_test values("+i+")");
+        }
+        ResultSet rs = conn.prepareStatement("select * from data_test;").executeQuery();
+        while (rs.next()){
+            System.out.print(rs.getObject(1)+"\t");
+        }
+        System.out.println();
+        System.out.println("------------------------");
+        stmt.executeUpdate("update data_test set test1 = 100;");
+        ResultSet rs1 = conn.prepareStatement("select * from data_test;").executeQuery();
+        while (rs1.next()){
+            System.out.print(rs1.getObject(1)+"\t");
+        }
+        System.out.println();
+        System.out.println("------------------------");
+        stmt.executeUpdate("delete from data_test");
+        ResultSet rs2 = stmt.executeQuery("select * from data_test;");
+        if (!rs2.next()){
+            System.out.println("数据删除成功");
+        }
+        stmt.close();
+        drop_table_data_test.execute();
+        drop_table_data_test.close();
+        rs.close();
+        conn.close();
+    }
+
+}