Rumah  >  Artikel  >  pangkalan data  >  MyBatis简介

MyBatis简介

WBOY
WBOYasal
2016-06-07 15:54:421301semak imbas

1.什么是MyBatis MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Obje

1.什么是MyBatis

MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java 对象)映射成数据库中的记录。(类似Hibernate)

2.从XML中构建 SqlSessionFactory

每一个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心。SqlSessionFactory 对象的实例可以通过 SqlSessionFactoryBuilder对象来 获 得 SqlSessionFactoryBuilder 对象可以从 XML 配置文件,或从 Configuration 类的实 例中构建 SqlSessionFactory 对象。

String resource = "domain/configuration.xml"; //配置文件的位置
Reader reader =Resources.getResourceAsReader(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(reader);

3. 从 SqlSessionFactory 中获取 SqlSession

SqlSession 对象完全包含以数据库为背景的所有执行 SQL 操作的 方法。你可以用 SqlSession 实例来直接执行已映射的 SQL 语句。

SqlSession session = sqlSessionFactory.openSession();
try {
	Blog blog = (Blog)session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
}
finally {
	session.close();
}

4.Mapper XML 文件

4.1 select

<select id="selectPerson" parameterType="int" resultType="hashmap">
  	 SELECT * FROM PERSON WHERE ID = #{id}
</select>

Id是select的标识,与Mapper.java中的函数名称一致完成映射。Hashmap代表返回值resultType是,其中key是指列名。#{id}是指参数,和Mapper.java中参数同名。

参数也可以指定数据类型#{id,javaType=int,jdbcType=NUMERIC}

类似于java完成以下代码:// Similar JDBC code, NOT MyBatis…

String selectPerson = "SELECT * FROM PERSON WHERE ID=?";
PreparedStatement ps = conn.prepareStatement(selectPerson);
ps.setInt(1,id);

4.2 insert, update, delete

<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
	insert into Author (username,password,email,bio) values (#{username},#{password},#{email},#{bio})
</insert>

Update,delete用法类似,还有parameterType属性等。

4.3 resultMap

要记住类型别名是你的伙伴。使用它们你可以不用输入类的全路径。比如:

<!-- In mybatis-config.xml file -->
<typeAlias type="com.someapp.model.User" alias="User"/>
使用时:
<select id="selectUsers" resultType="com.someapp.model.User">
等同于: <select id="selectUsers" resultType="User">

Eg: type属性是指实体类,一般要把

resultMap用来解决映射类的属性与数据库字段不一致的情况。

<resultMap type="Address" id="addressMap">
	<id column="id" property="id"/>
	<result column="postcode" property="postcode" jdbcType=&rdquo;varchar&rdquo;/>
</resultMap>
<select id="selectById" parameterType="int" resultMap="addressMap">
	select * from t_address where id=#{id}
</select>

4.4 sql

这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中。比如:

<sql id="userColumns"> id,username,password </sql>

这个 SQL 片段可以被包含在其他语句中,例如:

<select id="selectUsers" resultType="map">  
	select  <include refid="userColumns"/>  from some_table
	where id = #{id}
</select>

用include元素来引用,refid属性的值必须是某个sql元素的id.

5.动态SQL

5.1 if

在动态 SQL 中所做的最通用的事情是包含部分 where 字句的条件。比如:

<select id="findActiveBlogLike" resultType="Blog"> 
	SELECT * FROM BLOG WHERE state = &lsquo;ACTIVE&rsquo; 
	<if test="title != null">
		AND title like #{title}
	</if>
	<if test="author != null and author.name != null">
		AND author_name like #{author.name}
	</if>
</select>

理解时注意:其中的参数title、author都是从函数中传过来的值。不属于数据库中字段值。所以需要test判断是否为空。

5.2 choose when otherwise

相当于java语言中的switch……case语句,只需要满足一个条件:

<select id="findActiveBlogLike" resultType="Blog">
	SELECT * FROM BLOG WHERE state = &lsquo;ACTIVE&rsquo;
	<choose>
		<when test="title != null">
			AND title like #{title}
		</when>
		<when test="author != null and author.name != null">
			AND author_name like #{author.name}
		</when>
		<otherwise>
			AND featured = 1
		</otherwise>
	</choose>
</select>

如果title、author二者都没提供,只返回 featured blogs(也许是由管理员策略地选 择的结果列表,而不是返回大量没有意义的随机博客结果列表)。

5.3 trim where set

为了变面下面代码这种情况,定义了where,set

<select id="findActiveBlogLike" resultType="Blog">
	SELECT * FROM BLOG WHERE 
	<if test="state != null">
		state = #{state}
	</if> 
	<if test="title != null">
		AND title like #{title}
	</if>
	<if test="author != null and author.name != null">
		AND author_name like #{author.name}
	</if>
</select>

上面那代码会因为其中某些条件不满足导致下面的sql代码的出现:

SELECT * FROM BLOG WHERE
SELECT * FROM BLOG WHERE   AND title like &lsquo;someTitle&rsquo;

导致查询失败,为了避免这种情况,大多使用where元素:

<select id="findActiveBlogLike" resultType="Blog">
	SELECT * FROM BLOG 
	<where> 
		<if test="state != null">
			state = #{state}
		</if> 
		<if test="title != null">
			AND title like #{title}
		</if>
		<if test="author != null and author.name != null">
			AND author_name like #{author.name}
		</if>
	</where>
</select>

Where碰到and开头会自动过滤掉and.动态更新语句相似的解决方案是 set。set 元素可以被用于动态包含更新的列,而不包含不需更新的,可以自动过滤掉句末的符号。

<update id="updateAuthorIfNecessary">
	update Author
	<set>
		<if test="username != null">username=#{username},</if>
		<if test="password != null">password=#{password},</if>
		<if test="email != null">email=#{email},</if>
		<if test="bio != null">bio=#{bio}</if>
	</set>
	where id=#{id}
</update>

不管是where元素和set元素遇到的问题都可以用trim来解决:

<trim prefix="WHERE" prefixOverrides="AND |OR ">  ... 	 </trim>
<trim prefix="SET" suffixOverrides=",">  ... 	 </trim>
prefixOverriders、suffixOverriders分别表示前后缀的过滤

5.4 foreach

迭代用法,一般用在in条件中。

<select id="selectPostIn" resultType="domain.blog.Post">
	SELECT * FROM POST P WHERE ID in
	<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
		#{item}//会组合成(1,2,3.....)
	</foreach>
</select>

foreach 元素是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可 以用在元素体内。它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素 是很智能的,它不会偶然地附加多余的分隔符。

关于collection:参考http://my.oschina.net/linuxred/blog/38802

list、array都是list、array

如果是复杂类型,map {, };那么collection指向想要的值的key,此列中collection=”id”

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn