Ibatis 扫盲

本文介绍了MyBatis的基本配置及使用方法,包括实体类、SQL语句配置、连接池设置等关键步骤,并通过示例展示了如何进行增删改查操作。

Eclipse 3.72

工程目录图:


table     :   tes

coulme:   id(int),name(string),factory(string),date(Date)


实体类:Tes.java


sql语句的配置文件tes.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">  
    <!--  表的命名空间,和sql语句配置,注意类型为对应的实体类 -->
<sqlMap namespace="Tes">
<typeAlias alias="tes" type="com.iba.Tes"/>
<select id="getTes" parameterClass="java.lang.String" resultClass="tes">
select id,name,factory,date from tes where name=#name#
</select>
<update id="updateTes" parameterClass="tes">
update tes set name=#name#,factory=#factory#,date=#date# where id=#id#
</update>
<insert id="insertTes" parameterClass="tes">  
INSERT INTO tes (id, name,factory,date) VALUES (#id#, #name#, #factory#,#date#)  
</insert>          
<delete id="deletetes" parameterClass="java.lang.String">  
delete from tes where id = #value#  
</delete>  
</sqlMap>



SqlMapConfig.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 
        <!-- Ibatis 的功能属性配置-->       
            cacheModelsEnabled="true" enhancementEnabled="true"  
            lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"  
            maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />  
        <transactionManager type="JDBC">  
            <dataSource type="SIMPLE">  
                <!-- JDBC驱动 -->  
                <property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />  
                <!-- 数据库URL -->  
                <property name="JDBC.ConnectionURL" value="jdbc:mysql://172.16.52.107:3306/ioffice52" />  
                <!-- 数据库用户名 -->  
                <property name="JDBC.Username" value="root" />  
                <!-- 数据库密码 -->  
                <property name="JDBC.Password" value="root" />  
                <property name="Pool.MaximumActiveConnections" value="10" />  
                <property name="Pool.MaximumIdleConnections" value="5" />  
                <property name="Pool.MaximumCheckoutTime" value="120000" />  
                <property name="Pool.TimeToWait" value="500" />  
                <property name="Pool.PingQuery" value="select 1 from ACCOUNT" />  
                <property name="Pool.PingEnabled" value="false" />  
                <property name="Pool.PingConnectionsOlderThan" value="1" />  
                <property name="Pool.PingConnectionsNotUsedFor" value="1" />  
            </dataSource>  
        </transactionManager>  
        <!-- 指定映射文件的位置,配置中可出现多个sqlMap节点,以指定项目内所有映射文件 -->  
        <sqlMap resource="com/iba/Ibatis.xml" />  
         <sqlMap resource="com/iba/tes.xml" />  
    </sqlMapConfig>  

测试类TestMain.java

package com.test;

import java.io.IOException;
import java.io.Reader;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

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

public class TestMain {

	    public static void main(String[] args) throws IOException, SQLException {
//	    	insIbatis();
//	    	insTes();
//	    	getTes();
	    	delTes();
//	    	updTes();
				System.out.println("DataBass option is success!");
	    }  
	    public static SqlMapClient aa() throws IOException, SQLException
	    {
	    	String resource ="SqlMapConfig.xml";
	    	Reader reader;
	    	reader = Resources.getResourceAsReader(resource);
	    	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);  
			sqlMap.startTransaction();  
	    	return sqlMap;
	    }
	    public static void insIbatis() throws IOException, SQLException{
	    	SqlMapClient sqlMap=aa();
	    	Ibatis ibatis = new Ibatis();  
	          ibatis.setId(new Integer(2));  
	          ibatis.setName("Tom");  
	          ibatis.setAge(new Integer(22));  
	          sqlMap.insert("insertIbatis",ibatis);  
	          sqlMap.commitTransaction();  
	    }
	    public static void insTes() throws IOException, SQLException{
	    	SqlMapClient sqlMap=aa();
	    	Tes ibatis = new Tes();  
	          ibatis.setId(new Integer(55));  
	          ibatis.setName("Tom");  
	          ibatis.setFactory("factory");  
	          sqlMap.insert("insertTes",ibatis);  
	          sqlMap.commitTransaction();  
	    }
	    public static void getTes() throws IOException, SQLException{
	    	SqlMapClient sqlMap=aa();
	          List<Tes> tes = (List<Tes>)sqlMap.queryForList("getTes", "Tom");  
	          System.out.println(tes.toString()); 
	          sqlMap.commitTransaction();  
	    }
	    public static void delTes() throws IOException, SQLException{
	    	SqlMapClient sqlMap=aa();
	    	Tes tes = new Tes();
	          sqlMap.delete("deletetes","1");
	          sqlMap.commitTransaction();  
	    }
	    public static void updTes() throws IOException, SQLException{
	    	SqlMapClient sqlMap=aa();
	    	Tes tes = new Tes();
	    	tes.setDate(new Date(System.currentTimeMillis()));
	    	tes.setFactory("upde_tes");
	    	tes.setName("shuohaoln e ");
	    	tes.setId(1);
	          sqlMap.update("updateTes", tes);
	          sqlMap.commitTransaction();  
	    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值