网站开发实用吗,网站推广交换链接,网站建设费属于广告费,北京金山办公软件股份有限公司官网输出类型
目录
输出简单类型输出 Map 类型key:列名 value:列名对应的值key:自己指定的列 value:自定义对象resultMap输出简单类型
CustomerMapper.java#xff1a;返回值为简单类型。
public interface CustomerMapper {/*查询总数*/public Integer getAccountCustomer();…输出类型
目录
输出简单类型输出 Map 类型key:列名 value:列名对应的值key:自己指定的列 value:自定义对象resultMap输出简单类型
CustomerMapper.java返回值为简单类型。
public interface CustomerMapper {/*查询总数*/public Integer getAccountCustomer();
}CustomerMapper.xml 返回值 resultType 为简单类型这个我们之前经常使用已经很熟悉了。
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.itlike.mapper.CustomerMapper!--查询总记录数--select idgetAccountCustomer resultTypeintegerSELECT count(*) FROM customer;/select/mapper测试类
public class MyTest {Testpublic void test(){SqlSession sqlSession MybatisUtils.openSession();CustomerMapper mapper sqlSession.getMapper(CustomerMapper.class);Integer accountCustomer mapper.getAccountCustomer();System.out.println(accountCustomer);sqlSession.close();}
}运行结果输出了数据库中的数据数量。12
输出 Map 类型
key:列名 value:列名对应的值
CustomerMapper.java返回值为 Map 类型
public interface CustomerMapper {/*根据Id查询用户*/public MapString,Object getCustomerWithId(Integer id);
}
CustomerMapper.xml返回值为 Map
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.itlike.mapper.CustomerMapper!--根据id查询客户--select idgetCustomerWithId resultTypejava.util.MapSELECT * FROM customer WHERE cust_id #{id}/select/mapper测试类:
public class MyTest {Testpublic void test(){SqlSession sqlSession MybatisUtils.openSession();CustomerMapper mapper sqlSession.getMapper(CustomerMapper.class);MapString, Object customer mapper.getCustomerWithId(1);System.out.println(customer);sqlSession.close();}
}运行结果key 是列名 value 是列名对应的值。
{cust_profession射手, cust_name鲁班, cust_id1, cust_phone13499887733, email12341241qq.com}key:自己指定的列 value:自定义对象 resultMap
之有在写输出时使用的都是 resultType但是 resultType 要求字段名称和数据库当中的名称一致才能有值否则为 null如果 sql 查询字段名和 pojo 的属性名不一致可以通过 resultMap 将字段名和属性名作一个对应关系。
当 domain 中的字段与数据库当中名字不一致时 直接查询出来的结果会是 null因为 domain 的字段与数据库不一样只有一个名称相同的 email 可以查询到。
需要用 resultMap 自己做一个映射关系将数据库中的名称与 domain 对应。
然后查询出来的结果便不是 null。