项目结构
1.创建Student实体类
package com.it.domain;
public class Student {
private int sno;
private String sname;
private String ssex;
private int sage;
private String sdept;
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getSdept() {
return sdept;
}
public void setSdept(String sdept) {
this.sdept = sdept;
}
@Override
public String toString() {
return "Student{" +
"sno=" + sno +
", sname='" + sname + '\'' +
", ssex='" + ssex + '\'' +
", sage=" + sage +
", sdept='" + sdept + '\'' +
'}';
}
}
2.创建StudentDao接口类
package com.it.dao;
import com.it.domain.Student;
import java.util.List;
import java.util.Map;
public interface StudentDao {
//返回类型是map
Map<Object,Object> selectMapById(int id);
//使用 resultMap定义映射关系
List<Student> selectAllStudents();
}
3.创建StudentDao接口类的映射文件StudentDao.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.it.dao.StudentDao">
<!-- 返回Map,resultType可以等于它的别名map,也可以等于它的全限定名称,java.util.HashMap
1.列名是map的key值,列值是map的value;
2.返回map时,最多只能返回一行记录,多一行都是错误的。
注意:返回map用的比较少,推荐使用返回对象
-->
<select id="selectMapById" resultType="map">
select sno,sname,sage,ssex,sdept from student where sno=#{renyi}
</select>
<!-- 使用resultMap
1.先定义resultMap
2.在result标签,使用resultMap来引用1步骤定义的
-->
<!-- 定义resultMap
id:自定义的一个名称,来表示你定义的这个resultMap,
type:java类型的全限定名称
-->
<resultMap id="studentMap" type="com.it.domain.Student">
<!-- 主键列使用id标签,
column:数据库列名
property:java类型的属性名
-->
<id column="sno" property="sno"/>
<!-- 非主键使用result-->
<result column="sname" property="sname"/>
<result column="ssex" property="ssex"/>
<result column="sage" property="sage"/>
<result column="sdept" property="sdept"/>
</resultMap>
<select id="selectAllStudents" resultMap="studentMap">
select sno,sname,sage,ssex,sdept from student
</select>
</mapper>
4.创建MyBatis.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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<environments default="mydev">
<environment id="mydev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/it/dao/StudentDao.xml"/>
</mappers>
</configuration>
5.创建MyBatisUntils工具类
package com.it.utils;
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 java.io.IOException;
import java.io.InputStream;
public class MyBatisUtils {
// private static SqlSessionFactory factory=null;
//
// static {
// String mybatis="mybatis.xml";
// InputStream resourceAsStream=null;
// try {
// resourceAsStream = Resources.class.getResourceAsStream(mybatis);
// SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// factory= builder.build(resourceAsStream);
// }catch (Exception e){
// e.printStackTrace();
// }
//
// }
//
// public static SqlSession getSqlSession(){
// SqlSession sqlSession=null;
// if (factory!=null){
// sqlSession = factory.openSession(true);
// }
// return sqlSession;
// }
private static SqlSessionFactory factory = null;
static {
String config="MyBatis.xml";
InputStream resourceAsStream=null;
try {
resourceAsStream = Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
factory = builder.build(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
SqlSession sqlSession=null;
if (factory!=null){
sqlSession = factory.openSession(true);
}
return sqlSession;
}
}
6.创建测试类
package com.it;
import static org.junit.Assert.assertTrue;
import com.it.dao.StudentDao;
import com.it.domain.Student;
import com.it.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
import java.util.Map;
/**
* Unit test for simple App.
*/
public class AppTest
{
@Test
public void selectMapByIdTest(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao mapper = sqlSession.getMapper(StudentDao.class);
Map<Object, Object> objectObjectMap = mapper.selectMapById(1);
System.out.println(objectObjectMap);
}
@Test
public void selectAllStudentsTest(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao mapper = sqlSession.getMapper(StudentDao.class);
List<Student> studentList = mapper.selectAllStudents();
for (Student stu:studentList){
System.out.println(stu);
}
}
}
数据库数据
结果展示
1.返回值是Map类型
2. resultMap:结果映射, 指定列名和java对象的属性对应关系。resultMap和resultType不要一起用,二选一。