ibatis

来源:互联网 发布:数据验证 excel 编辑:程序博客网 时间:2024/06/09 18:31

sqlMapConfig.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>
<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:@localhost:1521:jiahua" /> 
  <property name="JDBC.Username" value="stu" /> 
  <property name="JDBC.Password" value="stu" /> 
  </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/xml/Student.xml" /> 
 <!--  List more here...
  <sqlMap resource="com/mydomain/data/Order.xml"/>
  <sqlMap resource="com/mydomain/data/Documents.xml"/>
  
  -->
  </sqlMapConfig> 


Student.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>

<typeAlias alias="Student" type="com.entity.Student" />
<!-- id 为sql 语句的别名                  resultClass 为返回值类型 -->
<select id="selectAllStudent" resultClass="Student">
select * from student
</select>

<!--  where id=#id# 表示从外部传进来的 id -->
<select id="selectStudentById" parameterClass="int" resultClass="Student">
select * from student where id=#id#
</select>

<insert id="insertStudent" parameterClass="Student">
<selectKey resultClass="int" keyProperty="id">
select STUDENT_SEQ.nextVal from dual
</selectKey>
insert into student(id,name) values(#id#,#name#)
</insert>

<delete id="deleteStudent" parameterClass="int">
delete student where id=#id#
</delete>

<update id="updateStudent" parameterClass="Student">
update student set name=#name# where id=#id#
</update>

<!-- 模糊查询 -->
<select id="selectStudentByName" parameterClass="String" resultClass="Student">
select id,name from student where name like '%$name$%'
</select>
</sqlMap>


public class StudentImpl implements StudentDao{

private static SqlMapClient sqlMapClient=null;
static{
try {
Reader reader=com.ibatis.common.resources.Resources.
getResourceAsReader("com/xml/sqlMapConfig.xml");
sqlMapClient= SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

得到 sqlMapClient进行操作数据库