ibatis-Day01

ibatis learning process

The establishment of a ibatis SqlClient Sql sqlMapConfig client by reading information in the database and as SqlMapConfig configuration, and loading different data sources SqlMap map file, thus calling its methods by ibatisTest SqlClient, then in its new in SqlClient the SqlMap client to invoke the sql statement corresponding map file

 

 

SqlMap mapping file

 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     // return result set mapping 15 exactly -.> 14 Necessary IF the columns (or aliases) match to the properties
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 class (ibatis in the configuration database connection properties and SqlMap SqlMapConfig the data source)

 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"> 13 <dataSource type = "SIMPLE"
    // configure the Oracle data source 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 (to read the information in the SqlClient SqlMapConfig, and establishing a client SqlMap The information, and new methods in the class, using the client to call a method in the method of 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 a java.io.Reader; Import java.io.IOException; Import java.util.List; Import java.sql.SQLException; public class the SqlClient {Private static the SqlMapClient sqlMapper; static {the try { 
    // SqlMapConfig information read Reader = Resources.getResourceAsReader Reader ( "the SqlMapConfig.xml" );
    // create a SqlMapClient client information according to the read 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 by a method corresponding to call the corresponding the 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 }

 

Guess you like

Origin www.cnblogs.com/idefault/p/11688653.html