当前位置: 首页 > 工具软件 > ActiveJDBC > 使用案例 >

activejdbc

史超英
2023-12-01

activejdbc - Implementation of Active Record pattern in Java. Inspired by Ruby on Rails ActiveRecord - Google Project Hosting

Introduction

ActiveJDBC is a Java implementation of Active Record design pattern. It was inspired by ActiveRecord ORM from Ruby on Rails.

For more information on features of ActiveJDBC, see the Features page.

 

 

Features

Please, follow this link: Features

News

January 25 2012 - Support for Microsoft SQL Server was added. This brings the number of databases supported by ActiveJDBC to 5: MySQL, PostgreSQL, Oracle, H2, MS SQLServer. Kudos to John Richardson for this contribution. Support of MS SQLServer includes both Microsoft and TDS drivers.

September 18 2011 - A new caching provider has been integrated into the project: EHCache. This was done because it seems that OSCache, however good of a project, is retired by OpenSymphony.

August 10 2011 - Dear community, we are preparing for a first formal release of ActiveJDBC and publishing it to the Maven Central. In preparations, we upgraded the version from 1.1-SNAPSHOT to 1.2-SNAPSHOT and also moved all classes from package: activejdbc to package: org.javalite.activejdbc

We also moved group artifact from: activejdbc to: org.javalite.activejdbc

All these superficial changes are required by Sonatype to publish to the Maven Central. We apologize for the temporary inconvenience, but this change is something we did not anticipate. However, with ActiveWeb following the suit, it too will be released under org.javalite group ID and package.

The old repository you are using for 1.1-SNAPSHOT: http://ipsolutionsdev.com/snapshots/ will stay untouched indefinitely, so you can upgrade to 1.2-SNAPSHOT at any time in the future, if you can wait till a fixed release 1.2 will be published to Maven Central (~ 2 weeks)

The new repository we are publishing snapshots is hosted by Sonatype: https://oss.sonatype.org/content/repositories/snapshots/org/javalite/

July 20 2011 Made JavaDoc accessible: http://ipsolutionsdev.com/activejdbc/

May 22 2011 Phil Suh added support for H2 Database

February 23 2011 Added a long awaited generation of JSON into classes Model and LazyList, for more information navigate to GenerationOfJson

January 12 2011 ActiveJDBC now has ability to load metadata on demand - when a database is first accessed. This means that in cases when a system has more than one database, their metadata is not loaded up all at once. This gives more flexibility because you do not need to have all connections available in places where you only need one.

November 9 2010 ActiveJDBC now has ability to generate stock XML from models and lists of models. Follow this link for more information: GenerationOfXml

September 28 2010 ActiveJDBC validations framework has been extended to take in dynamic parameters, and reflect internationalized messages. For more information, take a look at Validations

September 7 2010 All artifacts from this project are published to a new snapshot repository: http://ipsolutionsdev.com/snapshots/.

August 18 2010: Well, it did finally happen: yesterday I ran a presentation on ActibeJDBC at Thoughtworks. Attendance was at healthy 25 people, and all were engaged and asked a lot of questions. To those attended: thank you for interest in the framework, your questions and suggestions! This proves once again that there is room for a new Java ORM system. If you'd like to get the presentation slides, follow this link: https://activejdbc.googlecode.com/svn/trunk/doc/activejdbc-cjug.pdf.

happy coding!

August 11 2010: Chicago Java User Group accepted my proposal for a presentation on ActiveJDBC. Thoughtworks will be hosting it on August 17th.

July 19 2010: ActiveJDBC sources are published on Google Code under Apache 2.0 License.

Design principles

  • Should infer metadata from DB (like ActiveRecord)
  • Should be very easy to work with
  • Should reduce amount of code to a minimum
  • No configuration, just conventions
  • Some conventions are overridable in code
  • No need to learn another language
  • No need to learn another QL - SQL is sufficient
  • Code must be lightweight and intuitive, should read like English
  • No sessions, no "attaching, re-attaching"
  • No persistence managers.
  • No classes outside your own models.
  • Models are lightweight, no transient fields
  • No proxying. What you write is what you get (WYWIWYG :))
  • Should have the least possible resistance to startup a project
  • No useless getters and setters (they just pollute code). You can still write them if you like.
  • No DAOs and DTOs - this is mostly junk code anyway

Programming model

For a simple example we will use a table called PEOPLE created with this MySQL DDL:

