ibatis基础开发

来源:互联网 发布:日本侵华成功知乎 编辑:程序博客网 时间:2024/06/09 19:00

一、工程文件结构

SqlMapConfig.xml为工程ibatis的配置文件

com.test.domain.Employe.java为一个Employ的实体类

com.sql.Employ.xml为Employ.java的映射文件

com.test.util.SQLMapClient.java为获得sqlmap的一个工具

com.test.domain.EmploySQLMapper.java Employe的dao类

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

二、文件内容

1、Employ.java

package com.test.domain;public class Employ {    private int id;    private String enployName;    private int salary;       public Employ() {    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }       public String getEnployName() {        return enployName;    }    public void setEnployName(String enployName) {        this.enployName = enployName;    }    public int getSalary() {        return salary;    }    public void setSalary(int salary) {        this.salary = salary;    }}
 

 

 

2、Employ.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="Employ">  <!-- Use type aliases to avoid typing the full classname every time. -->  <typeAlias alias="Employ" type="com.test.domain.Employ"/>  <!-- 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="EmployResult" class="Employ">    <result property="id" column="id"/>    <result property="enployName" column="employ_name"/>    <result property="salary" column="salary"/>  </resultMap>  <!-- Select with no parameters using the result map for Account class. -->  <select id="selectAllEmploy" resultMap="EmployResult">      <![CDATA[          select * from employees      ]]>  </select>  <!-- A simpler select example without the result map.  Note the       aliases to match the properties of the target result class. -->    <!-- Insert example, using the Account parameter class -->  <insert id="insertEmploy" parameterClass="Employ">      <![CDATA[          insert into employees (employ_name, salary) values(#enployName#, #salary#)      ]]>  </insert>  <!-- Update example, using the Account parameter class -->  <!-- Delete example, using an integer as the parameter class --></sqlMap>
 

 

 

3、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>  <!-- 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="com.mysql.jdbc.Driver"/>      <property name="JDBC.ConnectionURL" value="jdbc:mysql:///test"/>      <property name="JDBC.Username" value="root"/>      <property name="JDBC.Password" value="root"/>    </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/sql/Employ.xml"/>  <!-- List more here...  <sqlMap resource="com/mydomain/data/Order.xml"/>  <sqlMap resource="com/mydomain/data/Documents.xml"/>  --></sqlMapConfig>
 

 

 

4、SQLMapClient.java

package com.test.util;import java.io.IOException;import java.io.Reader;import sun.misc.Resource;import com.ibatis.common.resources.Resources;import com.ibatis.sqlmap.client.SqlMapClient;import com.ibatis.sqlmap.client.SqlMapClientBuilder;public class SQLMapClient {    private static SqlMapClient sqlMaper = null;       static {        String resource = "SqlMapConfig.xml";        try {            Reader reader = Resources.getResourceAsReader(resource);            sqlMaper = SqlMapClientBuilder.buildSqlMapClient(reader);            reader.close();        } catch (IOException e) {            e.printStackTrace();        }    }    public static SqlMapClient getSqlMaper() {        return sqlMaper;    }}
 

 

 

5、EmploySQLMapper.java

package com.test.domain;import java.sql.SQLException;import java.util.List;import com.ibatis.sqlmap.client.SqlMapClient;import com.test.util.SQLMapClient;public class EmploySQLMapper {    private SqlMapClient sqlMapper = null;       public EmploySQLMapper() {        sqlMapper = SQLMapClient.getSqlMaper();    }       public void insertEmploy(Employ employ) throws SQLException {        sqlMapper.insert("insertEmploy", employ);    }       public List<Employ> selectAllEmploy() throws SQLException {        return sqlMapper.queryForList("selectAllEmploy");    }       public static void main(String[] args) throws SQLException {        Employ employ = new Employ();        employ.setEnployName("sun");        employ.setSalary(1500);//               EmploySQLMapper employSQLMapper = new EmploySQLMapper();//        employSQLMapper.insertEmploy(employ);               List<Employ> employees = employSQLMapper.selectAllEmploy();               for (Employ e : employees) {            System.out.println(e.getEnployName() + ":" + e.getSalary());        }    }}