MyBaits的HelloWorld

去官网:http://www.mybatis.org/下载需要的文件:我下载的是mybatis-3.1.0-bundle.zip,如图:

MyBaits的HelloWorld_第1张图片


新建项目:helloMybatis:这里是我的项目的结构,采用的是Oracle10g数据库。

MyBaits的HelloWorld_第2张图片


废话不说,看步骤,看代码

1:在数据库中建好一张student表,并插入一条数据:

create table student (id number(7) primary key, name varchar2(30), age number(3), sex char(2));
insert into student values(1,'james',29,'m');

2:建对应这个表的org.hello.mybatis.bean.Student类:代码:

package org.hello.mybatis.bean;

public class Student
{
    private int id;
    private String name;
    private String sex;
    private int age;

    public Student()
    {
    }

    public Student(String name, String sex, int age)
    {
	this.name = name;
	this.sex = sex;
	this.age = age;
    }

    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 int getAge()
    {
	return age;
    }

    public void setAge(int age)
    {
	this.age = age;
    }

    @Override
    public String toString()
    {
	return "id : " + id + "\tname : " + name + "\tsex : " + sex
		+ "\tage : " + age;
    }
}

3:建config.properties文件:代码:

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:phoenix
username=scott
password=tiger

4:建StudentMapper.xml文件(这里只写了一个根据ID查询的方法):代码:

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

5:建mybatis-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>
<properties resource="org/hello/mybatis/bean/config.properties"/>
	<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="org/hello/mybatis/bean/StudentMapper.xml"/>
	</mappers>
</configuration>

6:建org.hello.mybatis.bean.dao.IStudentDAO接口:代码:

package org.hello.mybatis.dao;

import org.hello.mybatis.bean.Student;

public interface IStudentDAO
{
    Student queryById(int id);
}
7:建org.hello.mybatis.bean.dao.impl.IStudentDAOImpl类:代码:

package org.hello.mybatis.dao.impl;

import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.hello.mybatis.bean.Student;
import org.hello.mybatis.dao.IStudentDAO;

public class IStudentDAOImpl implements IStudentDAO
{
    private static SqlSession sqlSession = null;
    static
    {
	String resource = "org/hello/mybatis/bean/mybatis-config.xml";
	SqlSessionFactory sqlSessionFactory = null;
	InputStream inputStream = null;
	try
	{
	    inputStream = Resources.getResourceAsStream(resource);
	    sqlSessionFactory = new SqlSessionFactoryBuilder()
		    .build(inputStream);
	    sqlSession = sqlSessionFactory.openSession();
	}
	catch (IOException e)
	{
	    e.printStackTrace();
	}
	finally
	{
	    try
	    {
		inputStream.close();
	    }
	    catch (IOException e)
	    {
		e.printStackTrace();
	    }
	}

    }

    @Override
    public Student queryById(int id)
    {
	Student student = null;
	try
	{
	    student = sqlSession.selectOne(
		    "org.hello.mybatis.bean.StudentMapper.selectStudent", id);
	}
	finally
	{
	    sqlSession.close();
	}
	return student;
    }

}
8:用Junit测试:测试类:org.hello.mybatis.dao.impl.StudentDAOTest,代码:

package org.hello.mybatis.dao.impl;

import junit.framework.TestCase;

import org.hello.mybatis.bean.Student;
import org.junit.Test;

public class StudentDAOTest extends TestCase
{
    @Test
    public void testQueryById()
    {
	Student student = new IStudentDAOImpl().queryById(1);
	System.out.println(student);
    }
}
9:运行结果:

id : 1	name : james	sex : m 	age : 29
至此,MyBaits的HelloWorld就算完了,其他高深的东东有待以后研究。




你可能感兴趣的:(数据库,String,JUnit,Class,oracle10g,encoding)