標籤:
Ibatis是MyBatis的前身,它是一個開源的持久層架構。它的核心是SqlMap——將實體Bean跟關聯式資料庫進行映射,將業務代碼和SQL語句的書寫進行分開。Ibatis是“半自動化”的ORM持久層架構。這裡的“半自動化”是相對Hibernate等提供了全面的資料庫封裝機制的“全自動化”ORM實現而言的,“全自動”ORM實現了POJO與資料庫表欄位之間的映射並且實現了SQL的自動產生和執行。而Ibatis的著力點,則在於POJO與SQL之間的映射關係,即Ibatis並不會為程式員在運行期自動產生並執行SQL,具體的SQL語句需要程式員編寫,然後通過映射設定檔將SQL語句所需的參數和返回的結果欄位對應到指定POJO中。本篇部落格將示範Ibatis結合MySQL資料庫的使用方法:
工程結構如:
由於該例子介紹的比較全面,檔案比較多,這裡只給出標出的兩個檔案中的代碼,完整的源碼可通過點擊本文最下面的超連結下載:
StudentDao.java檔案中的代碼:
package com.ghj.dao.imp;import java.io.IOException;import java.io.Reader;import java.sql.SQLException;import java.util.HashMap;import java.util.List;import java.util.Map;import com.ghj.dao.IStudentDao;import com.ghj.vo.Student;import com.ibatis.common.resources.Resources;import com.ibatis.sqlmap.client.SqlMapClient;import com.ibatis.sqlmap.client.SqlMapClientBuilder;/** * 學生管理資料訪問層介面實作類別 * * @author 高煥傑 */public class StudentDao implements IStudentDao {private SqlMapClient sqlMapClient;public StudentDao() {String resource = "config/sqlMapConfig.xml";try {Reader reader = Resources.getResourceAsReader(resource);//讀取設定檔sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);} catch (IOException e) {e.printStackTrace();}}/** * 新增學生資訊 * * @author 高煥傑 */@Overridepublic boolean add(Student student) throws SQLException{return sqlMapClient.update("add", student) > 0;}/** * 依據使用者名稱刪除學生資訊 * * @author 高煥傑 */@Overridepublic boolean deleteByUserName(String userName) throws SQLException{return sqlMapClient.delete("deleteByUserName", userName) > 0;}/** * 依據使用者名稱更新密碼 * * @author 高煥傑 */@Overridepublic boolean updatePasswordByUserName(String userName, String password) throws SQLException{Map<String, Object> params = new HashMap<String, Object>();params.put("userName", userName);params.put("password", password);return sqlMapClient.update("updatePasswordByUserName", params) > 0;}/** * 根據學生使用者名稱查詢學生資訊 * * @author 高煥傑 */@Overridepublic Student findByUserName(String userName) throws SQLException{return (Student)sqlMapClient.queryForObject("findByUserName", userName);}/** * 查詢所有學生資訊 * * @author 高煥傑 */@Override@SuppressWarnings("unchecked")public List<Student> findAll() throws SQLException{return sqlMapClient.queryForList("findAll");}}
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><!-- 為Student類設定一個別名 --><typeAlias alias="student" type="com.ghj.vo.Student"/><!-- 配置表和實體Bean之間的映射關係 --><resultMap id="studentMap" class="com.ghj.vo.Student"><result property="id" column="id"/><result property="userName" column="user_name"/><result property="password" column="password"/><result property="state" column="state"/></resultMap><!-- 新增學生資訊 --><insert id="add" parameterClass="student">insert into student values(#id#, #userName#, #password#, #state#)</insert><!-- 依據使用者名稱刪除學生資訊 --><delete id="deleteByUserName" parameterClass="java.lang.String"> delete from student where user_name=#userName# </delete><!-- 依據使用者名稱更新密碼 --><update id="updatePasswordByUserName" parameterClass="java.util.HashMap"> update student set password=#password# where user_name=#userName# </update><!-- 根據學生使用者名稱查詢學生資訊 --><select id="findByUserName" parameterClass="string" resultMap="studentMap">select * from student where user_name=#userName#</select><!-- 查詢所有學生資訊 --><select id="findAll" resultMap="studentMap">select * from student</select></sqlMap>
【
0分下載該範例程式碼】
Ibatis結合MySQL資料庫的使用方法