Mybatis中resultMap标签和sql标签的设置方式

网友投稿 700 2022-11-09

Mybatis中resultMap标签和sql标签的设置方式

Mybatis中resultMap标签和sql标签的设置方式

目录resultMap标签和sql标签的设置1、项目目录2、数据库中的表的信息3、配置文件的信息4、User类5、IUserDao接口6、MybatisTest7、运行结果resultMap标签的使用规则自定义结果映射规则association联合查询使用association进行分布查询collection分步查询

resultMap标签和sql标签的设置

1、项目目录

2、数据库中的表的信息

3、配置文件的信息

1、SqlMapConfig.xml文件

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

2、IUserDao.xml

其中的mapper标签中的namespace属性指的就是持久层中的接口,这里的sql语句都是对应这个接口中的方法,也就是指定了命名空间。

在这里resultMap标签是查询结果的列名和实体类的属性名的对应关系,也就是说我们类中的属性名不一定和数据库中的保持一致,其中property配置的就是类中的属性名,column设置的就是数据库中表的字段名。

在sql语句的标签中之前的,resultType变成了resultMap。sql标签中直接写的是就是sql语句,这个可以有效的避免重复的写sql相同代码,如果要引用sql标签中内容,在对应的语句中需要引用Include标签,具体的可以看下面的代码。

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from users

select * from users where id = #{uid}

4、User类

package com.mybatis.domain;

import java.io.Serializable;

import java.util.Date;

public class User implements Serializable {

private Integer userId;

private String userName;

private Date userBirthday;

private String userSex;

private String userAddress;

public Integer getUserId() {

return userId;

}

public void setUserId(Integer userId) {

this.userId = userId;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public Date getUserBirthday() {

return userBirthday;

}

public void setUserBirthday(Date userBirthday) {

this.userBirthday = userBirthday;

}

public String getUserSex() {

return userSex;

}

public void setUserSex(String userSex) {

this.userSex = userSex;

}

public String getUserAddress() {

return userAddress;

}

public void setUserAddress(String userAddress) {

this.userAddress = userAddress;

}

@Override

public String toString() {

return "User{" +

"userId=" + userId +

", userName='" + userName + '\'' +

", userBirthday=" + userBirthday +

", userSex='" + userSex + '\'' +

", userAddress='" + userAddress + '\'' +

'}';

}

}

5、IUserDao接口

package com.mybatis.dao;

import com.mybatis.domain.User;

import java.util.List;

public interface IUserDao {

/**

* 查询所有用户

* @return

*/

List findAll();

/**

* 根据ID查询用户信息

* @param userId

* @return

*/

User findById(Integer userId);

}

6、MybatisTest

package com.mybatis.test;

import com.mybatis.dao.IUserDao;

import com.mybatis.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.After;

import org.junit.Before;

import org.junit.Test;

import java.io.IOException;

import java.io.InputStream;

import java.util.Date;

import java.util.List;

public class MybatisTest {

private InputStream in;

private SqlSession session;

private IUserDao userDao;

@Before

public void init() throws Exception {

this.in = Resources.getResourceAsStream("SqlMapConfig.xml");

SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();

System.out.println(in);

SqlSessionFactory factory = factoryBuilder.build(in);

// this.session = factory.openSession(true);

this.session = factory.openSession();

this.userDao = session.getMapper(IUserDao.class);

}

@After

public void destory() throws IOException {

session.commit();

this.in.close();

this.session.close();

}

@Test

public void testFindAll() throws Exception{

List users = userDao.findAll();

for (User user:users){

System.out.println(user);

}

}

}

7、运行结果

resultMap标签的使用规则

自定义结果映射规则

select * from employee where id=#{id}

OaIOvtFMIr

association联合查询

association可以指定联合的javabean对象

property="dept":指定哪个属性是联合对象javaType:指定这个属性的类型

select e.id id,e.name empName,e.email email,e.sex sex,e.d_id d_id,

d.id did,d.name deptName from employee e,dept d

where e.d_id=d.id and e.id=#{id}

使用association进行分布查询

1、先按照员工id查询员工信息将会调用查询员工的sql

2、根据查询员工信息中的d_id值去部门表中查出部门信息

3、部门设置到员工中

column="d_id">

column="d_id">

select * from employee where id=#{id}

嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则

select d.id did,d.name deptName,e.id eid,

e.name empName,e.sex,e.email

from dept d left join employee e

on d.id=e.d_id

where d.id=#{id}

collection分步查询

column="{id}">

&ltOaIOvtFMIr;!-- 或则 column="{deptId=id}"-->

column="{id}">

&ltOaIOvtFMIr;!-- 或则 column="{deptId=id}"-->

select * from employee where d_id=#{deptId}

select * from dept where id=#{id}

当分布查询需要传递多个多个值时,将多个值封装map传递

colum=“{key1=column1,key2=colum2...}”

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:POJ 2562:Primary Arithmetic
下一篇:「图型计算架构」GraphTech生态系统2019-第1部分:图型数据库
相关文章

 发表评论

暂时没有评论,来抢沙发吧~