首页 > 代码库 > Mybatis的搭建与使用

Mybatis的搭建与使用

Hibernate是非常完美的一个持久性框架,但是由于其考虑到各种功能和通用性,导致这个框架太大了,另外就是封装的太好了导致自主性比较小。所以Mybatis受到很多公司的欢迎。Mybatis以前叫做ibatis,用法稍微有点区别,在这里我用的是Mybatis 3

1.去官网下载Mybatis的Jar包 http://mybatis.github.io/

2.配置SqlMapConfig.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>
	<!-- 给bean取别名用在mapping文件中 -->
	<typeAliases>
		<typeAlias type="com.lubby.bean.Worker" alias="Worker" />
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<!-- 使用JDBC的事务管理 -->
			<transactionManager type="JDBC" />
			<!-- 数据源 -->
			<dataSource type="POOLED">
				<property name="driver" value=http://www.mamicode.com/"org.postgresql.Driver" />>
3.worker bean

package com.lubby.bean;
public class Worker {
	private String id;
	private String name;
	int age;
	public Worker() {
		super();
	}
	public Worker(String id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Worker [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

3.bean的mapping配置文件  WorkerSqlMap.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">

<!-- namespace用于java代码调用时识别指定xml的mapper文件 -->
<mapper namespace="com.lubby.bean">
	<sql id="field">
		id,name,age
	</sql>
	<select id="getAllWorker" resultType="Worker">
		select
		<include refid="field" />
		from worker
	</select>
	<select id="getWorkerById" resultType="Worker">
		select
		<include refid="field" />
		from worker where
		id= #{id}
	</select>
	<select id="getWorkerByName" resultType="Worker">
		select
		<include refid="field" />
		from worker where name like '%'||#{name}||'%'
	</select>

	<insert id="addWorker">
		insert into worker (
		<include refid="field" />
		) values(#{id},#{name},#{age})
	</insert>

	<update id="updateWorker">
		update worker set name=#{name} ,age=#{age} where
		id=#{id}
	</update>

	<delete id="deleteWorkerById">
	delete from worker where id=#{id};
	</delete>
</mapper>
4.在主函数中使用mybatis

package com.lubby.test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

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 com.lubby.bean.Worker;

public class Test {
	public static void main(String[] args) {
		SqlSessionFactory factory = null;
		try {
			Reader reader = Resources.getResourceAsReader("com/lubby/config/SqlMapConfig.xml");
			factory = new SqlSessionFactoryBuilder().build(reader);

		} catch (IOException e) {
			e.printStackTrace();
		}

		SqlSession session = factory.openSession();

		Worker worker = new Worker("003", "Join", 100);
		//session.insert("com.lubby.bean.addWorker", worker);
		//session.delete("com.lubby.bean.deleteWorkerById", "005");
		//session.commit();
		List<Worker> list = session.selectList("com.lubby.bean.getAllWorker");
		System.out.println(session.selectOne("com.lubby.bean.getWorkerById", "001"));
		System.out.println(session.update("com.lubby.bean.updateWorker", worker));
		System.out.println(session.delete("com.lubby.bean.deleteWorkerById", "004"));
		session.commit();
		System.out.println(session.selectList("com.lubby.bean.getWorkerByName", "J"));
		System.out.println(list);
	}

}