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. 目前比较全的CSS重设(reset)方法总结

    在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是 重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览器都能够理解和适用多有CSS规则,并且呈现相同的 ...

  2. [51NOD1405] 树的距离之和(树DP)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1405 (1)我们给树规定一个根.假设所有节点编号是0-(n-1 ...

  3. Sql 行转列问题总结

    行转列问题总结 1.行转列 ---1.最简单的行转列/* 问题:假设有张学生成绩表(tb)如下:姓名 课程 分数张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物 ...

  4. ecnu1624求交集多边形面积

    链接 本来在刷hdu的一道题..一直没过,看到谈论区发现有凹的,我这种方法只能过凸多边形的相交面积.. 就找来这道题试下水. 两个凸多边形相交的部分要么没有 要么也是凸多边形,那就可以把这部分单独拿出 ...

  5. XMLHttpRequest对象进行Ajax操作

    XMLHttpRequest 对象的三个常用的属性: 1. onreadystatechange 属性  onreadystatechange 属性存有处理服务器响应的函数. 请求状态改变的事件触发器 ...

  6. heaters

    https://leetcode.com/problems/heaters/ 开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过.原因是heater会有重复情况,这时候对于飘红部分就不会往前 ...

  7. [css] vertical-align和line-height

    原文链接:http://www.zhangxinxu.com/wordpress/2015/08/css-deep-understand-vertical-align-and-line-height/ ...

  8. 青云的机房组网方案(简单+普通+困难)(虚树+树形DP+容斥)

    题目链接 1.对于简单的版本n<=500, ai<=50 直接暴力枚举两个点x,y,dfs求x与y的距离. 2.对于普通难度n<=10000,ai<=500 普通难度解法挺多 ...

  9. eclipse里maven install时,报错提示jdk为无效的目标版本:1.7

    http://blog.csdn.net/wabiaozia/article/details/51733372 ************************************ 报错提示: [ ...

  10. android,JNI创建进程,使用fork()

    long add(long x,long y) { pid_t fpid; //fpid表示fork函数返回的值 int count=0; fpid=fork(); if (fpid < 0) ...