CREATE TABLE people (
  id  int(11) NOT NULL auto_increment PRIMARY KEY,
  name VARCHAR(56) NOT NULL,
  last_name VARCHAR(56),
  dob DATE,
  graduation_date DATE,
  created_at DATETIME,
  updated_at DATETIME
  );

ActiveJDBC infers DB schema parameters from a database. This means you do not have to provide it in code, the simplest example model looks like this:

public class Person extends Model {}

Despite the fact that there is no code in it, it is fully functional and will map to a table called PEOPLE automatically.

Here is how to use it:

How to query

Querying is easy:

List<Person> people = Person.where("name = 'John'");
Person aJohn =  people.get(0);
String johnsLastName = aJohn.get("last_name");

As you can see, the amount of code is reduced to a level when it is actually readable. Finder methods can also be parametrized like this:

List<Person> people = Person.where("name = ?", "John");
Person aJohn =  people.get(0);
String johnsLastName = aJohn.get("last_name");

Paging through data

List<Employee> people = Employee.where("department = ? and hire_date > ? ", "IT", hireDate)
         .offset(21)
         .limit(10)
         .orderBy("hire_date asc");

This query will ensure that the returned result set will start at the 21st record and will return only 10 records, according to the "orderBy". The ActiveJDBC has a built in facility for various database flavors and it will generate appropriate SQL statement that is specific for a DB (Oracle, MySQL, etc) and is efficient. It will not fetch all records, starting with 1.

Creating new records

There are several ways to do this, and the simplest is:

Person p = new Person();
p.set("name", "Marilyn");
p.set("last_name", "Monroe");
p.set("dob", "1935-12-06");
p.saveIt();

There is also a shorthand version of doing the same:

new Person().set("name", "Marilyn").set("last_name", "Monroe").set("dob", "1935-12-06").saveIt();

and yet another one :

Person.createIt("name", "Marilyn", "last_name", "Monroe", "dob", "1935-12-06");

Updating a single record

Employee e = Employee.findFirst("first_name = ?", "John");
e.set("last_name", "Steinbeck").saveIt();

deleting a record

Employee e = Employee.findFirst("first_name = ?", "John");
e.delete();

For more detailed information, look at the Features page

How to download

If you are using Maven, add these repositories to your pom:

<repositories>
    <repository>
        <id>sonatype-nexus-snapshots</id>
        <name>Sonatype Nexus Plugin Snapshots</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>sonatype-nexus-plugin-snapshots</id>
        <name>Sonatype Nexus Plugin Snapshots</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

If you are not using Maven, you can pull down the latest from here: https://oss.sonatype.org/content/repositories/snapshots/org/javalite/

Current version is: 1.2-SNAPSHOT.

How to build

Check the project out from SVN:

svn checkout http://activejdbc.googlecode.com/svn/trunk/ activejdbc

Navigate to the project directory:

cd activejdbc

Execute this command at the root of the project (provided you have Maven installed):

 mvn clean install -Dmaven.test.skip=true

Note: you need to disable tests because the build would fail if you do not have databases configured for testing.

What ActiveJDBC is not

  • ActiveJDBC is not a layer on top of Hibernate
  • ActiveJDBC is not a JPA implementation, it has it's own annotations and all are optional; the only standard it adheres to is JDBC

Getting Started

Please, follow this link: Getting Started

Supported databases

Currently ActiveJDBC supports MySQL, PostgreSQL, Oracle, H2 and MS SQLServer

Support

Please, post your questions here: http://groups.google.com/group/activejdbc-group

Frequently Asked Questions

FAQ

Want to contribute?

Please consider these steps:

  • Pull down code
  • Configure local databases MySQL, PosgreSQl, Oracle XE
  • Run four builds (see scripts in root dir build_xxx.sh)
  • Study tests, code, ask as many questions as you like

 

Once you are comfortable with code, please request a branch created for your feature. You will be made a contributor on the project, and will commit your code to a branch. We will examine code and it all checks, we will merge the code to trunk, and make it official.

Please consider these requirements:

  1. You must provide decent JavaDoc for new classes and methods
  2. Entire code base must successfully run through 4 different builds, one for each database: MySQL, Oracle, PostgreSQL, H2. This means that you need to have these databases installed locally and configured appropriately (except h2, it is tested in memory).
  3. Your new code must be covered by good quality tests. For examples, please see many existing tests in the project. Using JSpec is not mandatory, but recommended, because it is used across the project.

We welcome your help, contributions and suggestions.

posted on 2012-02-20 23:56  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/02/20/2360724.html

 类似资料:

相关阅读

相关文章

相关问答