Mybatis学习(涵盖所有内容)

目录

MyBatis 学习笔记    2

1.MyBatis 本是apache的一个开源项目iBatis ,后来迁移到Github    2

2.然后就专注于结果集的返回    3

3.别名    3

resultMap 的配置是一个关键点    5

5. 还有一个强大的功能是 动态sql语句    5

5.1    5

5.2 在动态update语句里相似的解决方式叫做set,这个set元素能够动态地更新列。例如:    6

5.3Foreach 元素    6

5.4.1先写一个DEMO 测试一下    7

5.4.2.定义接口    8

5.4.3.实现 Mapper文件    9

5.4.4 配置文件    10

5.4.5测试结果    12

5.4.6 高级映射分两种 一种是映射写select语句, 一种是映射写连接查询    13

5.4.7测试结果 为(不尽人意,感觉Mybatis好不智能,虽然方便)    15

5.4.8 解决办法    16

5.4.9尴尬,也没爆Sql 1+n问题怕是只是n+1才能了    19

5.5动态sql    20

6.1以下算是高级应用,Mybatis调优化    21

6.2关于openSession()    22

6.3 RowBounds 用来分页    22

6.4 结果处理器    22

6.5 控制事物    23

6.6 mapper两种方式, annotation或者XML XML主流都用XML    23

6.7 org.apache.ibatis.jdbc.SqlBuilder;    23

 

 

 

MyBatis 学习笔记

1.

MyBatis 本是apache的一个开源项目iBatis ,后来迁移到Github

 

  • MyBatis的优势
    • 1. Mybatis直接使用xml配置sql语句,查询自动将结果封装成实体。
    • 2. SQL命令声明在XML配置文件中,便于维护。
    • 3. 有一级和二级缓存机制(需要在配置文件中打开)
    • 4. MyBatis提供级联查询。

 

直接写接口定义方法,XXXXXXXMapper.xml 映射成dao层实现

也可以在接口中写注解方式,但是不利于复杂sql语句的编写

public interface BlogMapper {

@Select("SELECT * FROM blog WHERE id = #{id}")

Blog selectBlog(int id);

}

创建方式与hibernate 一样

SqlSessionFactoryBuilder 解析XML

SqlSessionFactory 创建sqlSession

SqlSession 现成不安全 封装connnection preparestatment 控制Transaction

 

事物管理

2.然后就专注于结果集的返回

<select id= " selectPerson" " parameterType= " int" " resultType= " hashmap "> >

SELECT * FROM PERSON WHERE ID = #{id}

</select>

注意这个参数的表示方法: #{id}

它告诉MyBatis 生成 PreparedStatement 参数。对于 JDBC,像这个参数会被标识为"?",

然后传递给PreparedStatement

 

Sql 元素

这个元素用来定义能够被其它语句引用的可重用SQL 语句块。例如:

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

这个 SQL 语句块能够被其它语句引用,如:

<select id="selectUsers" parameterType="int" resultType="hashmap">

select <include refid="userColumns"/>

from some_table

where id = #{id}

</select>

3.别名

这样的JavaBean 可以像HashMap 一样简单地映射到 ResultSet 结果集。

<select id="selectUsers" parameterType="int"

resultType=" com.someapp.model.User">

select id, username, hashedPassword

from some_table

where id = #{id}

</sql>

别忘了别名是您的朋友,使用别名您不用输入那长长的类路径。例如:

<!-- In Config XML file -->

<typeAlias type=" com.someapp.model.User" alias=" User"/>

<!-- In SQL Mapping XML file -->

<select id="selectUsers" parameterType="int"

resultType=" User">

select id, username, hashedPassword

from some_table

where id = #{id}

</sql>

 

4.ResultMap 属性详解

 

 

 

Mybatis也有1+N 问题, 环也有1+N问题,等等 hibernate也有1+N问题

 

联合嵌套结果集(Nested Results for Association) 是MyBatis3的解决方案

