resultMap结果集是用来定义sql查询的结果与java对象的映射关系。它主要解决2大问题:
1)可以解决POJO属性名和表结构的字段名不一致问题(甚至是 不是标准的驼峰命名法)
2)可以完成高级查询,例如:一对一,一对多,多对多
其中还有一个比较重要的属性:autoMapping自动映射 默认为true。
注意:1.如果true,并且列名称和字段名称一致,是可以完成映射。
2.如果为true,名称不一致,但是符合驼峰命名,但必须开启驼峰命名匹配。
下面简单介绍一个案列:
接口:UserMapper.java
package com.inter; import java.util.List; import org.apache.ibatis.annotations.Param; import com.pojo.User; public interface UserMapper { // 查询数据 public List<User> selectAll(); }
Mybatis配置文件:Mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 加载外部配置文件 --> <properties resource="jdbc.properties"></properties>
/* <settings> <!--开启驼峰命名法--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> */
<!-- 起别名 --> <typeAliases> <typeAlias alias="User" type="com.pojo.User"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <!-- 指定mapper的配置文件 --> <mappers> <mapper resource="UserMapper.xml"/> </mappers> </configuration>
映射文件:UserMapper.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.inter.UserMapper"> <resultMap type="User" id="userResult" autoMapping="true"> <id property="id" column="id"/> </resultMap> <!-- 查询所有的数据 --> <select id="selectAll" resultMap="userResult"> select * from tb_user </select> </mapper>
实体类:User.java
package com.pojo; public class User { private int id; private String name; private String age; private String sex; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; } }
测试类:
package com.test; import static org.junit.Assert.*; import java.io.InputStream; import java.util.List; 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 com.inter.UserMapper; import com.pojo.User; public class TestMybatisoneTest { private UserMapper userMapper; InputStream inputStream; SqlSessionFactory sqlSessionFactory; SqlSession sqlSession; @Before public void setUp() throws Exception { String resource = "mybatis-config.xml"; inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); sqlSession = sqlSessionFactory.openSession(); // 动态代理实现 userMapper = sqlSession.getMapper(UserMapper.class); } @Test public void test4(){ List<User> list = userMapper.selectAll(); for (User user : list) { System.out.println(user+" "); } } }