美文网首页
mybatis学习笔记一

mybatis学习笔记一

作者: 大赟哥 | 来源:发表于2017-06-09 19:52 被阅读0次

[TOC]

框架技术

框架技术.png

持久化

持久化1.png 持久化2.jpg

mybatis概念

  • 概念:一个持久层框架

  • 作用:ORM将sql语句映射成实体类

  • 特点:巧灵活、半自动化、使用与中小型项目的开发

mybatis 入门

1.png

1、创建mybatis-config.xml文件

<!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://localhost:3306/bank?useUnicode=true&characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="UserMapper.xml"/>
  </mappers>
</configuration>

2、创建映射文件

<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  <mapper namespace="user">
    <select id="selectUser" resultType="com.hemi.mybatis.bean.User">
        select * from user where uid=1;
    </select>
  </mapper>

3、获取xml配置文件

InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

4、创建SqlSessionFactory

SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);

5、获取SqlSession

SqlSession sqlSession = factory.openSession();

6、调用SqlSession的selectOne(命名空间.id名称);

Object object = sqlSession.selectOne("user.selectUser");

7、关闭SqlSession

sqlSession.close();

增删改

2.png
<insert id="insertUser" parameterType="com.hemi.mybatis.bean.User">
    <!-- 通过#{属性名}来获取对象的值 -->
    insert into user (username,password) values(#{username},#{password}); 
</insert>

<update id="updateUser" parameterType="com.hemi.mybatis.bean.User">
    update user set username=#{username},password=#{password} where uid=#{uid}
</update>

3.png
<delete id="deleteUser" parameterType="int">
    delete from user where uid=#{value}
</delete>

Mapper接口开发

一、定义一个接口

public interface TypeMapper {
    Type selectType(int typeid);
}

二、定义一个mapper.xml映射文件

mapper文件的要求:

1、namespace的值就是对象接口的全类名,并且类名和xml文件名保持一致
2、id的值就是抽象方法
3、resultType的值必须和抽象方法的返回值一致
4、parameterType的值和抽象方法的参数类型一致

注意 mapper.xml文件的约束是mapper.dtd,不是config.dtd

三、使用

将mybatis入门步骤中的步骤六改为如下代码:

TypeMapper mapper=sqlSession.getMapper(TypeMapper.class);
Type type=mapper.selectType(1);

动态sql

if

  SELECT * FROM good INNER JOIN type ON good.type = type.typeid where 1=1
    <if test="gname !=null and gname !=''">
      and gname like concat('%',#{gname},'%')
    </if>

注意:

1、字符串的拼接建议使用concat来代替${}

2、判断条件中获取数据不用加#{},与el表达式不一样

where

作用where可以自动去除第一个and

 <where>
      <if test="gname !=null and gname !=''">
        and gname like concat('%',#{gname},'%')
      </if>
      <if test="typename !=null and typename!=''">
        and typename like concat('%',#{typename},'%')
      </if>
    </where>

choose when otherwise

作用:组合使用,相当于if else if else

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = 'ACTIVE'
  <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>

set

  update good 
     <set>
        <if test="gname!=null and gname!=''">
          gname=#{gname},
        </if>
        <if test="gprice!=null and gprice!=''">
          gprice=#{gprice}
        </if>
      </set>
  where gid=#{gid}

trim

作用:去除多余字符串

两种常见的用法

1、where and

prefix:字首 prefixOverrides:去除第一个指定的字符串

    select * from good
    <trim prefix="where" prefixOverrides="and|or">
      <!--添加where在前面,并且去除第一个and-->
      <if test="gname !=null and gname !=''">
        and gname like concat('%',#{gname},'%')
      </if>
      <if test="typename !=null and typename!=''">
        and typename like concat('%',#{typename},'%')
      </if>
    </trim>

2、set

prefix:字首 suffixOverrides:去除最后指定的字符串

    update good 
    <trim prefix="set" suffixOverrides=",">
      <!--添加set在前面,并且去除最后一个,-->
        <if test="gname!=null and gname!=''">
          gname=#{gname},
        </if>
        <if test="gprice!=null and gprice!=''">
          gprice=#{gprice},
        </if>
      </trim>

foreach

作用:动态循环拼接sql部分内容

1、open代表在集合前面添加的字符串

2、close代表在集合后面添加的字符串

3、separator代表集合分割使用的字符串

4、collection代表被循环的集合,值可以是list、map、array

5、常见用法,in的语句

<select id="selectGoodByGid" parameterType="list" resultType="Good">
    select gid,gname,gprice,count,type typeid from good
    <where>
      gid in

      <foreach item="item" collection="list" open="(" separator="," close=")">
             #{item}
        </foreach>
    </where>
  </select>

1、如何配置多个数据库
使用environment可以配置多个数据库,通过ID来表示
2、transactionManager的type有哪些
JDBC、MANAGED

3、dataSource作用
配置数据源,有三种类型:UNPOOLED、POOLED、JNDI还可以配置四大参数(驱动。url/用户名、密码)

4、properties有什么用,如何用
加载配置文件(resource/url),
配置键值对(proprerty)
注意:如果resource和proprerty,resource优先级高

5、typeAliases有什么用,如何用
起别名
用类名(类名)

6、databaseIdProvider + databaseid

7、databaseIdProvider:给数据库起别名

8、databaseid:在statement中使用
myBatista根据url可以自动使用当前是什么数据库,然后使用哪个sql语句

9、mapper中的namespace有什么用
如果有相同的statementid的时候,使用namespace类区别
使用mapper接口开发dao时,namespace与接口全类名一致

10、增删改查
id:statement的唯一标识,或者说是调用方法名
parameterType:传入参数的数据类型 基本数据(可以不写)+引用类型(必须写)
resultType:返回数据类型
parameterType和resultType:都可以使用别名来表示数据类名

11、myBatista中的数据类型的别名
Integer Integer,int
int _int

12、#{}作用
获取传入数据

13、Mapper接口开发的四大要求
1、namespace的值就是对象接口的全类名,并且类名和xml文件名保持一致
2、id的值就是抽象方法
3、resultType的值必须和抽象方法的返回值一致
4、parameterType的值和抽象方法的参数类型一致

相关文章

网友评论

      本文标题:mybatis学习笔记一

      本文链接:https://www.haomeiwen.com/subject/lavfqxtx.html