文檔結(jié)構(gòu):
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>
- //類型別名設(shè)置
- <typeAliases>
- <typeAlias alias="Article" type="dao.domain.Article"/>
- <typeAlias alias="Classtype" type="dao.domain.Classtype"/>
- </typeAliases>
-
- //數(shù)據(jù)源配置
- <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標(biāo)簽,用于標(biāo)記常用的字符.
- <sql id="articleFileds">article_id,title,author,classid</sql>
-
- //parameterType,參數(shù)類型
- //resultType。返回類型
- //id。唯一值,調(diào)用時(shí)使用。
- <select id="getArticleById" resultType="Article" parameterType="int">
- //include標(biāo)簽,復(fù)用sql標(biāo)簽
- select <include refid="articleFileds"/>
- from article
- where
- article_id = #{id}
- </select>
-
- //mybatis中最關(guān)鍵的標(biāo)簽: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" />
-
- //一對一映射的關(guān)鍵:association
- //select:執(zhí)行dao.ClassTypeDAO.getClassTypeById方法,并將返回值設(shè)置到peoperty.來完成一對一映射
- <association property="classtype" column="classid" select="dao.ClassTypeDAO.getClassTypeById"/>
- </resultMap>
-
- //resultMap:通過此值指向resultMap,可以進(jìn)行復(fù)雜的處理
- <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));
- }
-
- }
運(yùn)行結(jié)果:
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]
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。