dome
使用hsql 数据库;
一对多,多对一;
可以直接运行的 dome: http://www.blogjava.net/Files/Good-Game/iba.rar
本例指涉及到查询,为了简单没有事务,和service层
先看测试运行:
运行结果:
DaoConfig工具类:
dao:
public class LevelDao {
public static List < Level > getAll(){
try {
return DaoConfig.getSqlMap().queryForList( " getAllLevel " );
} catch (Exception e) {
e.printStackTrace();
}
return null ;
}
}
持久类说明
level.xml
map confg
使用hsql 数据库;
一对多,多对一;
可以直接运行的 dome: http://www.blogjava.net/Files/Good-Game/iba.rar
本例指涉及到查询,为了简单没有事务,和service层
先看测试运行:
@Test
public void testLevel() {
try {
Level level = (Level)DaoConfig.getSqlMap().queryForObject( " level.getLevelId " , new Integer( 2 ) );
System.out.println( " f_level= " + level.getFlevel().getName());
System.out.println( "-> level= " + level.getName());
for (Level ltmp : level.getClevel()){
System.out.println( " c_root= " + ltmp.getName());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void testLevel() {
try {
Level level = (Level)DaoConfig.getSqlMap().queryForObject( " level.getLevelId " , new Integer( 2 ) );
System.out.println( " f_level= " + level.getFlevel().getName());
System.out.println( "-> level= " + level.getName());
for (Level ltmp : level.getClevel()){
System.out.println( " c_root= " + ltmp.getName());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
运行结果:
hsql 数据库 初始化
create table level ( id int not null , name varchar ( 80 ) null , fid int , constraint pk_supplier primary key (id), constraint fk_item_1 foreign key (fid) references level (id) )
create index levelName on level (name)
insert into level (id,name,fid) values ( 1 , ' root ' , null )
insert into level (id,name,fid) values ( 2 , ' level_1 ' , 1 )
insert into level (id,name,fid) values ( 3 , ' root_2 ' , 1 )
insert into level (id,name,fid) values ( 4 , ' root_1_1 ' , 2 )
insert into level (id,name,fid) values ( 5 , ' root_1_2 ' , 2 )
insert into level (id,name,fid) values ( 6 , ' root_1_1_1 ' , 4 )
运行结果
f_level = root
-> level = level_1
c_level = root_1_1
c_level = root_1_2
create table level ( id int not null , name varchar ( 80 ) null , fid int , constraint pk_supplier primary key (id), constraint fk_item_1 foreign key (fid) references level (id) )
create index levelName on level (name)
insert into level (id,name,fid) values ( 1 , ' root ' , null )
insert into level (id,name,fid) values ( 2 , ' level_1 ' , 1 )
insert into level (id,name,fid) values ( 3 , ' root_2 ' , 1 )
insert into level (id,name,fid) values ( 4 , ' root_1_1 ' , 2 )
insert into level (id,name,fid) values ( 5 , ' root_1_2 ' , 2 )
insert into level (id,name,fid) values ( 6 , ' root_1_1_1 ' , 4 )
运行结果
f_level = root
-> level = level_1
c_level = root_1_1
c_level = root_1_2
DaoConfig工具类:
package
test.persistence;
import com.ibatis.common.jdbc.ScriptRunner;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
/**
* damo api
* http://portals.apache.org/bridges/multiproject/jpetstore/apidocs/overview-summary.html
* @author Administrator
*
*/
public class DaoConfig {
private static SqlMapClient sqlMap = null ;
static {
try {
Properties props = Resources.getResourceAsProperties( " properties/database.properties " );
String url = props.getProperty( " url " );
String driver = props.getProperty( " driver " );
String username = props.getProperty( " username " );
String password = props.getProperty( " password " );
if (url.equals( " jdbc:hsqldb:mem:jpetstore " )) {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url, username, password);
try {
ScriptRunner runner = new ScriptRunner(conn, false , false );
runner.setErrorLogWriter( new PrintWriter( System.out ) );
runner.setLogWriter( new PrintWriter( System.out ));
runner.runScript(Resources.getResourceAsReader( " hsql/test.sql " ));
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
}
String resource = " test/persistence/sql-map-config.xml " ;
Reader reader = Resources.getResourceAsReader (resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
throw new RuntimeException( " Description. Cause: " + e, e);
}
}
public static SqlMapClient getSqlMap(){
return sqlMap ;
}
}
import com.ibatis.common.jdbc.ScriptRunner;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
/**
* damo api
* http://portals.apache.org/bridges/multiproject/jpetstore/apidocs/overview-summary.html
* @author Administrator
*
*/
public class DaoConfig {
private static SqlMapClient sqlMap = null ;
static {
try {
Properties props = Resources.getResourceAsProperties( " properties/database.properties " );
String url = props.getProperty( " url " );
String driver = props.getProperty( " driver " );
String username = props.getProperty( " username " );
String password = props.getProperty( " password " );
if (url.equals( " jdbc:hsqldb:mem:jpetstore " )) {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url, username, password);
try {
ScriptRunner runner = new ScriptRunner(conn, false , false );
runner.setErrorLogWriter( new PrintWriter( System.out ) );
runner.setLogWriter( new PrintWriter( System.out ));
runner.runScript(Resources.getResourceAsReader( " hsql/test.sql " ));
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
}
String resource = " test/persistence/sql-map-config.xml " ;
Reader reader = Resources.getResourceAsReader (resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
throw new RuntimeException( " Description. Cause: " + e, e);
}
}
public static SqlMapClient getSqlMap(){
return sqlMap ;
}
}
dao:
public class LevelDao {
public static List < Level > getAll(){
try {
return DaoConfig.getSqlMap().queryForList( " getAllLevel " );
} catch (Exception e) {
e.printStackTrace();
}
return null ;
}
}
持久类说明
package
test.domain;
import java.util.List;
public class Level {
private Integer id ;
private String name ;
//得到此对象父节点 (多对一)
private Level flevel ;
//得到此对象孩子节点 (一对多)
private List < Level > clevel;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public Level getFlevel() {
return flevel;
}
public void setFlevel(Level flevel) {
this .flevel = flevel;
}
public List < Level > getClevel() {
return clevel;
}
public void setClevel(List < Level > clevel) {
this .clevel = clevel;
}
}
import java.util.List;
public class Level {
private Integer id ;
private String name ;
//得到此对象父节点 (多对一)
private Level flevel ;
//得到此对象孩子节点 (一对多)
private List < Level > clevel;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public Level getFlevel() {
return flevel;
}
public void setFlevel(Level flevel) {
this .flevel = flevel;
}
public List < Level > getClevel() {
return clevel;
}
public void setClevel(List < Level > clevel) {
this .clevel = clevel;
}
}
level.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd" >
< sqlMap namespace ="level" >
< typeAlias alias ="level" type ="test.domain.Level" />
< resultMap class ="level" id ="getLevelByFa" >
< result property ="id" column ="id" />
< result property ="name" column ="name" />
< result property ="flevel" column ="fid" select ="level.getLevelId" />
< result property ="clevel" column ="id" select ="level.getLevelByfId" />
</ resultMap >
< select id ="getLevelId" resultClass ="level" parameterClass ="int" resultMap ="getLevelByFa" >
select
id ,
name ,
fid
from level
WHERE id =#id#
</ select >
< select id ="getLevelByfId" resultClass ="level" parameterClass ="int" resultMap ="getLevelByFa" >
select
id ,
name ,
fid
from level
WHERE fid =#id#
</ select >
</ sqlMap >
<! DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd" >
< sqlMap namespace ="level" >
< typeAlias alias ="level" type ="test.domain.Level" />
< resultMap class ="level" id ="getLevelByFa" >
< result property ="id" column ="id" />
< result property ="name" column ="name" />
< result property ="flevel" column ="fid" select ="level.getLevelId" />
< result property ="clevel" column ="id" select ="level.getLevelByfId" />
</ resultMap >
< select id ="getLevelId" resultClass ="level" parameterClass ="int" resultMap ="getLevelByFa" >
select
id ,
name ,
fid
from level
WHERE id =#id#
</ select >
< select id ="getLevelByfId" resultClass ="level" parameterClass ="int" resultMap ="getLevelByFa" >
select
id ,
name ,
fid
from level
WHERE fid =#id#
</ select >
</ sqlMap >
map confg
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd" >
< sqlMapConfig >
< properties resource ="properties/database.properties" />
< settings
useStatementNamespaces ="true"
/>
< transactionManager type ="JDBC" >
< dataSource type ="SIMPLE" >
< property value ="${driver}" name ="JDBC.Driver" />
< property value ="${url}" name ="JDBC.ConnectionURL" />
< property value ="${username}" name ="JDBC.Username" />
< property value ="${password}" name ="JDBC.Password" />
</ dataSource >
</ transactionManager >
< sqlMap resource ="test/persistence/level.xml" />
</ sqlMapConfig >
<! DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd" >
< sqlMapConfig >
< properties resource ="properties/database.properties" />
< settings
useStatementNamespaces ="true"
/>
< transactionManager type ="JDBC" >
< dataSource type ="SIMPLE" >
< property value ="${driver}" name ="JDBC.Driver" />
< property value ="${url}" name ="JDBC.ConnectionURL" />
< property value ="${username}" name ="JDBC.Username" />
< property value ="${password}" name ="JDBC.Password" />
</ dataSource >
</ transactionManager >
< sqlMap resource ="test/persistence/level.xml" />
</ sqlMapConfig >