简单入门案例
Ibatis 入门
1.创建数据库脚本
?drop database sample;
?create database sample;
?use sample;
?drop table t_user;
?create table t_user(
??id int not null auto_increment,
??name varchar(50) default null,
??sex int(11) default null,
??primary key (id)
?);
?insert into t_user values (1,'1',1),(2,'2',2),(3,'3',3),(4,'4',4),(5,'5',5);
2.编写Ibaits必须的配置文件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 cacheModelsEnabled="true" enhancementEnabled="true"
???lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
???maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />
??<transactionManager type="JDBC">
???<dataSource type="SIMPLE">
????<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
????<property name="JDBC.ConnectionURL"
?????value="jdbc:mysql://localhost/sample" />
????<property name="JDBC.Username" value="root" />
????<property name="JDBC.Password" value="mysqladmin" />
????<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 sample" />
????<property name="Pool.PingEnabled" value="false" />
????<property name="Pool.PingConnectionsOlderThan" value="1" />
????<property name="Pool.PingConnectionsNotUsedFor" value="1" />
???</dataSource>
??</transactionManager>
??<sqlMap resource="com/ctgusec/zhupan/maps/User.xml" />
?</sqlMapConfig>
3.编写实体类
?package com.ctgusec.zhupan.model;
?import java.io.Serializable;
?public class User implements Serializable {
??private static final long serialVersionUID = 1L;
??private Integer id;
??private String name;
??private Integer sex;
??.
??.
??.
4.在编写数据库表到实体类的映射
?<?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">
??<typeAlias alias="user" type="com.ctgusec.zhupan.model.User" />
??<select id="getUser" parameterresultresultparameterparameterparameterClass="java.lang.Integer">
???<![CDATA[delete from t_user where id=#value#]]>
??</delete>
?</sqlMap>
5.测试
?package com.ctgusec.zhupan;
?import java.io.Reader;
?import java.sql.SQLException;
?import java.util.List;
?import com.ctgusec.zhupan.model.User;
?import com.ibatis.common.resources.Resources;
?import com.ibatis.sqlmap.client.SqlMapClient;
?import com.ibatis.sqlmap.client.SqlMapClientBuilder;
?public class ExampleMain {
??//删除的操作
??public static boolean delete() {
???boolean flag = true;
???String resource = "com/ctgusec/zhupan/maps/SqlMapConfig.xml";
???SqlMapClient sqlMap = null;
???try {
????Reader reader = Resources.getResourceAsReader(resource);
????sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
???} catch (Exception e) {
????flag = false;
????e.printStackTrace();
???}
???try {
????sqlMap.startTransaction();
????sqlMap.delete("deleteUser",new Integer(7));
????sqlMap.commitTransaction();
???} catch (Exception e) {
????flag = false;
????e.printStackTrace();
???} finally {
????try {
?????sqlMap.endTransaction();
????} catch (SQLException e) {
?????flag = false;
?????e.printStackTrace();
????}
???}
???return flag;
??}
??//增加的操作
??public static boolean add() {
???boolean flag = true;
???String resource = "com/ctgusec/zhupan/maps/SqlMapConfig.xml";
???SqlMapClient sqlMap = null;
???try {
????Reader reader = Resources.getResourceAsReader(resource);
????sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
???} catch (Exception e) {
????flag = false;
????e.printStackTrace();
???}
???try {
????sqlMap.startTransaction();
????User user = new User();
????user.setName("xc");
????user.setSex(new Integer(1));
????sqlMap.insert("insertUser", user);
????sqlMap.commitTransaction();
???} catch (Exception e) {
????flag = false;
????e.printStackTrace();
???} finally {
????try {
?????sqlMap.endTransaction();
????} catch (SQLException e) {
?????flag = false;
?????e.printStackTrace();
????}
???}
???return flag;
??}
??//修改的操作
??public static void update() {
???// 首先初始化iBatis获得一个SqlMapClient对象
???String resource = "com/ctgusec/zhupan/maps/SqlMapConfig.xml";
???com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
???try {
????java.io.Reader reader = com.ibatis.common.resources.Resources
??????.getResourceAsReader(resource);
????sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
???} catch (Exception e) {
????e.printStackTrace();
???}
???// sqlMap系统初始化完毕,开始执行update操作
???try {
????sqlMap.startTransaction();
????User user = new User();
????user.setId(new Integer(1));
????user.setName("zhangsan");
????user.setSex(new Integer(1));
????sqlMap.update("updateUser", user);
????sqlMap.commitTransaction();
???} catch (SQLException e) {
????System.out.println(e.getMessage());
???} finally {
????try {
?????sqlMap.endTransaction();
????} catch (SQLException e) {
?????e.printStackTrace();
????}
???}
??}
??//查询的操作
??public static List getUser() {
???// 首先初始化iBatis获得一个SqlMapClient对象
???String resource = "com/ctgusec/zhupan/maps/SqlMapConfig.xml";
???com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
???List user = null;
???try {
????java.io.Reader reader = com.ibatis.common.resources.Resources
??????.getResourceAsReader(resource);
????sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
???} catch (Exception e) {
????e.printStackTrace();
???}
???// sqlMap系统初始化完毕,开始执行getAllUser操作
???try {
????sqlMap.startTransaction();
????user = sqlMap.queryForList("getAllUser", null);
????sqlMap.commitTransaction();
???} catch (SQLException e) {
????System.out.println(e.getMessage());
???} finally {
????try {
?????sqlMap.endTransaction();
????} catch (SQLException e) {
?????e.printStackTrace();
????}
???}
???return user;
??}
??public static void main(String[] args) {
?//??System.out.println(delete());
???// System.out.println(add());
???// System.out.println("testtest");
???// update();
???// List user=getUser();
???// for(int i=0;i<user.size();i++)
???// {
???// System.out.print(((User)user.get(i)).getName()+"、");
???// }
??}
?}
6.其中所使用的包
?commons-dbcp.jar
?commons-logging.jar
?commons-loggin-api.jar
?commons-pool.jar
?hsqldb.jar
?ibatis-common-2.jar
?ibatis-dao-2.jar
?ibatis-sqlmap-2.jar
?mysql-connector-java-3.1.12-bin.jar