Ibaties使用实例一

本实例是只使用ibaties进行的开发,后续会分享使用spring的例子。

 

第一步:编写sqlmap.xml文件

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE sqlMapConfig
 PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
 "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
 
 <sqlMapConfig>
  <settings cacheModelsEnabled="true" enhancementEnabled="true"
   lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
   maxTransactions="5" useStatementNamespaces="false" />
 
  <transactionManager type="JDBC">
 
   <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>

  <sqlMap resource="com/warefont/pojo/Employee.xml" />
 </sqlMapConfig>

 

第二步:编写Employee.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
 <sqlMap namespace="employee">
  <insert id="insert_employee" parameterClass="com.warefont.pojo.Employee">
   insert into employee(name,gender,department) values
   (#name#,#gender#,#department#)
  </insert>
 
  <select id="getEmployeet" resultClass="com.warefont.pojo.Employee">
   select id, name, gender,department from employee
  </select>
 
  <delete id="delEmployee" parameterClass="int">
   delete from employee where id=#value#
  </delete>
 
  <update id="updateEmployee" parameterClass="com.warefont.pojo.Employee">
   update employee set name=#name#,gender=#gender#,department=#department#
   where id=#id#
  </update>
 </sqlMap>

第三步:将配置文件注入到程序中

package com.warefont.util;

import java.io.IOException;
import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class GetSqlMapClient {

 private static SqlMapClient sqlMapClient;

 static {
  String resource = "sqlMap.xml";
  Reader reader = null;
  try {
   reader = Resources.getResourceAsReader(resource);
   sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public static SqlMapClient getSqlMapInstance() {
  return sqlMapClient;
 }

}
第四步:使用

package com.warefont.dao;

import java.sql.SQLException;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.warefont.pojo.Employee;
import com.warefont.util.*;

public class EmployeeDao {

 public void insertEmp(Employee emp){
  SqlMapClient sqlMap = GetSqlMapClient.getSqlMapInstance();
  try {
   sqlMap.insert("insert_employee", emp);
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }

}
第五步:测试

package com.warefont.test;

import com.warefont.dao.EmployeeDao;
import com.warefont.pojo.Employee;

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Employee emp = new Employee();
  emp.setDepartment("dept");
  emp.setGender("testGender");
  emp.setName("hewei");
  
  EmployeeDao empDao = new EmployeeDao();
  empDao.insertEmp(emp);
 }

}

PS:这只是一个简单的应用,但是包含了其基本的使用。

你可能感兴趣的:(sql,xml,mysql,ibatis,jdbc)