花了几天时间来搞Kodo JDO,网上资料不是很全,抽时间整理了一下。
可以到网上下载工具,下载后会发一个LicenseKey到你的邮箱(www.solarmetric.com)。
下载后就可以开始了:
先新建一个类:
有人说持久类必须有一个私有的构造函数,我试了一下,不然;都可以。
Student.java
package
com;
import
java.io.Serializable;
public
class
Student
implements
Serializable
{
private
String name;
private
int
age;
private
Student()
{
}
public
Student(String name,
int
age)
{
this
.name
=
name;
this
.age
=
age;
}
/** */
/**
*
@return
the age
*/
public
int
getAge()
{
return
age;
}
/** */
/**
*
@param
age the age to set
*/
public
void
setAge(
int
age)
{
this
.age
=
age;
}
/** */
/**
*
@return
the name
*/
public
String getName()
{
return
name;
}
/** */
/**
*
@param
name the name to set
*/
public
void
setName(String name)
{
this
.name
=
name;
}
}
然后通过工具Kodo Mapping Workbench产生package.jdo和package.mapping文件。
为了避免每次都实现javax.jdo.spi.PersistenceCapable类,Kodo提供了Enhance(增强)类的class文件的工具,
类增强之后class文件会变大。工具的具体操作自己看一下文档吧,不具体说了。
也可以用bin目录下的bat文件进行编译,麻烦一点。
package.jdo
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<
jdo
>
<
package
name
=
"
com
"
>
<
class
name
=
"
Student
"
/>
</
package
>
</
jdo
>
package.mapping
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<
mapping
>
<
package
name
=
"
com
"
>
<
class
name
=
"
Student
"
>
<
jdbc
-
class
-
map type
=
"
base
"
pk
-
column
=
"
JDOID
"
table
=
"
STUDENT
"
/>
<
field name
=
"
age
"
>
<
jdbc
-
field
-
map type
=
"
value
"
column
=
"
AGE
"
/>
</
field
>
<
field name
=
"
name
"
>
<
jdbc
-
field
-
map type
=
"
value
"
column
=
"
NAME
"
/>
</
field
>
</
class
>
</
package
>
</
mapping
>
和类文件在同一个目录。在src目录下还有一个配置文件,jar文件就不啰嗦了:
kodo.properties
kodo.LicenseKey: 2C61
-
EF63
-
F131
-
32B2
-
3200
kodo.FetchGroups:
kodo.WriteLockLevel: write
kodo.RestoreMutableValues:
false
kodo.CopyObjectIds:
false
kodo.PersistentClasses: com.Student
kodo.RetryClassRegistration:
false
javax.jdo.PersistenceManagerFactoryClass: kodo.jdbc.runtime.JDBCPersistenceManagerFactory
javax.jdo.option.NontransactionalWrite:
false
javax.jdo.option.Multithreaded:
false
javax.jdo.option.ConnectionUserName: root
javax.jdo.option.ConnectionPassword: admin
javax.jdo.option.ConnectionURL: jdbc\:mysql\:
//
localhost:3306/bikestore
javax.jdo.option.ConnectionDriverName: com.mysql.jdbc.Driver
kodo.jdbc.Schemas:
kodo.jdbc.ForeignKeyConstraints:
false
还要注意一下版本,我使用的是kodo-jdo-3.4.1的,因为增强类的编译会不一致。
映射这一块搞定了。
下面开始写一个测试类:
活学活用,上篇以介绍,引入log4j.properties。
StudentTest.java
package
com.test;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.List;
import
java.util.Properties;
import
javax.jdo.JDOHelper;
import
javax.jdo.PersistenceManager;
import
javax.jdo.PersistenceManagerFactory;
import
javax.jdo.Query;
import
javax.jdo.Transaction;
import
org.apache.log4j.Logger;
import
com.Student;
import
junit.framework.TestCase;
public
class
StudentTest
extends
TestCase
{
private
static
final
Logger log
=
Logger.getLogger(StudentTest.
class
);
private
PersistenceManagerFactory factory;
private
PersistenceManager manager;
/** */
/**
* 初始化持久化工厂类
*/
public
void
setUp()
{
try
{
InputStream in
=
new
FileInputStream(
"
src/kodo.properties
"
);
//
factory = JDOHelper.getPersistenceManagerFactory(in);
//
或者
Properties props
=
new
Properties();
props.load(in);
in.close();
factory
=
JDOHelper.getPersistenceManagerFactory(props);
manager
=
factory.getPersistenceManager();
log.info(factory
+
"
"
+
manager);
}
catch
(FileNotFoundException e)
{
e.printStackTrace();
}
catch
(IOException e)
{
e.printStackTrace();
}
}
/** */
/**
* 释放资源
*/
public
void
tearDown()
{
manager
=
null
;
factory
=
null
;
}
public
void
testSaveUser()
{
Student student
=
new
Student(
"
tang
"
,
23
);
try
{
Transaction tran
=
manager.currentTransaction();
tran.begin();
manager.makePersistent(student);
tran.commit();
log.info(
"
保存成功
"
);
}
catch
(Exception e)
{
e.printStackTrace();
log.error(
"
保存异常
"
, e);
}
}
public
void
testGetStudent()
{
try
{
//
主键ID不能作为条件,否则会报异常
Query query
=
(Query)manager.newQuery(Student.
class
,
"
name == 'ddd'
"
);
List list
=
(List)query.execute();
Student stu
=
(Student)list.get(
0
);
log.info(
""
+
stu.getAge());
log.info(
"
获取成功
"
);
}
catch
(Exception e)
{
e.printStackTrace();
log.error(
"
获取异常
"
, e);
}
}
public
void
testGetStudents()
{
try
{
Query query
=
(Query)manager.newQuery(Student.
class
);
List list
=
(List)query.execute();
log.info(
""
+
list.size());
log.info(
"
获取所有成功
"
);
}
catch
(Exception e)
{
e.printStackTrace();
log.error(
"
获取所有异常
"
, e);
}
}
public
void
testDeleteStudents()
{
try
{
//
主键ID不能作为条件,否则会报异常,可以批量删除
Query query
=
(Query)manager.newQuery(Student.
class
,
"
name == 'tang'
"
);
List list
=
(List)query.execute();
log.info(
""
+
list.size());
Transaction tran
=
manager.currentTransaction();
tran.begin();
manager.deletePersistentAll(list);
tran.commit();
log.info(
"
删除一批成功
"
);
}
catch
(Exception e)
{
e.printStackTrace();
log.error(
"
删除一批异常
"
, e);
}
}
public
void
testUpdateStudent()
{
try
{
Transaction tran
=
manager.currentTransaction();
tran.begin();
Query query
=
(Query)manager.newQuery(Student.
class
);
List list
=
(List)query.execute();
Student Student
=
(Student)list.get(
0
);
Student.setName(
"
ddddddd
"
);
Student.setAge(
26
);
//
javax.jdo.option.NontransactionalWrite: true
//
设置为true,不会报异常,但是不会更新数据库
//
事务从这里开始不行,会报在事务外的异常
//
tran.begin();
manager.makePersistent(Student);
tran.commit();
log.info(
""
+
list.size());
log.info(
"
更新成功
"
);
}
catch
(Exception e)
{
e.printStackTrace();
log.error(
"
更新异常
"
, e);
}
}
}
数据库脚本:
create database bikestore
use bikestore
create table Student (jdoid int primary key,
name varchar(20), age int); 测试的是用Mysql5.0。
完成了,出现了几个问题,manager.getObjectById("jdoid", false);出错,还有不能根据主键更新,
有谁知道,请告诉我,不胜感激!