1.设计Dao接口

public interface UserDao {

  public boolean addUser(User user);

}

public interface CategoryDao {

  public boolean addCategory(Category category);

  public Category getCategoryById(int id);

  public List<Category> getAllCategorys();

}

public interface ArticleDao {

  public boolean addArticle(Article article);

  public Article getArticleById(int id);

  public List<Article> getAllArticles();

  public List<Article> getArticelsByCategory(int categoryId);

  public List<Article> getArticlesByTitle(String title);

}

2. 对接口进行实现

public class UserDaoImpl extends BaseDao<User> implements UserDao {

@Override

public boolean addUser(User user) {

  try {

    super.add(user);

    return true;

  } catch (Exception e) {

      e.printStackTrace();

      return false;

  }

 }

}

-------》针对这个配置做映射文件(别忘了在核心配置文件中为实体类添加别名【mybatis.cfg.xml】)

<typeAliases>

<typeAlias alias="User" type="cn.smartapp.blogs.pojo.User"/>

<typeAlias alias="Category" type="cn.smartapp.blogs.pojo.Category "/>

<typeAlias alias="Article" type="cn.smartapp.blogs.pojo.Article"/>

</typeAliases>

User实体类的映射文件

<?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="cn.smartapp.blogs.pojo.User">

<insert id="insertPojo" parameterType="User" useGeneratedKeys="true">

insert into blog_user(user_name,user_pass,nick_name) values(#{userName},#{password},#{nickName})

</insert>

</mapper>

****千万别忘记映射文件写好后,要在核心配置文件中加入映射

<mappers>

<mapper resource="cn/smartapp/blogs/pojo/User.xml"/>

<mapper resource="cn/smartapp/blogs/pojo/Category.xml"/>

<mapper resource="cn/smartapp/blogs/pojo/Article.xml"/>

</mappers>

2.做一个添加User的测试

public class UserDaoAddTest

@Test

public void test() {

UserDao userDao = new UserDaoImpl();

User user = new User();

user.setUserName("mingming");

user.setPassword("123123");

user.setNickName("明明");

boolean doFlag = userDao.addUser(user);

Assert.assertTrue(doFlag);

}

}

3.实现Article一对多双向关联(User,Category)

(1)多对一关联,因为Article是从表,所以Article是多的一方,从这边关联

-》把ArticleDao实现掉(代码就略了,和前面一样)

<?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="cn.smartapp.blogs.pojo.Article">

<resultMap type="Article" id="ArticleResult">

<id column="aid" property="id" />

<result column="art_title" property="title" />

<result column="art_content" property="content" />

<result column="art_pubtime" property="pubTime" javaType="java.util.Date"/>

<association property="User" foreignColumn="pub_user_id">

<id column="uid" property="id" />

<result column="user_name" property="userName" />

<result column="user_pass" property="password" />

<result column="nick_name" property="nickName" />

</association>

<association property="Category" foreignColumn="cate_id">

<id column="cid" property="id" />

<result column="cate_name" property="cateName" />

</association>

</resultMap>

<insert id="insertPojo" parameterType="Article" useGeneratedKeys="true">

insert into blog_article(art_title,art_content,art_pubtime,pub_user_id,cate_id)

