▲前提条件
開発環境(eclipse)があること
DBが用意してあること
▲DataNucleus Access Platformのダウンロード
ここからdatanucleus-accessplatformをダウンロードしてくる
▲EclipseにDataNucleus Pluginをインストール
1.Help>Install New Software>Addボタンをクリック
2.以下を入力
name:何でもよい
Location:「http://www.datanucleus.org/downloads/eclipse-update」
3.OKをクリック
4.チェックしてNextボタンをクリックしてインストールする
5.指示通りすすめればよい
6.インストールが完了したらEclipseを再起動する
▲プロジェクトの作成と実行
1.何でもよいのでJavaプロジェクトを適当に作成する
2.ダウンロードしたdatanucleus-accessplatformを展開しておく
3.libフォルダを作成し、jarファイルを2で展開したlibとdepsからコピーする
4.3のjarファイルをビルドパスに追加する
5.プロジェクトの上でマウスを右クリックして表示されるメニューの「DataNucleus - Add DataNucleus Support」を選択
6.エンティティクラスを作成
@PersistenceCapable
public class Hoge {
private int id;
private String name;
public Hoge() {
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
7.jdoconfig.propertiesファイルを作成
- mysqlで書いてますが、お好きなDBの設定に変更して下さい
javax.jdo.PersistenceManagerFactoryClass=org.datanucleus.jdo.JDOPersistenceManagerFactory
javax.jdo.option.ConnectionDriverName=com.mysql.jdbc.Driver
javax.jdo.option.ConnectionURL=jdbc:mysql://localhost:3306/hoge
javax.jdo.option.ConnectionUserName=root
javax.jdo.option.ConnectionPassword=
8.メインクラスを作成(ここではmainメソッドだけ表示)
public static void main(String[] args) {
PersistenceManagerFactory pmf =
JDOHelper.getPersistenceManagerFactory("jdoconfig.properties");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction t = pm.currentTransaction();
t.begin();
Hoge test = new Hoge();
test.setId(100);
test.setName("hoge");
pm.makePersistent(test);
t.commit();
}
9.プロジェクトの上でマウスを右クリックして表示されるメニューの「DataNucleus - Enable Auto-Enhancement」を選択
10.プロジェクトの上でマウスを右クリックして表示されるメニューの「DataNucleus - Run Schema Tool」を選択
以下のものを設定すればOK
- Driver Path
- Driver Name
- Connection URL
- User Name
- Passsword
11.プロジェクトの上でマウスを右クリックして表示されるメニューの「Run - java」を実行
これで実行、出来てるはず。。。
確認はDBに対してSQL叩けば確認できます
0 件のコメント:
コメントを投稿