Mybatis-获取值的两种方式


<select id="findAll" resultType="user">

    select * from user

</select>



注意:

1、查询的标签select必须设置属性resultType或resultMap,用于设置实体类和数据库表的映射 关系

resultType:自动映射,用于属性名和表中字段名一致的情况

resultMap:自定义映射,用于一对多或多对一或字段名和属性名不一致的情况

2、当查询的数据为多条时,不能使用实体类作为返回值,只能使用集合,否则会抛出异常 TooManyResultsException;但是若查询的数据只有一条,可以使用实体类或集合作为返回值

二、获取参数值的两种方式(重点掌握)


**Mybatis获取参数值的两种方式: ${} 和 #{}

${}本质上就是字符串拼接,#{}本质上是占位符赋值

${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;但是#{}使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时,可以自 动添加单引号**

**(1) 单个字面量类型的参数

若mapper接口中的方法参数是单个的时候

此时可以任意使用${}和#{}获取参数值**

**(2) 多个字面量类型的参数

若mapper接口中的方法参数是多个的时候,这个时候Mybatis会自动将这些参数放在Map集合中,以Mybatis已定义的 arg0,arg1…为键,来获取值,或param1,param2…为键 获取值。 注意:使用${}时需手动添加单引号**

**(3) map集合类型的参数

若mapper接口中的方法需要多个参数时,我们可以创建map集合,将数据放到map集合中 也是通过${}个#{}来获取值,注意: ${}需要手动添加单引号**

**(4) 实体类类型的参数

若mapper接口中的方法参数为实体类对象时 此时可以使用

$ {} #{},通过访问实体类对象中的属性名获取属性值**,注意${}需要手动加单引号

**(5)使用@Param标识参数

通过@Param该注解标识mapper接口当中的方法参数,会将这些参数放在Map集合中,以该注解的 value值为键 以参数为值;

和#{} 和 ${}访问map集合的键可以获取相对应的值,仍然注意字符串拼接的问题。**


package com.atguigu.dao;



import com.atguigu.domain.User;



import java.util.List;



public interface UserMapper {

    //查询所有用户

    public List<User> findAll();

    public int insertUser(User user); //添加

    public int deleteUser(int id); //删除

    public int updateUser(User user); //修改

    public User findUserById(int id); //查询一个实体类对象

}




<?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="com.atguigu.dao.UserMapper">

    <select id="findAll" resultType="user">

        select * from user

    </select>

    

    <insert id="insertUser" parameterType="user">

        insert into user(id,name,gender) values(#{id},#{name},#{gender})

    </insert>



    <delete id="deleteUser" parameterType="int">

        delete from user where id=#{id}

    </delete>



    <update id="updateUser" parameterType="user">

        update user set naMybatis_demo1me=#{name},gender=#{gender} where id=#{id}

    </update>



    <select id="findUserById" resultType="user" parameterType="int">

        select * from user where id=#{id}

    </select>

</mapper>




import com.atguigu.dao.UserMapper;

import com.atguigu.domain.User;

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 org.junit.Test;



import java.io.IOException;

import java.io.InputStream;

import java.util.List;



public class TestMybatis {

    @Test

    public void test1() throws IOException {

        //1.读取核心配置文件

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

        //2.创建SqlSessionFactoryBuild对象

        SqlSessionFactoryBuilder sqlSessionFactoryBuilder

                = new SqlSessionFactoryBuilder();

        //3.通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);

        //4.创建SqlSession对象 通过sqlSession对象所操作的sql都会自动提交

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //5.代理模式创建UserMapper接口的代理实现类

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);



        List<User> users = mapper.findAll();

        System.out.println(users);



        sqlSession.close();

    }

    @Test

    public void test2() throws IOException {

        //1.读取核心配置文件

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

        //2.创建SqlSessionFactoryBuild对象

        SqlSessionFactoryBuilder sqlSessionFactoryBuilder

                = new SqlSessionFactoryBuilder();

        //3.通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);

        //4.创建SqlSession对象 通过sqlSession对象所操作的sql都会自动提交

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //5.代理模式创建UserMapper接口的代理实现类

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);



        User user = new User();

        user.setId(2);

        user.setName("隔壁老王");

        user.setGender("男");



        int result = mapper.insertUser(user);

        System.out.println(result);

    }



    @Test

    public void test3() throws IOException {

        //1.读取核心配置文件

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

        //2.创建SqlSessionFactoryBuild对象

        SqlSessionFactoryBuilder sqlSessionFactoryBuilder

                = new SqlSessionFactoryBuilder();

        //3.通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);

        //4.创建SqlSession对象 通过sqlSession对象所操作的sql都会自动提交

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //5.代理模式创建UserMapper接口的代理实现类

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);



        int result = mapper.deleteUser(2);

        System.out.println(result);

    }

    @Test

    public void test4() throws IOException {

        //1.读取核心配置文件

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

        //2.创建SqlSessionFactoryBuild对象

        SqlSessionFactoryBuilder sqlSessionFactoryBuilder

                = new SqlSessionFactoryBuilder();

        //3.通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);

        //4.创建SqlSession对象 通过sqlSession对象所操作的sql都会自动提交

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //5.代理模式创建UserMapper接口的代理实现类

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);



        User user = new User();

        user.setId(2);

        user.setName("李四");

        user.setGender("女");



        int result = mapper.updateUser(user);

        System.out.println(result);

    }

    @Test

    public void test5() throws IOException {

        //1.读取核心配置文件

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

        //2.创建SqlSessionFactoryBuild对象

        SqlSessionFactoryBuilder sqlSessionFactoryBuilder

                = new SqlSessionFactoryBuilder();

        //3.通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);

        //4.创建SqlSession对象 通过sqlSession对象所操作的sql都会自动提交

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //5.代理模式创建UserMapper接口的代理实现类

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);



        User user = mapper.findUserById(1);

        System.out.println(user);

    }

}



三、小结


**本章介绍了获取mapper接口的方法参数值的两种方式,需要重点掌握。

如有理解不到位之处,请读者予以指正**

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值