Document structure:
Src/config
Ibatisconfiguration. xml:
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype configuration public "-// mybatis.org//dtd config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> // type alias Settings <typealiases> <typealias alias =" article "type =" Dao. domain. article "/> <typealias alias =" classtype "type =" Dao. domain. classtype "/> </typealiases> // data source configuration <environments default =" development "> <environment id =" development "> <transactionmanager type =" JDBC "/> <datasource type = "pool Ed "> <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> // ing table <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 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" Dao. articledao "> // SQL tag, used to mark common characters. <SQL id = "articlefileds"> article_id, title, author, classid </SQL> // parametertype, parameter type // resulttype. Return type // ID. Unique value, used for calling. <Select id = "getarticlebyid" resulttype = "article" parametertype = "int"> // include tag, reuse the SQL tag select <include refID = "articlefileds"/> from articlewherearticle_id =#{ ID} </SELECT> // The most critical tag in 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"/> // the key to one-to-one ing: association // select: Execute Dao. classtypedao. getclasstypebyid method, and set the return value to peoperty. <association property = "classtype" column = "classid" select = "Dao. classtypedao. getclasstypebyid "/> </resultmap> // resultmap: points to resultmap through this value, complex processing can be performed <select id = "getarticleandclasstypebyid" resultmap = "articleandclasstype" parametertype = "int"> select <include refID = "articlefileds"/> from articlewherearticle_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 classtypewhereclassid = #{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;}@Overridepublic 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;}@Overridepublic 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();}}@Overridepublic Article getArticleById(int id) {return mapper.getArticleById(id);}@Overridepublic 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();}}@Overridepublic 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));}}
Running result:
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]