十分钟上手-搭建vue开发环境(新手教程)

单片机+ESP8266+继电器+云平台+MQTT服务 物联网 单片机+ESP8266+继电器+云平台+MQTT服务,DIY实现物联网。

  返回  

Mybatis02-传参、代理

2021/8/21 15:39:40 浏览:

一、sqlsession的三种查询方式

test代码

		//1.读取资源
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        //2.创建sessionFactory
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resource);
        //3.创建session
        SqlSession session = build.openSession();

        //4.调用方法
        List<Flower> list = session.selectList("com.xie.mapper.FlowerMapper.selectAll");
		System.out.println(list);
   
        Flower one = session.selectOne("com.xie.mapper.FlowerMapper.selectOne", 2);
        Flower one1 = session.selectOne("com.xie.mapper.FlowerMapper.selectOne2", list.get(0));
        System.out.println(one1);
        System.out.println(one.getName());
        session.close();

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="com.xie.mapper.Flower">
    <select id="selectAll" resultType="flower">
        select * from flower
    </select>
    <select id="selectOne"  resultType="flower" parameterType="int">
        select * from flower where id = #{id}
    </select>
    <select id="selectOne2" resultType="flower" >
        select * from flower where id = #{id} and name = #{name}
    </select>
</mapper>
  1. selectOne(String namespace,Object param)
  2. selectList(String namespace,Object param)
  3. selectMap(String namespace,Object param)
    可以看到,这三种方式只允许我们传入一个参数,若需要传入多个参数怎么办呢?

二、sqlsession的传参方式

  1. 传入单个参数
  2. 传入整个对象
  3. 传入一个map
<?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.xie.mapper.Flower">
    <select id="selectAll" resultType="flower">
        select * from flower
    </select>
    传入单个参数
    <select id="selectOne"  resultType="flower" parameterType="int">
        select * from flower where id = #{id}
    </select>
    传入一个flower对象,id,name都是它的属性
    <select id="selectOne2" resultType="flower" >
        select * from flower where id = #{id} and name = #{name}
    </select>
    传入的参数是一个map,key1对应id,key2对应name
    <select id="selectOne3" resultType="flower>
        select * from flower where id=#{key1} and name=#{key2}
</mapper>

三、Mapper代理

  1. xml文件的namespace必须是接口的全路径,通过这个namespace将xml作为接口的实现类
  2. 一个接口职能对应一个xml,不能多个
  3. 接口最好与xml同名

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号