mybatis入门笔记二

文章介绍了如何在Idea中设置文件模板,包括核心配置和映射文件。接着讲解了封装SqlSessionUtils工具类以简化SqlSession的获取,避免重复读取配置文件。然后详细讨论了MyBatis中参数传递的两种方式——字符串拼接和占位符赋值,在不同场景下的使用,以及参数为单个、多个、实体类型或通过@Param注解的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、idea中设置核心配置文件和映射文件的模板

settings–>editor–>File and Code Templates

​ Name设置模板名字;Extension设置文件后缀 之后复制粘贴,将不固定内容不写

二、封装SqlSessionUtils工具类并测试功能

新建工具包和SqlsessionUtil工具类

package com.yhg.utils;

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.InputStream;

public class SqlSessionUtil {
    public  SqlSession getSqlSession(){
         SqlSession sqlSession = null;
        try {
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSession;
    }
}


在测试类中获取sqlSession就不用反复写读取配置文件,获取sqlSessionFactoryBuilder对象…直接调

用sqlSessionUtil类中的getSession方法,但是在getSqlSession方法中异常处理要使用try/catch,不建

议抛出异常,这样调用该方法时也要处理该异常。

三、mybatis获取参数的两种方式及各种情况

①字符串拼接:${} ②占位符赋值:#{}

各种情况:

一,mapper接口方法的参数为单个的字面量类型

<select id="getUserByU serName" resultType="User">
        select *from t_user where userName=#{userName}
    </select>

#{}在单个字面量参数的情况下,其中的值可以填任意,传过来的参数名并重要,重要的是传过来的参数

<select id="getUserByUserName" resultType="User">
        select *from t_user where userName='${userName}'
    </select>

${} 字符串拼接在单个字面量参数的情况下,如是字符类型,需要加单引号

二,mapper接口中的参数是多个时

select id="checkUser" resultType="User">
        select * from t_user where userName=#{arg0} and psd=#{param2}
    </select>

在多个参数时,此时mybatis会将这写参数放在一个map中,以两种方式进行存储

a>以arg0,arg1…为键,以参数为值

b>以param1,param2…为键,以参数为值

因此在使用${}或#{}获取参数时以键的方式获取,不一定要固定一套方式以arg为键,可以两套方式交叉

获取参数

三, mapper接口中有多个参数时,可以放在一个map中

首先在测试类中设置一个map,以userName,psd为键,参数为值

public void CheckByMapest() {
//
        SqlSession sqlSession =new SqlSessionUtil().getSqlSession();
        //获取mapper接口对象
        UserMapper mapper= sqlSession.getMapper(UserMapper.class);
        //测试功能
        Map<String, Object> map=new HashMap<>();
        map.put("userName","张三");
        map.put("psd","111111");
        User user=mapper.checkUserByMap(map);
        System.out.println(user);
        //提交事务
    }

设置好map集合后,mybatis直接以所设定的键值对访问参数

<select id="checkUserByMap" resultType="User">
        select * from t_user where userName=#{userName} and psd=#{psd}
    </select>

四,mapper接口的参数是一个实体类型的参数

    <insert id="insertUser">
        insert into t_user values (#{id},#{userName},#{psd},#{age},#{sex},#{email})
    </insert>

若传进来的参数是一个实体类型参数,只需要以属性的方式访问属性值即可。

五,通过@Parm命名参数

User checkUserByParam(@Param("userName") String userName,@Param("psd") String psd);

在mapper接口中定义方法时,以@parma的值为键,以参数为值,同时mybatis会自己以param1,

param2…为键,参数为值

 <select id="checkUserByParam" resultType="User">
    select * from t_user where userName=#{userName} and psd=#{psd}
   </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值