种_试_测_的_ibatis

类_试_测_的_ibatis
package com.ibatis.example;

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;
public class Test
{
    private static SqlMapClient sqlMap = null;
    public static void main(String[] args) throws SQLException
    {
        buildConfig();//加载配置
        getAll();//查询所有数据
        //getById(1); //根据Id查询
        //add();//添加对象
        //del(1);//根据主键删除
        //update();//修改
    }
    public static void buildConfig()//加载配置
    {
        try
        {
            //配置文件的位置:默认在src目录下,如果存放其他位置,就加上相应的包名如:"com.ibatis.example.sqlMapConfig.xml"
            String resource = "sqlMapConfig.xml"; //$NON-NLS-1$
            Reader reader = Resources.getResourceAsReader(resource);
            sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    public static void getById(int key) throws SQLException//根据Id查询
    {
        UserInfo userInfo = null;
        Object object = sqlMap.queryForObject("getUserInfo", key);
        {
            userInfo = (UserInfo)object;
        }
        System.out.println("查询到对象:" + userInfo);
    }
    public static void getAll() throws SQLException //查询所有UserInfo
    {
        sqlMap.startTransaction();
        List<?> list = sqlMap.queryForList("getAllUserInfo", null);
        System.out.println(list.size());
        sqlMap.commitTransaction();
    }
    public static void login() throws SQLException //多条件查询,方式一
    {
        String sql = "uAddress='a' and uSex='a'";
        List queryForList = sqlMap.queryForList("login", sql);
        for (int i = 0; i < queryForList.size(); i++)
        {
            System.out.println(queryForList.get(i));
        }
    }
    public static void login2() throws SQLException //多条件查询,方式二(建议用)
    {
        UserInfo userInfo = new UserInfo();
        Map map = new HashMap();
        map.put("name", "a");
        map.put("id", 3);
        //List queryForList = sqlMap.queryForList("login2", map);//返回多个结果
        Object obj = sqlMap.queryForObject("login2", map, userInfo);//返回单个结果
        System.out.println(obj);
    }
    public static void add() throws SQLException //添加
    {
        sqlMap.startTransaction();
        UserInfo userInfo = new UserInfo(9, "a", "a", "a");
        Object insert = sqlMap.insert("addUserInfo", userInfo);
        System.out.println(insert);
        sqlMap.commitTransaction();
    }
    public static void del(int key) throws SQLException //删除
    {
        sqlMap.startTransaction();
        Object del = sqlMap.delete("delUserInfo", key);
        int parseInt = Integer.parseInt(del.toString());
        sqlMap.commitTransaction();
        if (parseInt > 0)
        {
            System.out.println("删除成功");
        }
    }
    public static void update() throws SQLException //修改
    {
        sqlMap.startTransaction();//开始事务
        UserInfo userInfo = new UserInfo(2, "h", "h", "h");
        int update = sqlMap.update("updUserInfo", userInfo);
        sqlMap.commitTransaction();
        System.out.println(update);
    }
}