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++,利用链式栈实现括号匹配,界面友好,操作方便,运行流畅
#include<iostream> #include<string> using namespace std; struct Node { char ch; Node* ne ...
- VS2013自动注释插件
在程序编写的时候,你是否见过这种写法?整个项目每个cs文件头部都包含一个,版权,版本等信息的注释头? 类似这个类文件: /*************************************** ...
- V-rep学习笔记:曲柄摇杆机构
在ADAMS中创建一个曲柄摇杆机构很方便,但是V-rep中建模就比较麻烦.下面将自己在V-rep中建立曲柄摇杆机构模型的过程记录下来(由于对V-rep不是很熟,可能会有一些错误,只能等以后发现了再改进 ...
- linux sudo apt-get用法详解
APT的使用(Ubuntu Linux软件包管理工具一)apt-cache search # ------(package 搜索包)apt-cache show #------(package 获取包 ...
- 参数(条件表)灵活配置GS01/GS02/GS03
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [SAP ABAP开发技术总结]ABAP常用事务码
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CUBRID学习笔记 9 创建示例数据库
如果你安装的时候没有装数据库,可以后期装 其实和上文基本差不多. cd ~ mkdir CUBRID_databases cd CUBRID_databases mkdir demodb cd dem ...
- python_way day18 html-day4, Django路由,(正则匹配页码,包含自开发分页功能), 模板, Model(jDango-ORM) : SQLite,数据库时间字段插入的方法
python_way day18 html-day4 1.Django-路由系统 - 自开发分页功能 2.模板语言:之母板的使用 3.SQLite:model(jDango-ORM) 数据库时间字 ...
- maven实现依赖的“全局排除”
大多数java应用源码构建和依赖管理是使用maven来实现的,maven也是java构建和依赖管理的事实上的标准.我们的应用系统也都是基于maven构建的,maven虽然在依赖管理方面确实很牛叉,但是 ...
- SQL SERVER赋权限
--创建登录账户 use master GO EXEC sp_addlogin 'jacky', 'pwd' --EXEC sp_droplogin 'jacky' --删除登陆账户 use Test ...