Mybatis --05.mybatis中的ResultMap、Sql片段

一、ResultMap自定义属性名和表结构字段映射

ResultMap是mabatis中最重要最强大的元素、使用resultMap可以解决二大问题:

1)、pojo属性名和表结构字段名不一致的问题(有些情况下也不是标准的驼峰格式)

2)、完成高级查询、如、一对一、一对多、多对多

解决表字段和属性名不一致的问题有二种方法:

1、如果是驼峰似的命名规则可以在mybatis核心配置中设置

2、使用ResultMap解决

3、加别名

下面使用ResultMap解决表结构和javaBean类字段名不一致问题

UserDaoMappers.xml






    
    
    
    
    
    
    
        
        
    

    
    

mybatis-config.xml




    
    
    
    
        
    
    
    
        
        
    
    
    
        
            
            
                
                
                
                
                
                
                
                
            
        
    
    
    
        
        
    

pojo、user.java

package com.sswblog.domain;

import java.io.Serializable;
import java.util.Date;

/**
 * @ Author     :ssw.
 * @ Date       :Created in 19:44 2018/11/4
 */
public class User implements Serializable {
    private static final long serialVersionUid = 1L;
    private Long id;
    private String userName;
    private String password;
    private String name;
    private Integer age;
    private Integer sex;
    private Date birthday;
    private Date created;
    private Date updated;

    public static long getSerialVersionUid() {
        return serialVersionUid;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", birthday=" + birthday +
                ", created=" + created +
                ", updated=" + updated +
                '}';
    }
}

UserDao.java

package com.sswblog.dao;

import com.sswblog.domain.User;

import java.util.List;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 0:06 2018/11/5
 */
public interface UserDao {
    /**
     * 查询所有用户
     * @return
     */
    public List findAllUsers();

}

UserDaoImpl.java

package com.sswblog.dao.Impl;

import com.sswblog.dao.UserDao;
import com.sswblog.domain.User;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 0:06 2018/11/5
 */
public class UserDaoImpl implements UserDao {
    //dao层增删除改查需要SqlSession
    private SqlSession sqlSession = null;

    //构造方法
    public UserDaoImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public List findAllUsers() {
        List users = this.sqlSession.selectList("UserDaoMapper.findAllUsers");
        this.sqlSession.close();
        return users;
    }

}

UserDaoImplTest.java

package com.sswblog.test;

import com.sswblog.dao.Impl.UserDaoImpl;
import com.sswblog.dao.UserDao;
import com.sswblog.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.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 15:05 2018/11/5
 */
public class UserDaoImplTest {
    private UserDao userDao = null;

    @Before
    public void setUp() throws Exception {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        userDao = new UserDaoImpl(sqlSession);
    }

    @Test
    public void findAllUsers() {
        List list = userDao.findAllUsers();
        for (User user : list) {
            System.out.println("user = " + user);
        }
    }

}

二、SQL片段

1、用法一:

在映射文件UserDaoMappers.xml中标签可以定义一个sql片段、再需要用到该sql片段、用include标签引入

UserDaoMappers.xml






    
    
    
    
    
    
    
        
        
    
    
    
        id,user_name,password,name,age,sex,birthday,created,updated
    

    
    

UserDao.java

package com.sswblog.dao;

import com.sswblog.domain.User;

import java.util.List;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 0:06 2018/11/5
 */
public interface UserDao {

    /**
     * 查询所有用户
     * @return
     */
    public List findAllUsers();
}

UserDaoImpl.java

package com.sswblog.dao.Impl;

import com.sswblog.dao.UserDao;
import com.sswblog.domain.User;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 0:06 2018/11/5
 */
public class UserDaoImpl implements UserDao {
    //dao层增删除改查需要SqlSession
    private SqlSession sqlSession = null;

    //构造方法
    public UserDaoImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public List findAllUsers() {
        List users = this.sqlSession.selectList("UserDaoMapper.findAllUsers");
        this.sqlSession.close();
        return users;
    }

}

UserDaoImplTest.java

package com.sswblog.test;

import com.sswblog.dao.Impl.UserDaoImpl;
import com.sswblog.dao.UserDao;
import com.sswblog.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.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 15:05 2018/11/5
 */
public class UserDaoImplTest {
    private UserDao userDao = null;

    @Before
    public void setUp() throws Exception {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        userDao = new UserDaoImpl(sqlSession);
    }

    @Test
    public void findAllUsers() {
        List list = userDao.findAllUsers();
        for (User user : list) {
            System.out.println("user = " + user);
        }
    }
}

Mybatis --05.mybatis中的ResultMap、Sql片段_第1张图片

2、用法二:

当同一个sql片段可能在很多个mapper文件里都有使用、这个 时候就要添加一个映射文件、来统一管理

sql片段

创建CommonSql.xml映射文件




    
        id,user_name,password,name,age,sex,birthday,created,updated
    

mybatis-config.xml中引入CommonSql.xml




    
    
    
    
        
    
    
    
        
        
    
    
    
        
            
            
                
                
                
                
                
                
                
                
            
        
    
    
    
        
        
        
    

UserDaoMappers.xml中使用sql片段、带上命名空间






    
    
    
    
    
    
    
        
        
    
    
    
        
    

    
    

测试类

package com.sswblog.test;

import com.sswblog.dao.Impl.UserDaoImpl;
import com.sswblog.dao.UserDao;
import com.sswblog.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.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 15:05 2018/11/5
 */
public class UserDaoImplTest {
    private UserDao userDao = null;

    @Before
    public void setUp() throws Exception {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        userDao = new UserDaoImpl(sqlSession);
    }
    @Test
    public void findAllUsers() {
        List list = userDao.findAllUsers();
        for (User user : list) {
            System.out.println("user = " + user);
        }
    }
}

Mybatis --05.mybatis中的ResultMap、Sql片段_第2张图片

 

你可能感兴趣的:(Mybatis)