Category and article bidirectional one-to-many association
(1) Implementation of the CategoryDao
public class Categorydaoimpl extends basedao<category> implements CategoryDao {
@Override
public boolean addcategory (category category) {
try {
Super.add (category);
return true;
} catch (Exception e) {
E.printstacktrace ();
return false;
}
}
@Override
Public Category Getcategorybyid (int id) {
return Super.getbyid (ID);
}
@Override
Public list<category> Getallcategorys () {
return Super.getall ();
}
}
(2) Implementation of the Articledao
public class Articledaoimpl extends basedao<article> implements Articledao {
@Override
public Boolean addarticle (article article) {
try {
Super.add (article);
return true;
} catch (Exception e) {
E.printstacktrace ();
return false;
}
}
@Override
Public article Getarticlebyid (int id) {
return Super.getbyid (ID);
}
@Override
Public list<article> Getallarticles () {
return Super.getall ();
}
@Override
Public list<article> getarticelsbycategory (int categoryId) {
map<string,object> params = new hashmap<string,object> ();
Params.put ("CID", categoryId);
Return Super.getlist ("Selectbycategory", params);
}
@Override
Public list<article> Getarticlesbytitle (String title) {
map<string,object> params = new hashmap<string,object> ();
Params.put ("title", title);
Return Super.getlist ("Selectbytitle", params);
}
}
》》》》》》》》》》》》》》》》》》》》》
Because article has a many-to-one association with user and category, foreign key additions can be problematic when adding data
Therefore, before making the mapping file, the article entity class is modified to add the corresponding attribute of the table foreign key column to the entity class.
Many-to-one references
Private category category;
private int Cateid;
Many-to-one references
private user user;
private int userId;
-------Note the above modification of the entity class, adding two foreign key-based property settings (Userid,cateid)
public void Setcategory (category category) {
This.setcateid (Category.getid ());
this.category = category;
}
public void SetUser (user user) {
This.setuserid (User.getid ());
This.user = user;
}
--------------Note that the above code is modified to add two original property set methods to the red code
======================================
(3) Category mapping file
<?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.Category" >
<resultmap type= "Category" id= "Categoryresult" >
<id column= "CID" property= "id"/>
<result column= "Cate_name" property= "Catename"/>
<!--a one-to-many association settings-
<collection property= "Articels" oftype= "article" javatype= "Java.util.Set" >
<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"/>
</collection>
</resultMap>
<insert id= "Insertpojo" parametertype= "Category" usegeneratedkeys= "true" >
Insert into Blog_category (cate_name) VALUES (#{catename})
</insert>
<select id= "Selectbyid" resultmap= "Categoryresult" parametertype= "int" >
Select
Tc.id as CID,
Tc.cate_name,
Ta.id as aid,
Ta.art_title,
Ta.art_content,
Ta.art_pubtime,
TA.PUB_USER_ID,
ta.cate_id
From Blog_category as TC inner join blog_article as Ta
On tc.id=ta.cate_id
Where Tc.id=#{id}
</select>
<select id= "SelectAll" resultmap= "Categoryresult" >
Select
Tc.id as CID,
Tc.cate_name,
Ta.id as aid,
Ta.art_title,
Ta.art_content,
Ta.art_pubtime,
TA.PUB_USER_ID,
ta.cate_id
From Blog_category as TC inner join blog_article as Ta
On tc.id=ta.cate_id
</select>
</mapper>
(4) Article mapping file
<?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"/>
<!--Many-to-one settings (article and user)-
<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>
<!--Many-to-one settings (article and category)--
<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},#{userid},#{cateid})
<selectkey >
</selectKey>
</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>
=========================
(5) Test Article Cascade add operation
public class Articledaoaddtest {
@Test
public void Test () {
Articledao Articledao = new Articledaoimpl ();
CategoryDao CategoryDao = new Categorydaoimpl ();
Article Article = new article (); Create a new article object that will be added
Article.settitle ("Today is a good day to write Java code!") "); Specify title
Calendar calendar = calendar.getinstance ();//Create Date Object
Article.setpubtime (Calendar.gettime ());//Set the publish time to the current time
Category category = Categorydao.getcategorybyid (1); Query a specified category
article.setcategory (category); Sets the specified classification to the article object (specifies the classification for the article)
User user = new user (); Create a user
User.setid (4); Specifies that the user is a user with an ID of 4 in the database table
article.setuser (user); Specify the publishing user for the article
article.setcontent ("haha haha ... "); Set the content of an article
Boolean doflag = articledao.addarticle (article); Add an article
assert.asserttrue (doflag);
}
(6) test the category and article one-to-many correlation is useful
public class Categorydaoselecttest {
@Test
public void Test () {
CategoryDao CategoryDao = new Categorydaoimpl ();
Category category = Categorydao.getcategorybyid (1);
int articlecount = Category.getarticels (). Size ();
Because of the correlation, a category contains multiple articles (article)
For (article Article:category.getArticels ()) {
System.out.println (Article.gettitle ());
}
Assert.assertequals (Articlecount, 5);
}
}
Mybatis Learning-4