ibatis学习

来源:互联网 发布:淘宝怎么投诉快递 编辑:程序博客网 时间:2024/06/11 07:52

 

1.ibatis配置详解

http://imticg.javaeye.com/blog/216080

http://lyb520320.javaeye.com/blog/586628

http://jc-dreaming.javaeye.com/blog/654034

首先,要有一个SqlMapConfig.xml(名字可以自己去)的文件。这个文件是用来配置连接数据库参数用的。如下所示:

 

<?xml version="1.0" encoding="UTF-8"?>  

<!DOCTYPE configuration  

PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"  

"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">  

<configuration>  

    <properties resource="jdbc.properties"/>  

    <typeAliases>  

        <typeAlias type="com.ibatis.xml.Person" alias="Person"/>  

    </typeAliases>  

    <environments default="development">  

        <environment id="development">  

            <transactionManager type="JDBC"/>  

            <dataSource type="POOLED">  

                <property name="driver" value="${driver}"/>  

                <property name="url" value="${url}"/>  

                <property name="username" value="${username}"/>  

                <property name="password" value="${password}"/>  

            </dataSource>  

        </environment>  

    </environments>  

    <mappers>  

        <mapper resource="com/ibatis/xml/person.xml"/>  

    </mappers>  

</configuration> 

其中,jdbc.properties是配置文件,内容如下所示:

 

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/ticket_system?useUnicode=true&characterEncoding=GBK

username=root

password=123456

 

 

然后,需要一个person.xml(名字自己取)的文件。此文件是写数据库操作语句的。如下所示:

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper  

PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"  

"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="com.ibatis.personOperation">  

    <select id="selectPerson" parameterType="java.lang.Integer"  

        resultType="Person">  

        select * from person where id = #{id}  

    </select>  

</mapper>  

创建一个SessionFactory的java文件,此文件用于创建连接数据库对象,最好使用单例模式。

 

package com.ibatis.xml;

 

import java.io.IOException;

import java.io.Reader;

 

import org.apache.ibatis.io.Resources;  

import org.apache.ibatis.session.SqlSessionFactory;  

import org.apache.ibatis.session.SqlSessionFactoryBuilder; 

 

public final class SessionFactory {

private String resource="com/ibatis/xml/SqlMapConfig.xml";  

    private SqlSessionFactory sqlSessionFactory=null;  

    private static SessionFactory sessionFactory=new SessionFactory();  

 

    private SessionFactory() {  

        try {  

        System.out.println(resource);

            Reader reader=Resources.getResourceAsReader(resource);  

            sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);  

        } catch (IOException e) {  

            System.out.println("#IOException happened in initialising the SessionFactory:"+e.getMessage());  

            throw new ExceptionInInitializerError(e);  

        }  

    }  

    public static SessionFactory getInstance() {  

        return sessionFactory;  

    }  

    public SqlSessionFactory getSqlSessionFactory() {  

        return sqlSessionFactory;  

    }

}

还需要创建一个person类。如下所示:
public class Person {
private int id = 0;  
    private String name = "";  
    private String sex = "male";  
    private Date birthday = null;  
  
    public Person() {  
  
    }  
  
    public int getId() {  
        return id;  
    }  
  
    public void setId(int id) {  
        this.id = id;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public String getSex() {  
        return sex;  
    }  
  
    public void setSex(String sex) {  
        this.sex = sex;  
    }  
  
    public Date getBirthday() {  
        return birthday;  
    }  
  
    public void setBirthday(Date birthday) {  
        this.birthday = birthday;  
    }  
  
    public String toString() {  
        return "id=" + id + "/t" + "name=" + name + "/t" + "sex=" + sex + "/t"  
                + "birthday=" + new java.sql.Date(birthday.getTime()).toString();  
    }  
  
}

接着按照person.xml中的内容创建一个接口

public interface PersonMapper {

Person selectById(Integer id);  

}

并实现这个接口:

public class PersonDao implements PersonMapper {

private SqlSessionFactory sessionFactory = SessionFactory.getInstance()  

    .getSqlSessionFactory();  

 

public Person selectById(Integer id) {  

Person person = new Person();  

SqlSession session = null;  

try {  

    session = sessionFactory.openSession();  

    person = (Person) session.selectOne(  

            "com.ibatis.personOperation", id);  

} finally {  

    session.close();  

}  

return person;  

}

}

 

然后,执行数据库操作时创建PersonDao对象,并使用它的方法来执行数据库操作。

 

原创粉丝点击