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. CodeForces 151B Phone Numbers

     Phone Numbers Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Sub ...

  2. 终端、shell、bash的区别联系

    最佳答案 终端,即所谓的命令行界面,又称命令终端,用户输入shell命令用的窗口,跟Windows里的DOS界面差不多. shell,Shell就是用户和操作系统之间的壳,中介,GUI和CLI都算是S ...

  3. U盘文件后缀变成.exe怎么办?

    现在U盘病毒90%都是kido病毒,中了kido病毒的U盘文件名会变成可执行文件,后缀带有exe,虽然杀毒软件可以杀掉,但都是没办法处理它的母体,由于此类文件并没有丢失,而是被隐藏了,因此我们可以靠手 ...

  4. 线程入门之start()和run()的区别

    package com.thread; /** * start()和run()的区别 * start():并行执行 * run():方法调用,顺序执行 * @author 95Yang */ publ ...

  5. springaop实现登陆验证

    1.首先配置好springmvc和springaop 2.先写好登陆方法,通过注解写代理方法 通过代理获得登陆方法的参数方法名,然后再aop代理方法内进行登陆验证 贴出代码 package com.h ...

  6. U盘安装XP_sp3

    1. 用的是老毛桃的 U盘制作工具(百度云OsSkill --> 全部文件 --> 软件安装包 --> 老毛桃 --> Install_LMT_v9_2__Win_x86.ex ...

  7. 学习笔记day5:inline inline-block block区别

    1. block元素可以包含block元素和inline元素:但inline元素只能包含inline元素.要注意的是这个是个大概的说法,每个特定的元素能包含的元素也是特定的,所以具体到个别元素上,这条 ...

  8. (转)Web自动化测试中的接口测试

    1.背景 1.1 Web程序中的接口 1.1.1 典型的Web设计架构 web是实现了基于网络通信的浏览器客户端与远程服务器进行交互的应用,通常包括两部分:web服务器和web客户端.web客户端的应 ...

  9. Object Pascal 方法与技巧

    4 方法与技巧 4.1 设置代码模板 代码模板是Delphi 的代码感知特性的一种,通过它可以快速.高效和正确地输入代码.代码模板将一些常用的语句块保存在模板中,然后程序员只要在代码编辑器中按下“Ct ...

  10. D3.js 第一个程序 HelloWorld

    一.HTML 是怎么输出 HelloWorld 的 <html> <head> <meta charset="utf-8"> <title ...