iBatis操作DDL和映射继承
本文系iBatis开发详解系列文章之iBatis操作DDL和映射继承。
DDL作为SQL的一个子集,是专门用于数据定义的语言,也就是我们所说的对数据库表/模式的操作。最为常见的就是修改表的结构,比如添加字段,修改字段类型,为字段改名等。那么我们来看看如何使用iBatis来进行DDL操作。
前面介绍的iBatis操作数据库都属于DML范畴,比如select,update,delete等。那么操作DDL我们使用的是statement标签,我们来看看对我们之前示例的订单项表来进行修改:
<statement id="addColumn">alter table orderitem add column type VARCHAR(50);</statement>
package ibatis;import java.io.IOException;import java.io.Reader;import com.ibatis.common.resources.Resources;import com.ibatis.sqlmap.client.SqlMapClient;import com.ibatis.sqlmap.client.SqlMapClientBuilder;public class DDLDemo {private static String config = "ibatis/SqlMapConfig.xml";private static Reader reader;private static SqlMapClient sqlMap;static {try {reader = Resources.getResourceAsReader(config);} catch (IOException e) {e.printStackTrace();}sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);}public static void main(String[] args) throws Exception {sqlMap.update("OrderItem.addColumn", null);}}
package ibatis.model;public class OrderItem implements java.io.Serializable {private Integer orderItemId;private String itemName;private int quantity;private float price;private String type;private Integer orderId;public OrderItem() {}public OrderItem(Integer orderItemId, String itemName, int quantity,float price, String type, Integer orderId) {super();this.orderItemId = orderItemId;this.itemName = itemName;this.quantity = quantity;this.price = price;this.orderId = orderId;}//getters and setterspublic String toString() {return "OrderItem [itemName=" + itemName + ", orderId=" + orderId+ ", orderItemId=" + orderItemId + ", price=" + price+ ", quantity=" + quantity + ", type=" + type + "]";}}
<resultMap id="orderItem"><result property="orderItemId" column="orderItemId" /><result property="orderId" column="orderId" /><result property="itemName" column="itemName" /><result property="quantity" column="quantity" /><result property="price" column="price" /><result property="type" column="type" /><discriminator column="type" javaType="java.lang.String"><subMap resultMap="Individual" value="individual" /><subMap resultMap="Group" value="group" /></discriminator></resultMap><resultMap id="Individual"extends="orderItem"><result property="username" column="username" /></resultMap><resultMap id="Group" extends="orderItem"><result property="company" column="company" /></resultMap>
package ibatis.model;public class Individual extends OrderItem {private String username;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}@Overridepublic String toString() {return super.toString() + "Individual [username=" + username + "]";}}
package ibatis.model;public class Group extends OrderItem {private String company;public String getCompany() {return company;}public void setCompany(String company) {this.company = company;}@Overridepublic String toString() {return super.toString() + "Group [company=" + company + "]";}}
<select id="getOrderItemById" resultMap="orderItem">select * from orderitem where orderitemId=1</select>
package ibatis;import java.io.IOException;import java.io.Reader;import com.ibatis.common.resources.Resources;import com.ibatis.sqlmap.client.SqlMapClient;import com.ibatis.sqlmap.client.SqlMapClientBuilder;public class DiscriminatorDemo {private static String config = "ibatis/SqlMapConfig.xml";private static Reader reader;private static SqlMapClient sqlMap;static {try {reader = Resources.getResourceAsReader(config);} catch (IOException e) {e.printStackTrace();}sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);}public static void main(String[] args) throws Exception {Object obj = sqlMap.queryForObject("OrderItem.getOrderItemById");System.out.println(obj.getClass().getName());System.out.println(obj);}}