项目作者: combivators

项目描述 :
基于JPA ORM标准规范的数据服务框架
高级语言: Java
项目地址: git://github.com/combivators/tiny-jpa.git
创建时间: 2019-12-07T08:08:27Z
项目社区:https://github.com/combivators/tiny-jpa

开源协议:GNU General Public License v3.0

下载


Tiny JPA: 一个基于JPA2.2规范的微服框架

设计目的

  • 提供一些基于JDBC底层数据库属性的访问工具。
  • 提供一个泛用JPA实体访问REST服务。
  • 提供JPA实体实装时经常使用的创建日期,更新日期,排序属性的基类。
  • 提供高速加载和输出CSV数据文件的控制接口(API)。

Usage

1. How to use JDBC Database Tool (java)

  1. SchemaParser parser = new SchemaParser(connection);
  2. Schema schema = parser.parse("PUBLIC");
  3. List<Table> tables = schema.getTables();
  4. Table table = tables.get(0);
  5. List<Column> columns = table.getColumns();
  6. Column column = columns.get(0);
  7. PrimaryKey primaryKey = column.getPrimaryKey();
  8. String columnName = column.getColumnName();
  9. String type = column.getTypeName();
  10. int size = column.getColumnSize();
  11. boolean isKey = column.isPrimaryKey();

2. Access JPA entities using REST services

  • Sample configuration file : application-dao.yml
  1. handler:
  2. dao:
  3. class: net.tiny.dao.EntityService
  4. path: /dao/v1
  5. auth: ${auth.base}
  6. pattern: .*/classes/, .*/test-classes/, .*/your-entity.*[.]jar
  7. entities: your.package.entity.*
  8. level: INFO
  9. auth:
  10. base:
  11. class: net.tiny.ws.auth.SimpleAuthenticator
  12. encode: false
  13. username: user
  14. password: password
  • Access REST API
  1. GET /dao/v1/{entity}/{id}
  2. GET /dao/v1/{entity}/list?size=99
  3. GET /dao/v1/{entity}/count
  4. POST /dao/v1/{entity}
  5. PUT /dao/v1/{entity}/{id}
  6. DELETE /dao/v1/{entity}/{id}
  7. Run a command line:
  8. curl -u user:password -v http://localhost:8080//dao/v1/table_name/1234

3. Import and Export CSV data

  • Import CSV data sample (java)
  1. CsvImporter.Options options = new CsvImporter.Options("XX_LOG.csv", "xx_log")
  2. .verbose(true)
  3. .truncated(true)
  4. .skip(1);
  5. CsvImporter.load(connection, options);
  • Batch import CSV data set to tables (java)
  1. CsvImporter.load(conn, "imports/csv");
  • Prepare batch import ‘imports/csv/table-ordering.txt’ file and it’s csv files
  1. table_name_1
  2. table_name_2

4. How to implement a DAO service class

  • Step.1: Implement a entity class extends BaseEntity or OrderEntity
  1. @Entity
  2. @Table(name = "xx_log")
  3. public class Log extends BaseEntity {
  4. /** ID */
  5. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "logSequenceGenerator")
  6. @SequenceGenerator(name = "logSequenceGenerator", sequenceName = "xx_log_sequence", allocationSize=1)
  7. @Id
  8. @Column(name = "id")
  9. private Long id;
  10. //...
  11. }
  • Step.2: Append a define of entity class in ‘META-INF/persistence.xml’
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persistence
  3. xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  6. version="2.1">
  7. <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
  8. <mapping-file>META-INF/orm.xml</mapping-file>
  9. <!-- Basic Entities -->
  10. <class>net.tiny.dao.converter.LocalDateAttributeConverter</class>
  11. <class>net.tiny.dao.converter.LocalDateTimeAttributeConverter</class>
  12. <class>net.tiny.dao.entity.BaseEntity</class>
  13. <class>net.tiny.dao.entity.OrderEntity</class>
  14. <class>net.tiny.dao.entity.LockableEntity</class>
  15. <!-- Application Entities -->
  16. <class>your.package.entity.Log</class>
  17. </persistence-unit>
  18. </persistence>
  • Step.3: Implement a custom dao class (Optional)
  1. public class LogDao extends BaseDao<Log, Long> {
  2. // Append your code here
  3. }
  • Step.4: Implement a service class using dao
  1. public class LogService extends BaseService<Log> {
  2. public LogService(ServiceContext c) {
  3. super(c, Account.class);
  4. }
  5. public LogService(BaseService<?> base) {
  6. super(base, Account.class);
  7. }
  8. // Append your code here
  9. public Optional<Log> findByDate(Date date) {
  10. if (date == null) {
  11. return Optional.empty();
  12. }
  13. try {
  14. Log log = super.dao()
  15. .getNamedQuery("Log.findByDate")
  16. .setParameter("date", date)
  17. .getSingleResult();
  18. return Optional.of(log);
  19. } catch (NoResultException e) {
  20. return Optional.empty();
  21. }
  22. }
  23. }

