ibatis学习过程
ibatis的SqlClient通过读取sqlMapConfig信息建立一个Sql客户端,而由于SqlMapConfig中对数据库进行配置,并且加载数据源不同的SqlMap映射文件,因此在ibatisTest中通过SqlClient调用其方法,然后在SqlClient中通过其新建的SqlMap客户端来调用对应的映射文件中的sql语句
SqlMap映射文件
1 <?xml version="1.0" encoding="UTF-8" ?>
2
3 <!DOCTYPE sqlMap
4 PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
5 "http://ibatis.apache.org/dtd/sql-map-2.dtd">
6 //命名空间
7 <sqlMap namespace="emp">
8
9 <!-- Use type aliases to avoid typing the full classname every time. -->
//别名
10 <typeAlias alias="emp" type="com.yinhai.ibatisdemo.ibatis.domain.Emp"/>
11
12 <!-- Result maps describe the mapping between the columns returned
13 from a query, and the class properties. A result map isn't
14 necessary if the columns (or aliases) match to the properties
15 exactly. -->
//返回结果集映射
16 <resultMap id="empResult" class="emp">
17 <result property="empno" column="EMPNO"/>
18 <result property="ename" column="ENAME"/>
19 <result property="job" column="JOB"/>
20 <result property="mgr" column="MGR"/>
21 <result property="hiredate" column="HIREDATE"/>
22 <result property="sal" column="SAL"/>
23 <result property="comm" column="COMM"/>
24 <result property="deptno" column="DEPTNO"/>
25 <result property="gender" column="GENDER"/>
26 </resultMap>
27
28 <!-- Select with no parameters using the result map for Account class. -->
29 <select id="selectAllEmps" resultMap="empResult">
30 select empno,
31 ename,
32 job,
33 mgr,
34 hiredate,
35 sal,
36 comm,
37 deptno,
38 gender
39 from emp
40 </select>
41 <select id="selectEmpById" parameterClass="java.lang.Integer" resultClass="emp">
42
43 select empno,
44 ename,
45 job,
46 mgr,
47 hiredate,
48 sal,
49 comm,
50 deptno,
51 gender
52 from emp
53 where empno = #empno#
54 </select>
55 <delete id="deleteEmp" parameterClass="java.lang.Integer">
56 delete from emp where empno=#empno#
57 </delete>
58 <update id="updateEmp" parameterClass="com.yinhai.ibatisdemo.ibatis.domain.Emp">
59 update emp set ename=#ename# where empno=#empno#
60 </update>
61 <insert id="insertEmp" parameterClass="com.yinhai.ibatisdemo.ibatis.domain.Emp">
62 insert into emp(empno,ename) values (#empno#,#ename#)
63 </insert>
64 </sqlMap>
SqlMapConfig类 (在ibatis中,在SqlMapConfig中配置数据库连接属性和SqlMap数据源)
1 <?xml version="1.0" encoding="UTF-8" ?>
2
3 <!DOCTYPE sqlMapConfig
4 PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
5 "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
6
7 <sqlMapConfig>
8 <settings useStatementNamespaces="true"/>
9 <!-- Configure a built-in transaction manager. If you're using an
10 app server, you probably want to use its transaction manager
11 and a managed datasource -->
//节点 事务交给JDBC处理
12 <transactionManager type="JDBC" commitRequired="false">
//配置Oracle数据源
13 <dataSource type="SIMPLE">
14 <property name="JDBC.Driver" value="oracle.jdbc.OracleDriver"/>
15 <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@192.168.10.135:1521:orcl"/>
16 <property name="JDBC.Username" value="px404"/>
17 <property name="JDBC.Password" value="px404"/>
18 </dataSource>
19 </transactionManager>
20
21 <!-- List the SQL Map XML files. They can be loaded from the
22 classpath, as they are here (com.domain.data...) -->
//配置SqlMap映射文件数据源
23 <sqlMap resource="emp.xml"/>
24
28
29 </sqlMapConfig>
SqlClient(在SqlClient中去读取SqlMapConfig的信息,并且根据信息建立一个SqlMap客户端,并在该类中新建方法,在方法中利用客户端去调用SqlMap中的方法)
package com.yinhai.ibatisdemo.ibatis.test;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import com.yinhai.ibatisdemo.ibatis.domain.Emp;
import java.io.Reader;
import java.io.IOException;
import java.util.List;
import java.sql.SQLException;
public class SqlClient {
private static SqlMapClient sqlMapper;
static {
try {
//读取SqlMapConfig的信息
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
//根据读取到的信息建立一个SqlMapClient客户端
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
//关闭读取流
reader.close();
} catch (IOException e) {
// Fail fast.
throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
}
}
// 查询所有信息
public static List selectAllEmps () throws SQLException {
return sqlMapper.queryForList("emp.selectAllEmps");
}
//根据id查询信息
public static Emp selectEmpById(Integer empno) throws SQLException {
return (Emp)sqlMapper.queryForObject("emp.selectEmpById",empno);
}
/*新增*/
public static int insertEmp(Emp emp)throws SQLException{
return sqlMapper.update("emp.insertEmp",emp);
}
/*修改*/
public static int updateEmp(Emp emp)throws SQLException{
return sqlMapper.update("emp.updateEmp",emp);
}
/*删除*/
public static int deleteEmp(Integer empno)throws SQLException{
return sqlMapper.update("emp.deleteEmp",empno);
}
}
ibatisTest(通过SqlClient对应的方法去调用对应的SqlMap)
1 package com.yinhai.ibatisdemo.ibatis.test;
2
3 import com.yinhai.ibatisdemo.ibatis.domain.Emp;
4
5 import java.sql.SQLException;
6 import java.util.List;
7
8 public class ibatisTests {
9 public static void main(String[] args) {
10 try {
11 //查询
12 List list=SqlClient.selectAllEmps();
13 for (Object i:list
14 ) {
15 System.out.println(i);
16 }
17 // System.out.print("one"+list.toString());
18
19 //根据id查询
20 // Emp emp=SqlClient.selectEmpById(9999);
21 // System.out.print("two "+emp);
22 //删除
23 // int i = SqlClient.deleteEmp(1466);
24 // System.out.print(i);
25
26 //修改
27 // Emp emp = new Emp();
28 // emp.setEname("测试");
29 // emp.setEmpno(1483);
30 // int i = SqlClient.updateEmp(emp);
31 // System.out.print(i);
32 //新增
33 // Emp emp = new Emp();
34 // emp.setEmpno(1011);
35 // emp.setEname("傅旭东");
36 // int i = SqlClient.insertEmp(emp);
37 // System.out.print(i);
38 }catch(SQLException e){
39 e.printStackTrace();
40 }
41
42 }
43 }