MyBatis

MyBatis

一、MyBatis框架(ORM)

1.1.MyBatis代替JDBC

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-psPQxRjE-1603509942562)(C:\Users\张强\Pictures\1.PNG)]

2.MyBatis工具类

package com.qfedu.util;

import java.io.IOException;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SessionUtil {

	private static SqlSession session = null;
	private static SqlSessionFactory factory = null;

	static{
		try {
			factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取SqlSession对象的工具方法
	 * @return
	 */
	public static SqlSession getSession(){

		session = factory.openSession(true);

		return session;
	}

	/**
	 * 关闭SqlSession的工具方法
	 * @param session
	 */
	public static void closeSession(SqlSession session){
		if(session != null){
			session.close();
			session = null;
		}
	}
}

3.MyBatis的ORM

image-20201017100214556
1.纯注解
   在dao/IUserdao2中,使用@select,@insert,@update,@delete(delete from user where uid = #{uid)方式,并在mybatis-config.xml配置<mapper class=""></mappper>
2.Dao和.xml
    定义一个dao接口,在resource/Dao/IUserdao.xml中配置namespace+id唯一标识访问sql语句
3.纯.xml
  在resource下创建mapper里面放置.xml文件里面对sql进行CURD操作,并在myBatis-config.xml中配置相关的.xml路径

4.MyBatis的动态SQL

  <sql id="all" >
         select * from users
    </sql>
    
    将具有相同的sql语句提出来,后面根据需求添加sql语句,具有动态的sql特点;
    namespace+id ,可以唯一标识某一个sql语句,从而达到CURD的操作;
    
    <select id="getAllUsers" resultType="com.qf.pojo.Users">
        <include refid="all"></include>
    </select>

    <select id="selectUserByuid" resultType="com.qf.pojo.Users">
        <include refid="all"></include>
        where uid=#{uid}
    </select>
sers">
        <include refid="all"></include>
        where uid=#{uid}
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值