关闭

Java使用Mybatis

135人阅读 评论(0) 收藏 举报

Java 和 Mybatis

1、准备“sqlite-jdbc-3.7.2.jar”和“mybatis-3.4.1.jar”

2、准备开发环境
Windows 7、MyEclipse 8.6、jdk1.7

3、在MyEclipse上新建java Project工程,目录结构如下:

mybatisDemo
    src
        com.jasper
           bean
               Person.java
               PersonMapper.xml
           mybatis
               TestMybatis.java
        config.xml
    lib
        mybatis-3.4.1.jar
        sqlite-jdbc-3.7.2.jar

在工程mybatisDemo的bin目录放入数据库文件sqlite.database

Person.java Java Bean
PersonMapper.xml O/R Mapping(对象/关系对应关系)
TestMybatis.java 测试案例
config.xml Mybatis的配置文件

4、准备数据
使用示例“SQLite嵌入式数据库”向sqlite.database中创建表person(id integer, name string),并插入数据
id name
1 ‘aaa’
2 ‘bbb’
3 ‘ccc’

5、准备bean和mapping xml
Person.java

package com.jasper.bean;

public class Person {
    private int id;
    private String name;

    public void setName( String name ) {
        this.name = name;
    }

    public String getName( ) {
        return name;
    }

    public void setId( int id ) {
        this.id = id;
    }

    public int getId( ) {
        return id;
    }

    @Override
    public String toString( ) {
        return "id="+id+",name="+name;
    }
}



PersonMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/mybatis-3-mapper.dtd">
<mapper namespace="com.jasper.bean.Person">
    <select id="getPerson" parameterType="int" resultType="com.jasper.bean.Person">
        select *
        from person where id=#{id}
    </select>
</mapper>

namespace随便写,只要不重复即可。


6、config.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="org.sqlite.JDBC"/>
                <property name="url" value="jdbc:sqlite:sqlite.database"/>
<!--                <property name="username" value=""/>-->
<!--                <property name="password" value=""/>-->
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/jasper/bean/PersonMapper.xml"/>
    </mappers>
</configuration>

因为sqlite没有密码(有密码的是商业版,要钱的),所以此处没有设置密码。


7、测试样例

package com.jasper.mybatis;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.jasper.bean.Person;

public class TestMybatis {

    public static void main( String[] args ) {
        InputStream is = TestMybatis.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = sessionFactory.openSession();
        String statement = "com.jasper.bean.Person.getPerson";
        Person person = (Person) session.selectOne(statement, new Integer(1));
        System.out.println(person);
                  session.close();
    }

}

statement 命名空间+id


8、测试执行结果
id=1,name=aaa

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20594次
    • 积分:502
    • 等级:
    • 排名:千里之外
    • 原创:31篇
    • 转载:4篇
    • 译文:0篇
    • 评论:4条
    最新评论