IbTis
简介:
数据库操作框架
优点:
增加设计上的灵活性
支持延迟加载、缓存、映射等。。
半自动化,需要自己写SQL,可以提高SQL的灵活性和性能
缺点:
与数据库兼容不好
移植性差
与Hibernate的映射区别:
Hibernate映射是元数据映射,就是映射类的属性
ibtis是数据映射,就是映射SQL的参数和返回值
操作过程:
1、创建数据库配置文件(SqlMapConfig.xml)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="net.sourceforge.jtds.jdbc.Driver"/> <property name="JDBC.ConnectionURL" value="jdbc:jtds:sqlserver://localhost:1433/msdb"/> <property name="JDBC.Username" value="sa"/> <property name="JDBC.Password" value="sa"/> </dataSource> </transactionManager> <!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) --> <sqlMap resource="com/lovo/orm/userBean.xml"/> <!-- List more here... <sqlMap resource="com/mydomain/data/Order.xml"/> <sqlMap resource="com/mydomain/data/Documents.xml"/> --> </sqlMapConfig>
2、创建PO对象
public class UserBean implements Serializable { private static final long serialVersionUID = 1L; private int id; private String name; private int age; }
3、创建SQL MAP映射文件
<?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> <typeAlias alias="users" type="com.lovo.bean.UserBean"/> <insert id="add" parameterClass="users"> insert into t_user(userName,age) values(#name#,#age#) </insert> <delete id="del" parameterClass="int"> delete from t_user where id = #aaa# </delete> <update id="update" parameterClass="users"> update t_user set userName=#name#,age=#age# where id=#id# </update> <select id="sel" resultClass="users"> select id,userName as name,age from t_user </select> <!-- 多条件查询一般把条件封装为一个对象 --> <select id="selByMore" parameterClass="users" resultClass="users"> select id,userName as name,age from t_user where userName like #name# and age > #age# </select> </sqlMap>
4、创建测试类
package com.lovo.test; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import com.lovo.bean.UserBean; public class Main { private static SqlMapClient sqlMap; static { Reader reader; try { reader = Resources.getResourceAsReader("SqlMapConfig.xml"); sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } public Main() { // 添加 //add(new UserBean("八戒",23)); // 删除 //del(1); // 修改 //update(new UserBean(2,"八哥",18)); // 查询 //sreach(); // 多条件查询 selByMore(); } public void selByMore() { try { System.out.println(sqlMap.queryForList("selByMore", new UserBean(1,"%哥%",16))); } catch (SQLException e) { e.printStackTrace(); } } public void sreach() { try { System.out.println(sqlMap.queryForList("sel").toString()); } catch (SQLException e) { e.printStackTrace(); } } public void update(UserBean userBean) { try { sqlMap.update("update",userBean); } catch (SQLException e) { e.printStackTrace(); } } public void del(int i) { try { sqlMap.delete("del",1); } catch (SQLException e) { e.printStackTrace(); } } public void add(UserBean userBean) { try { sqlMap.insert("add",userBean); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { new Main(); } }
iBatis 框架体系
最新推荐文章于 2019-01-08 07:44:42 发布