1. Designing the DAO Interface
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. Implement the interface
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;
}
}
}
-------The mapping file for this configuration (don't forget to add an alias "Mybatis.cfg.xml" to the entity class in the core configuration file)
<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>
Mapping file for User entity class
<?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>
Do not forget to add mappings to the core configuration file after the mapping file has been written.
<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. Do a test that adds a user
public class Userdaoaddtest
@Test
public void Test () {
Userdao Userdao = new Userdaoimpl ();
User user = new user ();
User.setusername ("mingming");
User.setpassword ("123123");
User.setnickname ("Ming");
Boolean doflag = userdao.adduser (user);
Assert.asserttrue (Doflag);
}
}
3. Implement article one-to-many bidirectional association (user,category)
(1) many to one association, because article is from the table, so article is more of a party, from this side associated
-"Implement Articledao (the code is slightly, as before)
<?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 joins 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 joins 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 joins 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 joins 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 Learning-3