目录结构:
Test.java
package com.you.me; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.List; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; /** * author:you.me 2010-12-22 * */ public class Test { static SqlMapClient sqlMapClient = null; static { try { Reader reader = Resources.getResourceAsReader("com/you/me/SqlMapConfig.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException, SQLException { /**查询FIND BY ID*/ // User user = (User) sqlMapClient.queryForObject("getUser",new Integer(2)); // System.out.println(user.getName()); /**查询FIND ALL*/ // List list = sqlMapClient.queryForList("getAllUser"); // System.out.println(list.size()); /**插入INSERT*/ // User user = new User(3,"王五","male",25,"wangwu","123456"); // sqlMapClient.insert("insertUser",user); /**删除DELETE*/ // sqlMapClient.delete("deleteUser",new Integer(2)); /**修改UPDATE*/ // User user = new User(3,"王老五","male",24,"wanglaowu","123456"); // sqlMapClient.update("updateUser",user); } }
User.java
package com.you.me; public class User { private int id; private String name; private String sex; private int age; private String username; private String password; public User() { } public User(int id, String name, String sex, int age, String username, String password) { super(); this.id = id; this.name = name; this.sex = sex; this.age = age; this.username = username; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } 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; } }
db.properties 用于通过属性文件连接数据库。如果在SqlMapConfig.xml中直接连接数据库则不需要这个。
driver=com.mysql.jdbc.Driver url=jdbc:mysql://192.168.1.74:3306/test user=root password=root
SqlMapConfig.xml 连接是的mysql数据库。需要加入mysq jar包mysql-connector-java-5.1.15-bin.jar 和ibatis核心包ibatis-2.3.4.726.jar
<?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>
<properties resource="com/you/me/db.properties" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}" /><!--通过属性文件连接数据库 -->
<property name="JDBC.ConnectionURL" value="${url}" />
<property name="JDBC.Username" value="${user}" />
<property name="JDBC.Password" value="${password}" />
<!-- <property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://192.168.1.74:3306/test" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="root" /> -->
</dataSource>
</transactionManager>
<sqlMap resource="com/you/me/User.xml" />
</sqlMapConfig>
<!-- SqlMapConfig.xml是ibatis连接数据库的配置文件 -->
User.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="User"> <!-- create table temp_user ( id number primary key , name varchar2(30), sex varchar2(10), age number , username varchar2(30), password varchar2(30)); insert into temp_user values(1,'张三','male',23,'zhangsan','123456'); insert into temp_user values(2,'李四','male',23,'lisi','123456'); commit; --> <select id="getUser" parameterClass="int" resultClass="com.you.me.User"><!--parameterClass传值参数类型 resultClass返回值类型--> select id,name,sex,age,username,password from temp_user where id = #id# </select> <select id="getAllUser" resultClass="java.util.List"> select id,name,sex,age,username,password from temp_user </select> <insert id="insertUser" parameterClass="com.you.me.User"> insert into temp_user values(#id#,#name#,#sex#,#age#,#username#,#password#) </insert> <delete id="deleteUser" parameterClass="int"> delete from temp_user where id = #id# </delete> <update id="updateUser" parameterClass="com.you.me.User"> update temp_user set name = #name# , age = #age# , username = #username# , password = #password# where id =#id# </update> </sqlMap>
怎么样,ibatis也不是很难吧,将所有的SQL语句统一管理起来,十分方便,对于写复杂的SQL语句也可以直接来写
顺便介绍下MyBatis
MyBatis来源于iBATIS,iBATIS是一个由Clinton Begin在2001年发起的开放源代码项目,iBATIS一词来源于“internet”和“abatis”的组合。该项目最初侧重于密码软件的开发,现在是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO),它是著名的ORM开发框架,分为Java和.NET版本,有着众多的追随者。
iBATIS更名为MyBatis并迁移到Google Code,此次项目迁移后,将启用新的网站http://www.mybatis.org/,由于目前只是改了名字,因此仍然可直接浏览iBatis的文档。
MyBatis开发团队希望脱离Apache而独立发展,并保证不会修改授权协议(Apache License)、代码完全兼容、包名不会更改、也不会删除 Apache站上的任何相关资源。
改名后的第一次版本MyBatis 3.0.1已经发布,基于iBatis 3.0版本,该版本非常稳定,已经有很多用户使用了数周时间,修复了一些小bug。欲下载 MyBatis 3.0.1请到它新的网站http://www.mybatis.org/。