Spring、Ibatis結合MySQL資料庫的使用方法

來源:互聯網
上載者:User

標籤:

        Ibatis是MyBatis的前身,它是一個開源的持久層架構。它的核心是SqlMap——將實體Bean跟關聯式資料庫進行映射,將業務代碼和SQL語句的書寫進行分開。Ibatis是“半自動化”的ORM持久層架構。這裡的“半自動化”是相對Hibernate等提供了全面的資料庫封裝機制的“全自動化”ORM實現而言的,“全自動”ORM實現了POJO與資料庫表欄位之間的映射並且實現了SQL的自動產生和執行。而Ibatis的著力點,則在於POJO與SQL之間的映射關係,即Ibatis並不會為程式員在運行期自動產生並執行SQL,具體的SQL語句需要程式員編寫,然後通過映射設定檔將SQL語句所需的參數和返回的結果欄位對應到指定POJO中。本篇部落格示範了Spring、Ibatis結合MySQL資料庫的使用方法:

        工程結構如:


        由於該例子介紹的比較全面,檔案比較多,這裡只給出標出的三個檔案中的代碼,完整的源碼可通過點擊本文最下面的超連結下載:

        StudentDao.java檔案中的代碼:

package com.ghj.dao.imp;import java.sql.SQLException;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;import com.ghj.dao.IStudentDao;import com.ghj.vo.Student;/** * 學生管理資料訪問層介面實作類別 *  * @author 高煥傑 */public class StudentDao extends SqlMapClientDaoSupport implements IStudentDao {/** * 新增學生資訊 *  * @author 高煥傑 */@Overridepublic boolean add(Student student) throws SQLException{return getSqlMapClientTemplate().update("add", student) > 0;}/** * 依據使用者名稱刪除學生資訊 *  * @author 高煥傑 */@Overridepublic boolean deleteByUserName(String userName) throws SQLException{return getSqlMapClientTemplate().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 getSqlMapClientTemplate().update("updatePasswordByUserName", params) > 0;}/** * 根據學生使用者名稱查詢學生資訊 *  * @author 高煥傑 */@Overridepublic Student findByUserName(String userName) throws SQLException{return (Student)getSqlMapClientTemplate().queryForObject("findByUserName", userName);}/** * 查詢所有學生資訊 *  * @author 高煥傑 */@Override@SuppressWarnings("unchecked")public List<Student> findAll() throws SQLException{return getSqlMapClientTemplate().queryForList("findAll", null);}}

        application-context.xml檔案中的代碼:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"default-autowire="byName" default-lazy-init="false"><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="false" destroy-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"></property><property name="user" value="root"></property><property name="password" value=""></property><property name="acquireIncrement" value="5"></property><property name="initialPoolSize" value="5"></property><property name="minPoolSize" value="5"></property><property name="maxPoolSize" value="20"></property><property name="maxStatements" value="100"></property><property name="numHelperThreads" value="10"></property><property name="maxIdleTime" value="60"></property></bean>    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  <property name="configLocation">            <value>classpath:config/sqlMapConfig.xml</value>        </property>  <property name="dataSource" ref="dataSource"/></bean><bean id="studentDao" class="com.ghj.dao.imp.StudentDao">  <property name="sqlMapClient">    <ref bean="sqlMapClient"/>  </property></bean><!-- Spring中配置事務操作:在Spring中實現事務操作有多種方式,其中以註解的方式最為常用,該種方式需要在需要配置事務操作的方法上添加@Transactional注釋   -->    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean><tx:annotation-driven transaction-manager="dataSourceTransactionManager" /></beans>

        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分下載該範例程式碼

Spring、Ibatis結合MySQL資料庫的使用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.