Mybatis 学习-3
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的更多相关文章
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作(转载)
本文转载自:http://www.cnblogs.com/jpf-java/p/6013540.html 上一篇博文MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybati ...
- MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合(转载)
孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(八)--Mybatis3.x与Spring4.x整合 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: m ...
- MyBatis学习总结(七)——Mybatis缓存(转载)
孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(七)--Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的 ...
- (原创)mybatis学习二,spring和mybatis的融合
mybatis学习一夯实基础 上文介绍了mybatis的相关知识,这一节主要来介绍mybaits和spring的融合 一,环境搭建 1,jar包下载,下载路径为jar包 2,将包导入到java工程中 ...
- (原创)mybatis学习一,夯实基础
一,what?(是什么) MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可 ...
- MyBatis学习--简单的增删改查
jdbc程序 在学习MyBatis的时候先简单了解下JDBC编程的方式,我们以一个简单的查询为例,使用JDBC编程,如下: Public static void main(String[] args) ...
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...
- 【Todo】Mybatis学习-偏理论
之前写过好几篇Mybatis相关的文章: http://www.cnblogs.com/charlesblc/p/5906431.html <SSM(SpringMVC+Spring+Myba ...
- MyBatis学习系列三——结合Spring
目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBat ...
- MyBatis学习系列二——增删改查
目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...
随机推荐
- 【转载】C++内存分配
原文:C++内存分配 内存泄露相信对C++程序员来说都不陌生.解决内存泄露的方案多种多样,大部分方案以追踪检测为主,这种方法实现起来容易,使用方便,也比较安全. 首先我们要确定这个模块的主要功能: 能 ...
- matplotlib
前导: 安装 numpy http://sourceforge.net/projects/numpy/files/ http://sourceforge.net/projects/numpy/file ...
- Django忘记管理员账号和密码的解决办法
看着Django的教程学习搭建网站,结果忘记第一次创建的账号和密码了.结果搭建成功以后,一直无法登陆到管理页面,进行不下去了. 如图所示: 在网上找了很多的方法都不行,最后使用新建一个superuse ...
- 格式化时间(SimpleDateFormat)
import java.text.SimpleDateFormat; import java.util.Date; public class Main{ public static void main ...
- FJNU 1153 Fat Brother And XOR(胖哥与异或)
FJNU 1153 Fat Brother And XOR(胖哥与异或) Time Limit: 1000MS Memory Limit: 257792K [Description] [题目描述] ...
- Python基础学习笔记(十三)异常
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-exceptions.html Python用异常对象(excep ...
- Android中的文件权限操作
默认本工程创建的文件本工程对其有读写权限. 我们可以通过context.openFileOutput("文件名", 模式): 我们可以创建私有, 共有, 只读, 只写文件, 默认的 ...
- ubuntu12.04 gdb安装使用
参考文档:http://blog.csdn.net/haoel/article/details/2879 http://www.programlife.net/gdb-manual.html [新手笔 ...
- 改变bootstarp图标水平方向
我一开始是以为bootstarp已经自定义了方向的类的,但是我查阅了好久都没有看到,我这里用的是CSS3的旋转180° 1:HTML <i class="icon-thumbs-dow ...
- iOS - OC NSNumber 数字
前言 @interface NSNumber : NSValue @interface NSDecimalNumber : NSNumber 将基本数据类型包装成 OC 对象 1.NSNumber 与 ...