MyBitis(iBitis)系列随笔之一:MyBitis入门实例

 

MyBitis(iBitis)系列随笔之一:MyBitis入门实例

MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM)

MyBitis(iBitis)系列随笔之三:实现简单的增删改查

MyBitis(iBitis)系列随笔之四:多表(多对一查询操作)

MyBitis(iBitis)系列随笔之五:多表(一对多关联查询)

MyBitis(iBitis)系列随笔之六:mybitis与spring集成

MyBits前身是iBitis,相对于Hibernate而言,它是半自动化ORM框架。本着分享和学习的目的,笔者将陆续把学习笔记与代码贴出,希望对想学习mybitis的同窗们有所帮助。
       鉴于我的测试数据库是mysql,同仁们现在把mysql装好,然后准备好mysql驱动以及从官网上下载mybitis(
http://code.google.com/p/mybatis/)。我下载的是最新的mybatis-3.2.2.zip。

   

      创建数据库test

       然后创建表t_user

DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `pass` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=gbk;

INSERT INTO `t_user` VALUES ('1', 'ssssssssssssssss', 'ddsssssssdd');


     然后创建与表对应的Java bean对象

 

package com.jefry;

public class User {
	private int id;
	private String name;
	private String pass;
	
	

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	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;
	}

}


       接下来,在classpath目录下面创建与配置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>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
				<property name="username" value="root"/>
				<property name="password" value="root"/>
			</dataSource>
		</environment>
	</environments>
	<mappers>
     <!--这个文件下面将创建-->
     <mapper resource="com/jefry/UserMapper.xml"/>
	</mappers>
</configuration>


        通过mybatis-config.xml中<mapper resource="com/jefry/UserMapper.xml"/>配置项,我们在包com.jefry下面创建文件UserMapper.xml
         内容如下:

<?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="User">
	<!--resultType 表示com.jefry.User这样一个实体对象 -->
	<select id="selectUser" parameterType="int" resultType="com.jefry.User">
        select * from t_user where id = #{id}
	</select>
</mapper>


     编写测试代码

package com.jefry;

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;

public class Test {
	static String resource = "mybatis-config.xml";

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = sqlSessionFactory.openSession();
		try {
			//User.selectUser中User对应UserMapper.xml文件中标签<mapper namespace="User"> namespace的值
			//selectUser 对应UserMapper.xml文件中标签<select id="selectUser" parameterType="int" resultType="com.jefry.User">中id值
			User user = session.selectOne("User.selectUser", 1);
			System.out.println("user.getName()=" +  user.getName());
		} finally {
			session.close();
		}
	}

}


通过运行就可以结果:user.getName()=ssssssssssssssss

 

源码下载:http://download.csdn.net/detail/jefry_xdz/5217151

你可能感兴趣的:(MyBitis(iBitis)系列随笔之一:MyBitis入门实例)