码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis 学习-3

时间:2016-01-27 22:58:04      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/MrzhangKk/p/5164657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!