values(#{title},#{content},#{pubTime},#{user.id}),#{category.id})

</insert>

<select id="SelectById" resultMap="ArticleResult" parameterType="int">

select

ta.id as aid,

ta.art_title,

ta.art_content,

ta.art_pubtime,

ta.pub_user_id,

ta.cate_id,

tu.id as uid,

tu.user_name,

tu.user_pass,

tu.nick_name,

tc.id as cid,

tc.cate_name

from blog_user as tu inner join blog_article as ta on tu.id=ta.pub_user_id

inner join blog_category as tc on tc.id=ta.cate_id where ta.id=#{id}

</select>

<select id="SelectAll" resultMap="ArticleResult">

select

ta.id as aid,

ta.art_title,

ta.art_content,

ta.art_pubtime,

ta.pub_user_id,

ta.cate_id,

tu.id as uid,

tu.user_name,

tu.user_pass,

tu.nick_name,

tc.id as cid,

tc.cate_name

from blog_user as tu inner join blog_article as ta on tu.id=ta.pub_user_id

inner join blog_category as tc on tc.id=ta.cate_id

</select>

<select id="SelectByCategory" resultMap="ArticleResult">

select

ta.id as aid,

ta.art_title,

ta.art_content,

ta.art_pubtime,

ta.pub_user_id,

ta.cate_id,

tu.id as uid,

tu.user_name,

tu.user_pass,

tu.nick_name,

tc.id as cid,

tc.cate_name

from blog_user as tu inner join blog_article as ta on tu.id=ta.pub_user_id

inner join blog_category as tc on tc.id=ta.cate_id where tc.id=#{cid}

</select>

<select id="SelectByTitle" resultMap="ArticleResult">

select

ta.id as aid,

ta.art_title,

ta.art_content,

ta.art_pubtime,

ta.pub_user_id,

ta.cate_id,

tu.id as uid,

tu.user_name,

tu.user_pass,

tu.nick_name,

tc.id as cid,

tc.cate_name

from blog_user as tu inner join blog_article as ta on tu.id=ta.pub_user_id

inner join blog_category as tc on tc.id=ta.cate_id where ta.title like #{title}

</select>

</mapper>

Mybatis 学习-3的更多相关文章

  1. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作(转载)

    本文转载自:http://www.cnblogs.com/jpf-java/p/6013540.html 上一篇博文MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybati ...

  2. MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合(转载)

      孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(八)--Mybatis3.x与Spring4.x整合 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: m ...

  3. MyBatis学习总结(七)——Mybatis缓存(转载)

      孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(七)--Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的 ...

  4. (原创)mybatis学习二,spring和mybatis的融合

    mybatis学习一夯实基础 上文介绍了mybatis的相关知识,这一节主要来介绍mybaits和spring的融合 一,环境搭建 1,jar包下载,下载路径为jar包 2,将包导入到java工程中 ...

  5. (原创)mybatis学习一,夯实基础

    一,what?(是什么) MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可 ...

  6. MyBatis学习--简单的增删改查

    jdbc程序 在学习MyBatis的时候先简单了解下JDBC编程的方式,我们以一个简单的查询为例,使用JDBC编程,如下: Public static void main(String[] args) ...

  7. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...

  8. 【Todo】Mybatis学习-偏理论

    之前写过好几篇Mybatis相关的文章: http://www.cnblogs.com/charlesblc/p/5906431.html  <SSM(SpringMVC+Spring+Myba ...

  9. MyBatis学习系列三——结合Spring

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBat ...

  10. MyBatis学习系列二——增删改查

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...

随机推荐

  1. C++实现二叉树,运用模板,界面友好,操作方便 运行流畅

    //.h文件 #ifndef TREE_H #define TREE_H #include<iostream> #include<iomanip> using namespac ...

  2. 25 个增强iOS应用程序性能的提示和技巧 应用程序性能的提示和技巧

    初级 在开发过程中,下面这些初级技巧需要时刻注意: 1.使用ARC进行内存管理2.在适当的情况下使用reuseIdentifier3.尽可能将View设置为不透明(Opaque)4.避免臃肿的XIBs ...

  3. Create Timer Example To Show Image Presentation in Oracle Forms

    Suppose you want to change multiple images after a specified time in home screen of your oracle form ...

  4. vs无法打开项目的解决方案

    错误提示: “未找到与约束 ContractName Microsoft.Internal.VisualStudio.PlatformUI.ISolutionAttachedCollectionSer ...

  5. 线程入门之join方法

    package com.thread; /** * <join:将某线程加入进来,相当于方法调用,也叫合并某个线程> * <功能详细描述> * * @author 95Yang ...

  6. iOS - Swift NSEnumerator 迭代器

    前言 public class NSEnumerator : NSObject, NSFastEnumeration 1.迭代器 let arr:NSArray = ["bei", ...

  7. Android播放视频

    package cn.c;   import java.io.File;   import java.io.IOException;   import android.app.Activity;   ...

  8. C++——友元、异常和其他

    一.友元 类并非只能拥有友元函数,也可以将类作为友元.在这种情况下,友元类的所有方法都可以访问原始类的私有成员和保护成员.另外,也可以做更严格的限制,只将特定的成员函数指定为另一个类的友元.哪些函数. ...

  9. ios中javascript直接调用oc代码而非通过改变url回调方式(转)

    之前一个ios项目中,需要通过UIWebview来打开一个静态页面,并在静态页面中 调用相关object-c代码. 一.以前使用js调用object-c的方法 关于如何使用javascript调用ob ...

  10. linux 后台执行命令

    本文主要内容: 1. 设置ctontab文件,并用它来提交作业. 2. 使用at命令来提交作业. 3. 在后台提交作业. 4. 使用nohup命令提交作业. 名词解释: cron 系统调度进程.可以使 ...