IBatis的resultMap使用

  1. 云栖社区>
  2. 博客>
  3. 正文

IBatis的resultMap使用

技术小美 2017-11-12 17:08:00 浏览496
展开阅读全文




1.创建Maven项目,项目名称ibatisdemo,目录结构如图所示

wKioL1jxxmGhyaMDAAAsfY6cVNw312.png-wh_50


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),如图所示

wKioL1jxx4uj-4CaAAA_Hn2COMA112.png-wh_50


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文件,如图所示

wKiom1jxx6XSdO0fAABBiG7KX_8999.png-wh_50


6.在src/main/resources下创建SqlMap.properties属性文件,如图所示

wKioL1jxx_rjWbeKAAA1kji7vws923.png-wh_50


7.SqlMap.properties属性文件内容如下

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=


8.在src/main/resources下创建SqlMapConfig.xml配置文件,如图所示

wKioL1jxyFfAN_NWAAA7MeVytcM493.png-wh_50


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,如图所示

wKioL1jxyMXh-WxdAAA79BEU38E756.png-wh_50


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,如图所示

wKioL1jxyTCgQ0zCAAA7YQdgXbw567.png-wh_50


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,如图所示

wKiom1jxyZjRJ9n3AAA8FYUVI88673.png-wh_50


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);
	}

}

wKiom1jxyc3jgerpAABwCKMO5XE886.png-wh_50




本文转自 素颜猪 51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1916265

网友评论

登录后评论
0/500
评论
技术小美
+ 关注