Bootstrap

MyBatis普通查询与一对一映射查询

文档结构:



src/config

ibatisConfiguration.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>
	//类型别名设置
	<typeAliases>
		<typeAlias alias="Article" type="dao.domain.Article"/>
		<typeAlias alias="Classtype" type="dao.domain.Classtype"/>
	</typeAliases>
	
	//数据源配置
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/manage?characterEncoding=UTF-8"/>
				<property name="username" value="root"/>
				<property name="password" value=""/>
			</dataSource>
		</environment>
	</environments>
	
	//映射表
	<mappers>
		<mapper resource="config/ArticleMapper.xml" />
		<mapper resource="config/ClassTypeMapper.xml" />
	</mappers>
</configuration>

ArticleMapper.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="dao.ArticleDAO" >
	//sql标签,用于标记常用的字符. 
	<sql id="articleFileds">article_id,title,author,classid</sql>
	
	//parameterType,参数类型
	//resultType。返回类型
	//id。唯一值,调用时使用。
	<select id="getArticleById" resultType="Article" parameterType="int">
		//include标签,复用sql标签
		select <include refid="articleFileds"/>
		from article
		where
			article_id = #{id}
	</select>
	
	//mybatis中最关键的标签:resultMap
	<resultMap id="ArticleAndClassType" type="Article">
		<id property="article_id" column="article_id" />
		<result property="title" column="title" />
		<result property="author" column="author" />
		<result property="classid" column="classid" />
		
		//一对一映射的关键:association
		//select:执行dao.ClassTypeDAO.getClassTypeById方法,并将返回值设置到peoperty.来完成一对一映射
		<association property="classtype" column="classid" select="dao.ClassTypeDAO.getClassTypeById"/>
	</resultMap>
	
	//resultMap:通过此值指向resultMap,可以进行复杂的处理
	<select id="getArticleAndClassTypeById" resultMap="ArticleAndClassType" parameterType="int">
		select <include refid="articleFileds"/>
		from article
		where
			article_id = #{id}
	</select>
</mapper>

ClassTypeMapper.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="dao.ClassTypeDAO" >
	<sql id="classtypeFileds">classid, classname</sql>
	
	<select id="getClassTypeById" parameterType="int" resultType="Classtype">
		select <include refid="classtypeFileds"/>
		from classtype
		where
			classid = #{id}
	</select>
</mapper>

/src/dao/domain

Article.java

package dao.domain;

public class Article {
	public int article_id;
	public String title;
	public String author;
	public int classid;
	public Classtype classtype;

	public Classtype getClasstype() {
		return classtype;
	}
	public void setClasstype(Classtype classtype) {
		this.classtype = classtype;
	}
	public int getArticle_id() {
		return article_id;
	}
	public void setArticle_id(int article_id) {
		this.article_id = article_id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public int getClassid() {
		return classid;
	}
	public void setClassid(int classid) {
		this.classid = classid;
	}
	@Override
	public String toString() {
		return "Article [article_id=" + article_id + ", title=" + title
				+ ", author=" + author + ", classid=" + classid
				+ ", classtype=" + classtype + "]";
	}
}

Classtype.java

package dao.domain;

public class Classtype {
	public int classid;
	public String classname;
	
	public int getClassid() {
		return classid;
	}
	public void setClassid(int classid) {
		this.classid = classid;
	}
	public String getClassname() {
		return classname;
	}
	public void setClassname(String classname) {
		this.classname = classname;
	}
	@Override
	public String toString() {
		return "Classtype [classid=" + classid + ", classname=" + classname
				+ "]";
	}
}

/src/dao

ArticleDAO.java

package dao;

import java.util.HashMap;

import dao.domain.Article;

public interface ArticleDAO {
	public Article getArticleById(int id);
	
	public Article getArticleAndClassTypeById(int id);
}

ClassTypeDAO.java

package dao;

import dao.domain.Classtype;

public interface ClassTypeDAO {
	public Classtype getClassTypeById(int id);
}

/src/dao/impl

ArticleDAOImpl.java

package dao.impl;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;

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 dao.ArticleDAO;
import dao.domain.Article;

public class ArticleDAOImpl implements ArticleDAO {
	private static SqlSession session = null;
	private static ArticleDAO mapper = null;
	
	static{
		String resouce = "config/ibatisConfiguration.xml";
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(resouce);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
		session = factory.openSession();
		mapper = session.getMapper(ArticleDAO.class);
		
		try {
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Article getArticleById(int id) {
		return mapper.getArticleById(id);
	}

	@Override
	public Article getArticleAndClassTypeById(int id) {
		return mapper.getArticleAndClassTypeById(id);
	}

}

ClassTypeDAOImpl.java

package dao.impl;

import java.io.IOException;
import java.io.Reader;

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 dao.ClassTypeDAO;
import dao.domain.Classtype;

public class ClassTypeDAOImple implements ClassTypeDAO {
	private static SqlSession session = null;
	private static ClassTypeDAO mapper = null;
	
	static{
		String resouce = "config/ibatisConfiguration.xml";
		Reader reader = null;
		
		try {
			reader = Resources.getResourceAsReader(resouce);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
		session = factory.openSession();
		
		mapper = session.getMapper(ClassTypeDAO.class);
		
		try {
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Classtype getClassTypeById(int id) {
		return mapper.getClassTypeById(id);
	}

}

/src/test

test.java

package test;

import dao.ArticleDAO;
import dao.ClassTypeDAO;
import dao.impl.ArticleDAOImpl;
import dao.impl.ClassTypeDAOImple;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ArticleDAO dao = new ArticleDAOImpl();
		
		System.out.println(dao.getArticleById(3));
		
		System.out.println(dao.getArticleAndClassTypeById(2));
		
		ClassTypeDAO classtypeDAO = new ClassTypeDAOImple();
		
		System.out.println(classtypeDAO.getClassTypeById(1));
	}

}

运行结果:

Article [article_id=3, title=title, author=test, classid=1, classtype=null]
Article [article_id=2, title=title, author=test, classid=1, classtype=Classtype [classid=1, classname=test]]
Classtype [classid=1, classname=test]

;