<select id="selectBlog" parameterType="int" resultMap="blogResult">

select

B.id as blog_id,

B.title as blog_title,

B.author_id as blog_author_id,

A.id as author_id,

A.username as author_username,

A.password as author_password,

A.email as author_email,

A.bio as author_bio

from Blog B t left r outer n join r Author A A n on d B.author_id = = A.id

where B.id = #{id}

</select>

注意到这个连接(join),要确保所有的别名都是唯一且无歧义的。这使映射容易多了,现

在我们来映射结果集:

p <resultMap " id="blogResult" type="Blog">

d <id property= " id" " " column=" " blog_id" />

<result property="title" column="blog_title"/>

<association property="author" column="blog_author_id" javaType="Author"

resultMap= " authorResult " />

</resultMap>

p <resultMap " id="authorResult" type="Author">

d <id " property="id" column="author_id"/>

<result property="username" column="author_username"/>

<result property="password" column="author_password"/>

<result property="email" column="author_email"/>

<result property="bio" column="author_bio"/>

</resultMap>

在上面的例子中,您会看到Blog 的作者("author")联合一个"authorResult"结果映射

来加载Author 实例。

 

resultMap 的配置是一个关键点

5. 还有一个强大的功能是 动态sql语句

 

5.1

<select id="findActiveBlogLike"

parameterType="Blog" 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 title like #{author.name}

</if>

MyBatis 3 - User Guide

49

</where>

</select>

 

<trim prefix="WHERE" prefixOverrides="AND |OR ">

</trim> 与上类似

 

5.2

在动态update语句里相似的解决方式叫做set,这个set元素能够动态地更新列。例如:

 

<update id="updateAuthorIfNecessary"

parameterType="domain.blog.Author">

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>

 

 

 

5.3Foreach 元素

另一个动态 SQL 经常使用到的功能是集合迭代,通常用在 IN 条件句。例如:

<select id="selectPostIn" resultType="domain.blog.Post">

SELECT *

FROM POST P

WHERE ID in

h <foreach " item="item" " index="index" collection="list"

" open="(" " separator="," close=")">

#{item}

</foreach>

</select>

 

 

对上面这个映射 SQL 语句的java 调用代码示例如下:

List<Integer> authorIdList = new ArrayList<Integer>();

authorIdList.add(2);

authorIdList.add(3);

authorIdList.add(4);

List<Post> postList = (List<Post>)session.selectList(

"selectPostIn", authorIdList);

//将会查询出 ID 是 2、3、4 的文章

 

 

 

以上是基础内容,

5.4.1先写一个DEMO 测试一下

类如下

public class Article {
private int id;
private String title ;
private String content;
private Author author;
private Blog blog;

}

 

public class Author {
private int id;
private String name;
private String age;
private Blog blog;
private HashSet<Article> articles;
}

 

public class Blog {
private int id;
private String name;
private Author author;
private HashSet<Article> articles;
}

 

 

5.4.2.定义接口

public interface baseMapper<T> {
int insert(T t );
int delete(T t);
int deleteById(int id);
int update(T t );
T select(T t);
T selectById(int id);
LinkedList<T> selectAll();
}

 

public interface articleMapper extends baseMapper<Article> {

}

 

public interface authorMapper extends baseMapper<Author> {

}

 

public interface blogMapper  extends  baseMapper<Blog>{

}

 

5.4.3.实现 Mapper文件

先写resultMap

先不用resultMap 直接用resultType试试看

articleMapper.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="dao.ArticleMapper" >
<sql id="Base_Column_List" >
id, blog_id, author_id
</sql>
<sql id="Blob_Column_List" >
title, content
</sql>
<!--返回单个对象-->
<select id="selectById" resultType="domain.Article" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from article
where id = #{id,jdbcType=INTEGER}
</select>
<!--返回集合-->
<select id="selectAll" resultType="domain.Article" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from article
</select>
<delete id="deleteById" parameterType="java.lang.Integer" >
delete from article
where id = #{id,jdbcType=INTEGER}
</delete>

</mapper>

 

 

5.4.4 配置文件

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>
<!--导入jdbc.properties -->
<properties resource="jdbc.properties"/>
<!--配置日志-->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.connectionURL}"/>
<property name="username" value="${jdbc.userId}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/articleMapper.xml"/>
</mappers>
</configuration>

 