5. JPA support configuration file sample

  1. logging:
  2. handler:
  3. output: none
  4. level:
  5. all: INFO
  6. main:
  7. - ${launcher.http}
  8. daemon: true
  9. executor: ${pool}
  10. callback: ${service.context}
  11. pool:
  12. class: net.tiny.service.PausableThreadPoolExecutor
  13. size: 5
  14. max: 10
  15. timeout: 3
  16. service:
  17. context:
  18. class: net.tiny.service.ServiceLocator
  19. # HTTP Server launcher
  20. launcher:
  21. http:
  22. class: net.tiny.ws.Launcher
  23. builder:
  24. port: 8092
  25. backlog: 10
  26. stopTimeout: 1
  27. executor: ${pool}
  28. handlers:
  29. - ${handler.sys}
  30. - ${handler.health}
  31. - ${handler.dao}
  32. handler:
  33. sys:
  34. class: net.tiny.ws.ControllableHandler
  35. path: /sys
  36. auth: ${auth.base}
  37. filters: ${filter.logger}
  38. dao:
  39. class: net.tiny.dao.EntityService
  40. path: /dao/v1
  41. auth: ${auth.base}
  42. filters: ${filter.jpa}, ${filter.logger}
  43. entities: your.entity.*
  44. pattern: .*/classes/, .*/your-entity.*[.]jar
  45. filter:
  46. logger:
  47. class: net.tiny.ws.AccessLogger
  48. out: stdout
  49. jpa:
  50. class: net.tiny.dao.HttpTransactionFilter
  51. producer: ${jpa.producer}
  52. auth:
  53. base:
  54. class: net.tiny.ws.auth.SimpleAuthenticator
  55. encode: false
  56. username: user
  57. password: password
  58. # JPA
  59. jpa:
  60. producer:
  61. class: net.tiny.dao.EntityManagerProducer
  62. properties: ${jpa.properties}
  63. # JPA2 Properties
  64. properties:
  65. javax:
  66. persistence:
  67. jdbc:
  68. driver: org.h2.Driver
  69. url: jdbc:h2:tcp://127.0.0.1:9092/h2
  70. user: sa
  71. password: sa
  72. # user: ${${vcap.alias}.cf.username}
  73. # password: ${${vcap.alias}.cf.password}
  74. show_sql: true
  75. provider: org.eclipse.persistence.jpa.PersistenceProvider
  76. validation:
  77. mode: auto
  78. lock:
  79. timeout: 1000
  80. query:
  81. timeout: 1000
  82. schema-generation:
  83. database:
  84. action: create
  85. create-source: metadata
  86. #Supported platforms : JavaDB Derby Oracle MySQL4 PostgreSQL SQLServer DB2 DB2Mainframe Sybase H2 HSQL
  87. #Others available : Informix TimesTen Attunity SQLAnyWhere DBase Cloudscape PointBase
  88. eclipselink:
  89. target-database: HSQL
  90. logging:
  91. level: INFO
  92. level:
  93. sql: FINE
  94. parameters: true
  95. jdbc:
  96. connection_pool:
  97. default:
  98. initial: 2
  99. min: 2
  100. max: 5
  101. weaving:
  102. changetracking: false

More Detail, See The Samples


Email : wuweibg@gmail.com