iBatis初级入门案例

阅读更多

小弟今天在Javaeye上面碰到一个好文章,是iBatis的入门,上面讲了一个小例子,觉得不错,不过代码里面有部分错,已纠正!


我用的是MyEclipse8.5+MySQL;

第一步是建数据库

create database if not exists `sample`;
USE `sample`; 
drop   table   if   exists  `t_user`;

CREATE   TABLE  `t_user` (
  `id`  int  ( 11 )  NOT   NULL  auto_increment,
  ` name `  varchar  ( 50 )  default   NULL  ,
  `sex`  int  ( 11 )  default   NULL  ,
   PRIMARY   KEY    (`id`)
) ENGINE = InnoDB  DEFAULT  CHARSET = latin1;

insert   into  `t_user`  values  ( 1 ,  ' dagmom'  , 1 ),( 2 ,  ' guhao '  , 2 ),( 3 ,  ' 3 '  , 3 ),( 4 ,  ' 4 '  , 4 ),( 5 ,  ' 5 '  , 5 ); 
 


第二步是准备所需要的jar包

下面是我写的树形结构图


iBatis初级入门案例_第1张图片
 

3,接下来开始配置 SqlMapConfig.xml文件


 


 



    

        

        

            

                

                

                

                

                

                

                

                

                

                

                

                

            

        

    

 
 

4,当然这里我么还需要写一个实体类,一边做OR Mapping映射

package com.dagmom.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;
    public User() {
    }
    public Integer getId() {
        return this.id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getSex() {
        return this.sex;
    }
    public void setSex(Integer sex) {
        this.sex = sex;
    }
}
 

5,和hibernate一样,iBatis也需要配置文件










    INSERT INTO t_user ( name, sex) VALUES ( #name#, #sex# )


    delete from t_user where id=#value#

 
 
这个比hibernate好的地方就是,他的sql语句是写在配置文件里面的,而且是原始的sql语句不想hql那样;

6,剩下就我们就是要写一个测试类

package com.dagmom;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.dagmom.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 void update(){
        //首先初始化iBatis获得一个SqlMapClient对象
        String  resource = "com/dagmom/maps/SqlMapConfig.xml";
        SqlMapClient sqlMap = null;
        try{
            Reader reader = Resources.getResourceAsReader(resource);
            sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
        }catch(Exception e){
            e.printStackTrace();
        }

        //sqlMao系统初始化完毕,开始执行update操作
        try{
            sqlMap.startTransaction();
            User user = new User();
            user.setId(new Integer(1));
            user.setName("guhao");
            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/dagmom/maps/SqlMapConfig.xml";
        SqlMapClient sqlMap = null;
        List user = null;
        try{
            Reader reader = 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.print("x");
        update();
        List user = getUser();
        System.out.println(user);
    }
} 
 
这个程序很简单,而且大家对iBatis应该有入门级的了解了,以后就容易了!


 

 

  • iBatis初级入门案例_第2张图片
  • 大小: 37.6 KB
  • 查看图片附件

你可能感兴趣的:(ibatis,java,mysql,入门教程)