log4j.properties

 

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.dao=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

jdbc.properties

jdbc.driverLocation=J://MAKER//jarFiles//jar//mysql-connector-java-5.1.7-bin.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/alltest
jdbc.userId=root
jdbc.password=123456

 

 

工具类

MyBatisUtil.java

 

package util;

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 java.io.IOException;
import java.io.Reader;

/**
* Created by Administrator on 2017/7/16.
*/
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static{
String resource = "mybatis-config.xml";

Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
// System.out.print(reader);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
public static SqlSession getsqlSession(){
return sqlSessionFactory.openSession();
}
public static void colsesqlSession(SqlSession s){
s.close();
}
}

 

 

测试方法

private SqlSession session;

@Before
public void before() {
session = MyBatisUtil.getsqlSession();
}
@After
public void after(){
MyBatisUtil.colsesqlSession(session);
}

 

//测试获取单个对象
@Test
public void test1() {

ArticleMapper articleMapper = session.getMapper(ArticleMapper.class);
Article article = articleMapper.selectById(1);
System.out.println(article);
}
//测试集合
@Test
public void test2() {
ArticleMapper articleMapper = session.getMapper(ArticleMapper.class);
LinkedList<Article> articles = articleMapper.selectAll();
for (Article article : articles) {
System.out.println(article);
}
}

 

 

 

5.4.5测试结果

DEBUG [main] - ==> Preparing: select id, blog_id, author_id , title, content from article where id = ?

DEBUG [main] - ==> Parameters: 1(Integer)

TRACE [main] - <== Columns: id, blog_id, author_id, title, content

TRACE [main] - <== Row: 1, 1, 1, <<BLOB>>, <<BLOB>>

DEBUG [main] - <== Total: 1

Article{id=1, title='title1', content='conten1', author=null, blog=null}

DEBUG [main] - ==> Preparing: select id, blog_id, author_id , title, content from article

DEBUG [main] - ==> Parameters:

TRACE [main] - <== Columns: id, blog_id, author_id, title, content

TRACE [main] - <== Row: 1, 1, 1, <<BLOB>>, <<BLOB>>

DEBUG [main] - <== Total: 1

Article{id=1, title='title1', content='conten1', author=null, blog=null}

 

Process finished with exit code 0

显然,没有查询到相关的 author 和blog 信息

配置resulmap 高级映射, 自己写sql语句

5.4.6 高级映射分两种 一种是映射写select语句, 一种是映射写连接查询

鉴于结果集需要区分column名字, 改了一下实体类属性名

