1、sql-map-config.xml在src目录下:
public static SqlMapClient getSqlMapInstance() throws IOException {
Reader reader = Resources
.getResourceAsReader("sql-map-config.xml");
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
return sqlMap;
}
2、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>
<!-- Configure a built-in transaction manager. If you're using an
app server, you probably want to use its transaction manager
and a managed datasource -->
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@10.10.10.10:1521:ora11g"/>
<property name="JDBC.Username" value="tempuser"/>
<property name="JDBC.Password" value="tempuser"/>
</dataSource>
</transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the
classpath, as they are here (com.domain.data...) -->
<sqlMap resource="../com/email/xml/EmailAdressEntity.xml"/>
</sqlMapConfig>
如果EmailAdressEntity.xml文件在src/com/email/xml包下,则需要按照如上配置加载资源文件
如果是在src目录下和sql-map-config.xml在同一个目录下,则需要如下配置即可
<sqlMap resource="../EmailAdressEntity.xml"/>
3、EmailAdressEntity.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="EmailAdressEntity">
<!-- Use type aliases to avoid typing the full classname every time. -->
<typeAlias alias="EmailAdressEntity" type="com.email.entiy.EmailAdressEntity" />
<!--
Result maps describe the mapping between the columns returned from a
query, and the class properties. A result map isn't necessary if the
columns (or aliases) match to the properties exactly.
-->
<resultMap id="EmailAdressEntityResult" class="EmailAdressEntity">
<result property="eid" column="eid" />
<result property="email" column="email" />
<result property="epasswd" column="epasswd" />
</resultMap>
<!--
Select with no parameters using the result map for Account class.
-->
<select id="selectAllemails" resultMap="EmailAdressEntityResult">
select * from emailaddress
</select>
<!--
A simpler select example without the result map. Note the aliases to
match the properties of the target result class.
-->
<select id="selectemailById" parameterClass="String"
resultClass="EmailAdressEntity">
select
eid,
email,
epasswd
from emailaddress
where eid = #eid#
</select>
<select id="selectemailByEmail" parameterClass="String"
resultClass="EmailAdressEntity">
select
eid,
email,
epasswd
from emailaddress
where email = #email#
</select>
<!-- Insert example, using the email parameter class -->
<insert id="insertemail" parameterClass="EmailAdressEntity">
insert into emailaddress (
eid,
email,
epasswd
)
values (
#eid#, #email#, #epasswd#
)
</insert>
<!-- Update example, using the email parameter class -->
<update id="updateemail" parameterClass="String">
update emailaddress set
eid = #eid#,
email = #email#,
epasswd = #epasswd#
where
eid = #eid#
</update>
<!-- Delete example, using an integer as the parameter class -->
<delete id="deleteemailById" parameterClass="String">
delete from emailaddress where eid = #eid#
</delete>
</sqlMap>