MyBatis之实例简介

晨曦之光 发布于 2012/04/25 16:16
阅读 2K+
收藏 5

MyBatis的本质是一个将SQL语句map到JAVA POJO的框架.(注意与其他ORM框架的区别,如Hibernate是数据库对象到JAVA对象的mapping).它不是用来消除SQL语句的,它主要是用来将SQL从代码中分离出来集中写在map文件中,免除在Java代码中写SQL语句时容易出现少写逗号,SQL语句格式不易读,JDBC重复的大量样板代码等问题.
下图是MyBatis的概念视图(图片来之IBatis in action)

1,对应上图的SqlMapConfig是一个configuration文件,其中配置MyBatis的设置,如datasource,transactionManager,用到的mapper文件等等.

<?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>
	<settings>
	  <setting name="cacheEnabled" value="false"/>
	  <setting name="lazyLoadingEnabled" value="false"/>
	</settings>
	<environments default="DEV">
		<environment id="DEV">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">   
			     <property name="driver" value="oracle.jdbc.driver.OracleDriver" />   
			     <property name="url" value="jdbc:oracle:thin:@dev:1521:orcl" />   
			     <property name="username" value="dev" />   
			     <property name="password" value="dev" />   
			</dataSource>
		</environment>
		<environment id="PROD">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">   
			     <property name="driver" value="oracle.jdbc.driver.OracleDriver" />   
			     <property name="url" value="jdbc:oracle:thin:@prod:1521:orcl" />   
			     <property name="username" value="prod" />   
			     <property name="password" value="prod" />   
			</dataSource>		
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/test/mybatis/mapper/MybatiscustomerMapper.xml"/>
	</mappers>
</configuration>
2,对应上图中SqlMaps的是一些SQL mapping文件,其中配置的是SQL语句(对应上图的Mapped Statement),以及这些SQL如何接收Java输入参数,如何将结果集map到Java对象等.
<?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="com.test.mybatis.mapper.MybatiscustomerMapper" >
  <resultMap id="BaseResultMap" type="com.test.mybatis.vo.Mybatiscustomer" >
    <id column="ID" property="id" jdbcType="DECIMAL" />
    <result column="NAME" property="name" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    ID, NAME
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.math.BigDecimal" >
    select 
    <include refid="Base_Column_List" />
    from MYBATISCUSTOMER
    where ID = #{id,jdbcType=DECIMAL}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.math.BigDecimal" >
    delete from MYBATISCUSTOMER
    where ID = #{id,jdbcType=DECIMAL}
  </delete>
  <insert id="insert" parameterType="com.test.mybatis.vo.Mybatiscustomer" >
    insert into MYBATISCUSTOMER (ID, NAME)
    values (#{id,jdbcType=DECIMAL}, #{name,jdbcType=VARCHAR})
  </insert> 
  <update id="updateByPrimaryKey" parameterType="com.test.mybatis.vo.Mybatiscustomer" >
    update MYBATISCUSTOMER
    set NAME = #{name,jdbcType=VARCHAR}
    where ID = #{id,jdbcType=DECIMAL}
  </update>
</mapper>
这样,MyBatis的配置就完成了,接着就是Java POJO对象了,这些都可以通过工具自动生成:

public class Mybatiscustomer {
    private BigDecimal id;
    private String name;

public interface MybatiscustomerMapper {

    int deleteByPrimaryKey(BigDecimal id);

    int insert(Mybatiscustomer record);

    Mybatiscustomer selectByPrimaryKey(BigDecimal id);

    int updateByPrimaryKey(Mybatiscustomer record);
}
客户端调用代码:#1从classpath下读取SqlMapConfig文件,#2这里会由MyBatis返回一个proxy对象,然后根据方法名对应到SqlMaps映射文件中的SQL,然后执行SQL.
	public static void main(String[] args) throws Exception {		
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("Configuration.xml"));#1	
		SqlSession sqlSession = factory.openSession();
		MybatiscustomerMapper mapper = sqlSession.getMapper(MybatiscustomerMapper.class); #2	
		Mybatiscustomer customer = new Mybatiscustomer();
		customer.setId(new BigDecimal(3));
		customer.setName("name3");		
		mapper.insert(customer);
		sqlSession.commit();
		sqlSession.close();
	}
SqlSession提供的API:
<T> T selectOne(String statement, Object parameter)
<E> List<E> selectList(String statement, Object parameter)
<K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey)
int insert(String statement, Object parameter)
int update(String statement, Object parameter)
int delete(String statement, Object parameter)

<T> T selectOne(String statement)
<E> List<E> selectList(String statement)
<K,V> Map<K,V> selectMap(String statement, String mapKey)
int insert(String statement)
int update(String statement)
int delete(String statement)

<E> List<E> selectList (String statement, Object parameter, RowBounds rowBounds)
<K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowbounds)
void select (String statement, Object parameter, ResultHandler handler)
void select (String statement, Object parameter, RowBounds rowBounds, ResultHandler handler)
MyBatis 主页 http://www.mybatis.org/index.html



原文链接:http://blog.csdn.net/kkdelta/article/details/7429232
加载中
0
乔康007
乔康007

这个图不错,有助于理解!

非常感谢~~

返回顶部
顶部