public class Article {
private int article_id;
private String article_title ;
private String article_content;
private Author article_author;
private Blog article_blog;

 

public class Author {
private int author_id;
private String author_name;
private String author_age;
private Blog author_blog;
private HashSet<Article> author_articles;

 

public class Blog {
private int blog_id;
private String blog_name;
private Author blog_author;
private HashSet<Article> blog_articles;

 

articleMapper.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="dao.ArticleMapper">

<resultMap id="authorResult" type="domain.Author">
<id property="author_id" column="author_id"/>
<result property="author_name" column="author_name"/>
<result property="author_age" column="author_age"/>
<association property="author_blog" javaType="domain.Blog" resultMap="blogResult"/>
</resultMap>
<resultMap id="blogResult" type="domain.Blog">
<id property="blog_id" column="blog_id"/>
<result property="blog_name" column="blog_name"/>
<association property="blog_author" javaType="domain.Author" resultMap="authorResult"/>
</resultMap>

<resultMap id="BaseResultMap" type="domain.Article">
<id property="article_id" column="article_id"/>
<result property="article_content" column="article_content"/>
<result property="article_title" column="article_title"/>
<association property="article_author" javaType="domain.Author" resultMap="authorResult"/>
<association property="article_blog" javaType="domain.Blog" resultMap="blogResult"/>
</resultMap>


<!--返回单个对象-->
<select id="selectById" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select ar.id AS article_id,
ar.content AS article_content,
ar.title AS article_title,
au.id AS author_id ,
au.age AS author_age,
au.name AS author_name,
bl.id AS blog_id,
bl.name AS blog_name
from article ar LEFT JOIN author au ON ar.author_id = au.id
LEFT JOIN blog bl ON ar.blog_id = bl.id
where ar.id = #{id,jdbcType=INTEGER}
</select>
<!--返回集合-->
<select id="selectAll" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select ar.id AS article_id,
ar.content AS article_content,
ar.title AS article_title,
au.id AS author_id ,
au.age AS author_age,
au.name AS author_name,
bl.id AS blog_id,
bl.name AS blog_name
from article ar LEFT JOIN author au ON ar.author_id = au.id
LEFT JOIN blog bl ON ar.blog_id = bl.id
</select>
<delete id="deleteById" parameterType="java.lang.Integer">
delete from article
where id = #{id,jdbcType=INTEGER}
</delete>

</mapper>

 

 

5.4.7测试结果 为(不尽人意,感觉Mybatis好不智能,虽然方便)

也就是说,会出现内存占用, 两个一模一样的Blog实例和Author实例

本来预想的是: Blog 和Author 会无限的占用内存,没想到Mybatis会自动检测了一对一双向, 不会一直的new 对象,注入属性

但是 Article 中的 author, blog 却是与 Author中的blog ,Blog中的Author不是一个对象.

 

5.4.8 解决办法

对于Article 就只专注于 Article 的信息就可以了 ,删除authorResult和blogResult 的association.


<resultMap id="authorResult" type="domain.Author">
<id property="author_id" column="author_id"/>
<result property="author_name" column="author_name"/>
<result property="author_age" column="author_age"/>
<!--<association property="author_blog" javaType="domain.Blog" resultMap="blogResult"/>-->
</resultMap>
<resultMap id="blogResult" type="domain.Blog">
<id property="blog_id" column="blog_id"/>
<result property="blog_name" column="blog_name"/>
<!--<association property="blog_author" javaType="domain.Author" resultMap="authorResult"/>-->
</resultMap>

 

虽然没有出现爆sql语句的现象,但是换成另一种就会爆了 ,重写articleMapper.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="dao.ArticleMapper">

<resultMap id="authorResult" type="domain.Author">
<id property="author_id" column="author_id"/>
<result property="author_name" column="author_name"/>
<result property="author_age" column="author_age"/>
<association property="author_blog" column="author_id" select="selectBlog2"/>
</resultMap>
<resultMap id="blogResult" type="domain.Blog">
<id property="blog_id" column="blog_id"/>
<result property="blog_name" column="blog_name"/>
<association property="blog_author" column="blog_id" select="selectAuthor2"/>
</resultMap>

<resultMap id="BaseResultMap" type="domain.Article">
<id property="article_id" column="article_id"/>
<result property="article_content" column="article_content"/>
<result property="article_title" column="article_title"/>
<association property="article_author" column="article_author_id"
select="selectAuthor"/>
<association property="article_blog" column="article_blog_id" select="selectBlog"/>
</resultMap>



<!--返回单个对象-->
<select id="selectById" resultMap="BaseResultMap" >
select ar.id AS article_id,
ar.content AS article_content,
ar.title AS article_title,
ar.author_id AS article_author_id ,
ar.blog_id AS article_blog_id
from article ar
where ar.id = #{id,jdbcType=INTEGER}
</select>
<select id="selectAuthor" resultMap="authorResult">
SELECT au.id AS author_id ,
au.age AS author_age,
au.name AS author_name
FROM author au
WHERE au.id= #{article_author_id,jdbcType=INTEGER}
</select>
<select id="selectBlog" resultMap="blogResult">
SELECT
bl.id AS blog_id,
bl.name AS blog_name
FROM blog bl
WHERE bl.id=#{article_blog_id,jdbcType=INTEGER}
</select>

<select id="selectAuthor2" resultMap="authorResult">
SELECT au.id AS author_id ,
au.age AS author_age,
au.name AS author_name
FROM author au
WHERE au.id= #{author_id,jdbcType=INTEGER}
</select>
<select id="selectBlog2" resultMap="blogResult">
SELECT
bl.id AS blog_id,
bl.name AS blog_name
FROM blog bl
WHERE bl.id=#{blog_id,jdbcType=INTEGER}
</select>

<!--返回集合-->
<select id="selectAll" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select ar.id AS article_id,
ar.content AS article_content,
ar.title AS article_title,
au.id AS author_id ,
au.age AS author_age,
au.name AS author_name,
bl.id AS blog_id,
bl.name AS blog_name
from article ar LEFT JOIN author au ON ar.author_id = au.id
LEFT JOIN blog bl ON ar.blog_id = bl.id
</select>
<delete id="deleteById" parameterType="java.lang.Integer">
delete from article
where id = #{id,jdbcType=INTEGER}
</delete>

</mapper>

 

5.4.9尴尬,也没爆Sql 1+n问题怕是只是n+1才能了

但这样配置相当麻烦

总结一下来 最少代码原则

resuqltMap id(唯一标识) type(java全类名)

子标签 id 和 result property(java类属性名) column(字段名)

association 一对一

