IBatis的resultMap使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IBatis的resultMap使用相关的知识,希望对你有一定的参考价值。
1.创建Maven项目,项目名称ibatisdemo,目录结构如图所示
2.pom.xml内容如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany</groupId> <artifactId>ibatisdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <build/> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-sqlmap --> <dependency> <groupId>org.apache.ibatis</groupId> <artifactId>ibatis-sqlmap</artifactId> <version>2.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> </project>
3.在src/main/java下创建实体类Student,包名(com.mycompany.entity),如图所示
4.实体类Student的内容如下
package com.mycompany.entity; public class Student { private int sid; private String name; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return "id="+sid + "name="+name; } }
5.在src/main/java下创建实体类Student的Student.xml文件,如图所示
6.在src/main/resources下创建SqlMap.properties属性文件,如图所示
7.SqlMap.properties属性文件内容如下
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password=
8.在src/main/resources下创建SqlMapConfig.xml配置文件,如图所示
9.SqlMapConfig.xml配置文件的内容如下
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <properties resource="SqlMap.properties"/> <transactionManager type="JDBC"> <dataSource type="simple"> <property name="JDBC.Driver" value="${driver}"/> <property name="JDBC.ConnectionURL" value="${url}"/> <property name="JDBC.Username" value="${username}"/> <property name="JDBC.Password" value="${password}"/> </dataSource> </transactionManager> <sqlMap resource="com/mycompany/entity/Student.xml"/> </sqlMapConfig>
10.在src/main/java下创建接口StudentDao,如图所示
11.接口StudentDao的内容如下
package com.mycompany.dao; import com.mycompany.entity.Student; public interface StudentDao { /** * 通过resultMap获取学生信息 * @param sid * @return */ public Student getStudentById(int sid); }
12.在src/main/java下创建接口StudentDao的实现类StudentDaoImpl,如图所示
13.接口StudentDao的实现类StudentDaoImpl的内容如下
package com.mycompany.dao.impl; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import com.mycompany.dao.StudentDao; import com.mycompany.entity.Student; public class StudentDaoImpl implements StudentDao { private static SqlMapClient sqlMapClient = null; static{ try { Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 查询学生信息 */ public Student getStudentById(int sid) { Student student = null; try { student = (Student) sqlMapClient.queryForObject("getStudentById",sid); } catch (SQLException e) { e.printStackTrace(); } return student; } }
14.在src/test/java下创建测试类StudentTest,如图所示
15.测试类StudentTest的内容如下
package com.mycompany.dao.impl; import com.mycompany.entity.Student; public class StudentTest { /** * @param args */ public static void main(String[] args) { StudentDaoImpl studentDaoImpl = new StudentDaoImpl(); //查询学生信息 Student student5 = studentDaoImpl.getStudentById(5); System.out.println(student5); } }
本文出自 “素颜” 博客,谢绝转载!
以上是关于IBatis的resultMap使用的主要内容,如果未能解决你的问题,请参考以下文章
ibatis/mybatis属性三:resultMap和resultClass/resultType
ibatis中的resultClass,parameterClass,resultMap,resultType的使用与区别