创建SqlMapClient 对象的类
public class SqlConfig {
private static final SqlMapClient sqlMap;
static {
try {
String resource = "../sql-map-config.xml";
Reader reader = Resources.getResourceAsReader (resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException ("Error initializing MyAppSqlConfig class. Cause: " + e);
}
}
public static SqlMapClient getSqlMapInstance () {
return sqlMap;
}
}
sql-map-config.xml文件:
<?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="../database.properties"/>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>
<!-- Type aliases allow you to use a shorter name for long fully qualified class names. -->
<typeAlias alias="order" type="testdomain.Order"/>
<!-- Configure a datasource to use with this SQL Map using SimpleDataSource.
Notice the use of the properties from the above resource -->
<transactionManager type="JDBC" >
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
<!-- Identify all SQL Map XML files to be loaded by this SQL map.
Notice the paths are relative to the classpath. -->
<sqlMap resource="com/globalzt/b2c/persistence/administration/sql/Goods.xml" />
<sqlMap resource="com/globalzt/b2c/persistence/administration/sql/Login.xml" />
<sqlMap resource="com/globalzt/b2c/persistence/administration/sql/GoodsBrand.xml" />
<sqlMap resource="com/globalzt/b2c/persistence/administration/sql/GoodsCategory.xml" />
<sqlMap resource="com/globalzt/b2c/persistence/administration/sql/Menu.xml" />
</sqlMapConfig>
database.properties文件:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.1.112:3306/b2c?characterEncoding=utf-8
username=b2c
password=b2c
public class SqlConfig {
private static final SqlMapClient sqlMap;
static {
try {
String resource = "../sql-map-config.xml";
Reader reader = Resources.getResourceAsReader (resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException ("Error initializing MyAppSqlConfig class. Cause: " + e);
}
}
public static SqlMapClient getSqlMapInstance () {
return sqlMap;
}
}
sql-map-config.xml文件:
<?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="../database.properties"/>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>
<!-- Type aliases allow you to use a shorter name for long fully qualified class names. -->
<typeAlias alias="order" type="testdomain.Order"/>
<!-- Configure a datasource to use with this SQL Map using SimpleDataSource.
Notice the use of the properties from the above resource -->
<transactionManager type="JDBC" >
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
<!-- Identify all SQL Map XML files to be loaded by this SQL map.
Notice the paths are relative to the classpath. -->
<sqlMap resource="com/globalzt/b2c/persistence/administration/sql/Goods.xml" />
<sqlMap resource="com/globalzt/b2c/persistence/administration/sql/Login.xml" />
<sqlMap resource="com/globalzt/b2c/persistence/administration/sql/GoodsBrand.xml" />
<sqlMap resource="com/globalzt/b2c/persistence/administration/sql/GoodsCategory.xml" />
<sqlMap resource="com/globalzt/b2c/persistence/administration/sql/Menu.xml" />
</sqlMapConfig>
database.properties文件:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.1.112:3306/b2c?characterEncoding=utf-8
username=b2c
password=b2c