  1. property(java类属性名) column="传入select的字段名(会在select中用#获取)" select="select 唯一标识 "
  2. property(java类属性名) resultMap(制定的resulMap唯一标识) (不内嵌)
  3. property(java类属性名) javaType=" java全类名"(内嵌)

collection 一对多

  1. property(java类属性名) column="传入select的字段名(会在select中用#获取)" select="select 唯一标识 "
  2. property(java类属性名) resultMap(制定的resulMap唯一标识) (不内嵌)
  3. property(java类属性名) ofType=" java全类名"(内嵌)

 

select id唯一标识 resultMap 或resultType(类全名)

#用来获取参数 返回值自动封装为list 根据resultMap/Type弄成对象

discriminator 选择器,例如若表中isBoos=1,则有一个专用汽车属性,用来选择装填属性,若果isBoos=2,则有一个专用飞机,等等

<discriminator javaType="int" column="vehicle_type">

<case value="1" resultMap="carResult"/>

<case value="2" resultMap="truckResult"/>

<case value="3" resultMap="vanResult"/>

<case value="4" resultMap="suvResult"/>

</discriminator>

 

5.5动态sql

<if test="title != null"> </if>

<choose><when test="title != null"></when><otherwise></choose></choose>

<trim prefix="WHERE" prefixOverrides="AND |OR "></trim>

或者<where>(中间if)</where>

<set>(中间if)</set>或者 <trim prefix="SET" suffixOverrides=","></trim>

 

<foreach " item="item" " index="index" collection="list"

" open="(" " separator="," close=")">

#{item}

</foreach>

List<Integer> authorIdList = new ArrayList<Integer>();

authorIdList.add(2);

authorIdList.add(3);

authorIdList.add(4);

List<Post> postList = (List<Post>)session.selectList(

"selectPostIn", authorIdList);

 

 

6.1以下算是高级应用,Mybatis调优化

 

2. 使用 ExecutorType. . REUSE

session = factory.openSession(ExecutorType.REUSE);

对于 XML 映射配置文件中定义的所有 SQL 语句在执行时都由 Connection 创建一个

PreparedStatement 对象来执行。但 MyBatis 会把每条 SQL 语句的PreparedStatement 对象缓存

起来,等到下次再执行相同的 SQL 语句,则使用缓存的 PreparedStatement 对象。如,执行 N 次

session.selectOne("selectBlog", #{id}),只是创建一个 PreparedStatement 对象。

 

6.2关于openSession()

默认的openSession()方法不需要参数,创建的 SqlSession 有以下特征:

• 将会启用一个事务作用域 (即不会自动提交)

• 一个连接对象将从正在生效的运行环境所配置的数据源实例中获得

• 事务隔离级别是由驱动或数据源使用的默认级别

• PreparedStatements 不会被重用,也不会进行批量更新

SqlSession openSession()

SqlSession openSession(boolean autoCommit)

SqlSession openSession(Connection connection)

SqlSession openSession(TransactionIsolationLevel level)

SqlSession openSession(ExecutorType execType,TransactionIsolationLevel level)

SqlSession openSession(ExecutorType execType)

SqlSession openSession(ExecutorType execType, boolean autoCommit)

SqlSession openSession(ExecutorType execType, Connection connection)

里面参数有 boolean autoCommit Connection connection TransactionIsolationLevel level ExecutorType execType

前四个不用解释 , ExecutorType execType 是前面用过的一个优化方法

ExecutorType.SIMPLE 它只为每个执行语句创建一个 PreparedStatement。

ExecutorType.REUSE 重用PreparedStatements。

ExecutorType.BATCH 将会批量执行更新语句,如果SELECT 在更新语句中被执行,要

根据需要进行区分,确保动作易于理解

注意 也可以通过 session. getConfiguration() 反射得到 Configuration 实例

 

6.3 RowBounds 用来分页

new RowBounds(1, 2) // 第一个个参数是跳过多少,第二个是取多少,前面参数要为null才能生效 

 

6.4 结果处理器

ResultHandler

定义一个自己的 处理器

public class resultHandlerArticle implements ResultHandler{

@Override
public void handleResult(ResultContext resultContext) {
System.out.println("resultContext.getResultCount()===="+resultContext.getResultCount());
Article article = (Article) resultContext.getResultObject();
System.out.println(article.getArticle_content());
}
}

 

测试类代码

@Test
public void testSelectListHandler() {
resultHandlerArticle resultHandler = new resultHandlerArticle();
session.select("dao.ArticleMapper.selectAll", null,resultHandler);
}

 

凡是 session.select() 没有返回值的 ,都需要用到自己定义的处理器,

但前提要把 configuration 里的SafeResultHandlerEnabled 设置为false

 

它的处理过程是这样:

根据mapper文件,自动处理成对象,然后对结果集ResultSet中的每一行, 都调用处理器的handleResult方法 ,给的权利还是不够大, 它自己做了对象映射,但是没有开放给使用者结果集的处理权限

 

 

6.5 控制事物

SQLSession

四个方法 顾名思义 (项目中可以交给Spring管理事物.mybatis 并不会自动提交)

void commit()

void commit(boolean force)

void rollback()

void rollback(boolean force)

void clearCache() 清楚Session缓存

session.close(); 确保session不工作后 可以关闭

 

6.6 mapper两种方式, annotation或者XML XML主流都用XML

 

 

6.7 org.apache.ibatis.jdbc.SqlBuilder;

有些无语,只不过是拼接字符串而已,对于Sql语句掌握基本良好的,完全可以不用

秒懂什么意思了吧

 

@Test
public void testSqlBuilder(){
SqlBuilder.BEGIN();
SqlBuilder.SELECT("username,userpassword,age");
SqlBuilder.FROM("user");
SqlBuilder.WHERE("usrename=1");
SqlBuilder.GROUP_BY("gropu_id");
System.out.println(SqlBuilder.SQL());
}

结果为

SELECT username,userpassword,age

FROM user

WHERE (usrename=1)

GROUP BY gropu_id

这个功能真是鸡肋 鸡肋鸡肋 食之无味,弃之可惜

 

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值