第一个ibatis

来源:互联网 发布:淘宝保证金能退回吗 编辑:程序博客网 时间:2024/06/05 14:28

   这个ibatis其实就和hibernate一样,也用来解决阻抗不匹配的问题。总的来说会用hibernate就会用ibatis,从官网来看ibatis已经加入了谷歌阵营,版本也升到了3.0.6.配置文件也有了一些差异。这里我们就是用2.3版本的就可以了。可惜官网上找不到了。现在就来学习一下吧

1.去迅雷上或其他地方下载ibatis的jar包,这个就不多说了

2.把jar包加载进去

3.编写vo类,代码如下所示

package org.lxh.vo;public class Account {private int id;private String username;private String password;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}

2.编写ibatis的配置文件,名称随便什么都可以

<?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="org.gjt.mm.mysql.Driver" /><property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/mldn" /><property name="JDBC.Username" value="root" /><property name="JDBC.Password" value="123456" /></dataSource></transactionManager><sqlMap resource="Account.xml" /></sqlMapConfig>


3.编写ibatis的映射文件,名称随便什么都可以

<?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="Account"><select id="getAccount" parameterClass="java.lang.String" resultClass="org.lxh.vo.Account"> select * from Account </select><insert id="createAccount" parameterClass="org.lxh.vo.Account"> insert into Account(username, password) values ( #username#, #password# )  </insert></sqlMap>

这里的id很重要,后面会用到,parameterClass表示传入的参数类型,resultClass表示查询的结果类型

4.编写Junit来进行测试

package test;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.junit.BeforeClass;import org.lxh.vo.Account;import com.ibatis.sqlmap.client.SqlMapClientBuilder;public class Test{@BeforeClasspublic  static void setUpBeforeClass() throws Exception {}        @org.junit.Test public void insert(){        try {       com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;  java.io.Reader reader = com.ibatis.common.resources.Resources.getResourceAsReader ("sqlmap.xml");          sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);         Account account = new Account();           account.setUsername("micheal");           account.setPassword("1234");           sqlMap.insert("createAccount", account);       }catch(Exception e){    e.printStackTrace();    }    }    @org.junit.Test public void query() {            try {       com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;  java.io.Reader reader = com.ibatis.common.resources.Resources.getResourceAsReader ("sqlmap.xml");          sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);     ArrayList all=(ArrayList) sqlMap.queryForList("getAccount");Iterator it=all.iterator();while(it.hasNext()){Account account=(Account)it.next();System.out.println(account.getUsername());System.out.println(account.getPassword());}    }catch(Exception e){    e.printStackTrace();    }    }}

这里我们可以把相同的代码写成一个类,这里为了方便就不写了。

最后来看一下效果截图


和hibernate相比,ibatis比较轻巧,上手也比较快。也有不少的企业使用这个。但我个人还是比较喜欢hibernate,这个例子比较简单,有问题的和我联系啊。如果写错了给位大神可以丢板砖,我也才刚学这个。


原创粉丝点击