Home  >  Article  >  Java  >  Getting started with MyBatis (2) --- one-to-one, one-to-many

Getting started with MyBatis (2) --- one-to-one, one-to-many

黄舟
黄舟Original
2016-12-21 14:23:311209browse

1. Create a database table

1.1. Create a data table and insert data at the same time

/*SQLyog EnterPRise v12.09 (64 bit)MySQL - 5.6.27-log: Database - mybatis
******* *************************************************** *************//*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=''*/;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS= 0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_ NOTES= @@SQL_NOTES, SQL_NOTES=0 */;CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;USE `mybatis`;/*Table structure for table `author` * /DROP TABLE IF EXISTS `author`;CREATE TABLE `author` (
`author_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'author ID primary key',
`author_username` varchar(30) NOT NULL COMMENT 'author username' ,
`author_passWord` varchar(32) NOT NULL COMMENT 'Author's password',
`author_email` varchar(50) NOT NULL COMMENT 'Author's email',
`author_bio` varchar(1000) DEFAULT 'This guy is terrible, nothing Not left' COMMENT 'Author profile',
`register_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'Registration time', PRIMARY KEY (`author_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;/*Data for the table ` author` */

insert into `author`(`author_id`,`author_username`,`author_password`,`author_email`,`author_bio`,`register_time`) values ​​(1,'Zhang San','123456', '123@QQ.com','Zhang San is a newbie and just started to register','2015-10-29 10:23:59'),(2,'Li Si','123asf','lisi@163. com','hunmiannying','2015-10-29 10:24:29'),(3,'王五','dfsd342','ww@sina.com','Kangxi Dynasty','2015-10 -29 10:25:23'),(4,'Zhao Liu','123098sdfa','zhaoliu@qq.com','Hua Wugu','2015-10-29 10:26:09'), (5,'Qianqi','zxasqw','qianqi@qq.com','This guy is a scumbag and left nothing','2015-10-29 10:27:04'),(6, 'Zhang Sanfeng','123456','zhangsf@qq.com','This guy is very lazy and left nothing behind','2015-10-29 11:48:00'),(7,'Zhang Wuji', 'qwertyuiop','wuji@163.com','This guy is very bad, he left nothing','2015-10-29 11:48:24');

/*Table structure for table `blog` */DROP TABLE IF EXISTS `blog`;CREATE TABLE `blog` (
`blog_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'BlogId primary key',
`blog_title` varchar( 255) NOT NULL COMMENT 'blog title',
`author_id` int(11) unsigned NOT NULL COMMENT 'author ID foreign key', PRIMARY KEY (`blog_id`), KEY `fk_author_id` (`author_id`), CONSTRAINT `fk_author_id ` FOREIGN KEY (`author_id`) REFERENCES `author` (`author_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;/*Data for the table `blog` */insert into `blog`(`blog_id` ,`blog_title`,`author_id`) values ​​(1,'Xiao Zhang's Blog',1),(2,'Xiao Li',2),(3,'Wang Wu is not a human being',3),(4, 'Zhao Diren',4),(5,'Qianqianqian',5);/*Table structure for table `posts` */DROP TABLE IF EXISTS `posts`;CREATE TABLE `posts` (
`post_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Post primary key ID',
`post_subject` varchar(255) NOT NULL COMMENT 'Post subject, title',
`post_body` text NOT NULL COMMENT 'Post content maximum 3000 characters',
`blog_id` int(11) unsigned NOT NULL COMMENT 'Blog primary key as foreign key',
`createtime` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'Article creation time', PRIMARY KEY (`post_id`), KEY `fk_blog_id` (`blog_id` ), CONSTRAINT `fk_blog_id` FOREIGN KEY (`blog_id`) REFERENCES `blog` (`blog_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;/*Data for the table `posts` */insert into `posts `(`post_id`,`post_subject`,`post_body`,`blog_id`,`createtime`) values ​​(1,'Introduction to Mybatis 1','What is MyBatis? rnMyBatis is an excellent persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manual setting of parameters and retrieval of result sets. MyBatis can use simple xml or annotations for configuration and native Maps to map interfaces and Java POJOs (Plain Old Java Objects, ordinary Java objects) into records in the database. ',1,'2015-10-29 10:32:21'),(2,'Getting started with Mybatis 2','To use MyBatis, just place the mybatis-x.x.x.jar file in the classpath.',1 ,'2015-10-29 10:32:52'),(3,'Oracle Learning','Oracle Database, also known as Oracle RDBMS, or Oracle for short. It is a relational database management system of Oracle Company',2, '2015-10-29 10:33:26'),(4,'JAVA Learning 1','Java is the general name of the Java object-oriented programming language and Java platform launched by Sun Microsystems in May 1995', 3,'2015-10-29 10:34:17'),(5,'PL/SQL','PL/SQL is also a programming language called Procedural Language/SQL. PL/SQL It is an extension of Oracle database to SQL statements', 4, '2015-10-29 10:37:52'), (6, 'CSS tag selector', 'Tag selector rnID selector rn class selector rn special selection ',5,'2015-10-29 10:39:44'),(7,'javascript','js: is a front-end scripting language',2,'2015-10-29 10:40:18') ;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

2. Create the project

2.1. Create the project and add the jar package

Getting started with MyBatis (2) --- one-to-one, one-to-many


2.2. Create the entity class taking author as an example

package com.pb.mybatis.po;import java.util.Date;/**
*

* @Title: Author.java

* @Package com.pb.mybatis.po

* @ClassName Author

* @Description: TODO (Blog author class)

* @author Liu Nan

* @date 2015-10-29 9:27:53 AM

* @version V1.0*/public class Author {    //作者ID
   private int authorId;    
   //作者用户名
   private String authorUserName;    
   //作者密码
   private String authorPassword;    
   //作者邮箱
   private String authorEmail;    
   //作者介绍
   private int authorBio;    
   //注册时间
   private Date registerTime;    /**
    * @return the authorId    */
   public int getAuthorId() {        return authorId;
   }    /**
    * @param authorId the authorId to set    */
   public void setAuthorId(int authorId) {        this.authorId = authorId;
   }    /**
    * @return the authorUserName    */
   public String getAuthorUserName() {        return authorUserName;
   }    /**
    * @param authorUserName the authorUserName to set    */
   public void setAuthorUserName(String authorUserName) {        this.authorUserName = authorUserName;
   }    /**
    * @return the authorPassword    */
   public String getAuthorPassword() {        return authorPassword;
   }    /**
    * @param authorPassword the authorPassword to set    */
   public void setAuthorPassword(String authorPassword) {        this.authorPassword = authorPassword;
   }    /**
    * @return the authorEmail    */
   public String getAuthorEmail() {        return authorEmail;
   }    /**
    * @param authorEmail the authorEmail to set    */
   public void setAuthorEmail(String authorEmail) {        this.authorEmail = authorEmail;
   }    /**
    * @return the authorBio    */
   public int getAuthorBio() {        return authorBio;
   }    /**
    * @param authorBio the authorBio to set    */
   public void setAuthorBio(int authorBio) {        this.authorBio = authorBio;
   }    /**
    * @return the registerTime    */
   public Date getRegisterTime() {        return registerTime;
   }    /**
    * @param registerTime the registerTime to set    */
   public void setRegisterTime(Date registerTime) {        this.registerTime = registerTime;
   }    /**(non Javadoc)
   
    *

Title: toString


   
    *

Description:重写toString方法


   
    * @return
   
    * @see java.lang.Object#toString()    */
   @Override    public String toString() {        return "Author [authorId=" + authorId + ", authorUserName="
               + authorUserName + ", authorPassword=" + authorPassword                + ", authorEmail=" + authorEmail + ", authorBio=" + authorBio                + ", registerTime=" + registerTime + "]";
   }

   
   
   
}

 

 

2.3、创建mybatis配置文件

 

br/>  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
   
   
       
       
       
       
   

 

2.4、创建实体类对象的接口以author为例

 

/***/package com.pb.mybatis.mapper;import java.util.List;import com.pb.mybatis.po.Author;/**

* @Title: AuthorMapper.java

* @Package com.pb.mybatis.mapper

* @ClassName AuthorMapper

* @Description: TODO (author interface)

* @author Liu Nan

* @date 2015-10-29 11:13:10 AM

* @version V1.0

*/public interface AuthorMapper {    
   /**
*
* @Title: findById

* @Description: TODO (find a user based on it)

* @param id
* @return Author*/
   public Author findAuthorById(int authorId);    
   /**
*
* @Title: findByName

* @Description: TODO (fuzzy query based on user name)

* @param name
* @return List*/
   public List findAuthorByName(String name);    
   /**
*
* @Title: addAuthor

* @Description: TODO(add author)

* @param author
* @return int*/
   public int addAuthor(Author author);    
   /**
*
* @Title: updateAuthor

* @Description: TODO (modify user)

* @param authro
* @return int*/
   public int updateAuthor(Author authro);    
   /**
*
* @Title: deleteAturho

* @Description: TODO (delete author based on ID)

* @param id
* @return int*/
   public int deleteAuthor(int authorId);

}

 

 

2.5、创建接口对应的mapper.xm以author为例

 

br/> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis .org/dtd/mybatis-3-mapper.dtd">< ;resultMap type="Author" id="authorResultMap"> INSERT INTO author(author_username,author_password,author_email,author_bio,register_time)
VALUES(#{authorUserName},#{authorPassword},#{authorEmail},# {authorBio},#{registerTime})
update author
set
author_username=#{authorUserName },
author_password=#{authorPassword},
author_email=#{authorEmail},
author_bio=#{authorBio},
register_time=#{registerTime}
where author_id=#{authorId}
delete from author
where author_id=#{authorId}

Three. Simple implementation of addition, deletion, modification and check

3.1. Take the author as an example for the test class

/***/package com.pb.mybatis.mapper;import java.io.InputStream;import java.util.Date;import java.util.List;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Before;import org.junit.Test;import com.pb.mybatis.po.Author;/**

* @Title: AuthorMapperTest.java

* @Package com.pb.mybatis.mapper

* @ClassName AuthorMapperTest

* @Description: TODO (Test)

* @author Liu Nan

* @date 2015 -10-29 11:57:21 AM

* @version V1.0

*/public class AuthorMapperTest {    
   private SqlSessionFactory sqlSessionFactory;    
   /**
*
* @Title: setUp

* @Description: TODO (method executed before each method)

* @throws Exception void*/
   @Before    public void setUp() throws Exception {
       String resource="configuration.xml";
       InputStream in=Resources.getResourceAsStream(resource);        //获取会话工厂
       sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
   }    /**
*
* @Title: testFindAuthorById

* @Description: TODO (find a user based on it)
void*/
   @Test    public void testFindAuthorById() {        //获取会话
       SqlSession sqlSession=sqlSessionFactory.openSession();        //Mapper接口
       AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class);        //调用方法
       Author author=authorMapper.findAuthorById(2);
       System.out.println(author);        //关闭会话        sqlSession.close();
   }    /**
*
* @Title: testFindAuthorByName

* @Description: TODO (fuzzy query based on user name)
void*/
   @Test    public void testFindAuthorByName() {        //获取会话
               SqlSession sqlSession=sqlSessionFactory.openSession();                //Mapper接口
               AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class);                //调用方法
               List authors=authorMapper.findAuthorByName("张");
               System.out.println(authors);                //关闭会话                sqlSession.close();                for(Author a:authors){
                   System.out.println(a.toString());
               }
   }    /**
*
* @Title: testAddAuthor

* @Description: TODO(Add author)
void*/
   @Test    public void testAddAuthor() {        //获取会话
       SqlSession sqlSession=sqlSessionFactory.openSession();        //Mapper接口
       AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class);        //调用方法
       Author author=new Author();
       author.setAuthorUserName("不知道");
       author.setAuthorPassword("1234567890");
       author.setAuthorEmail("123456@qq.com");
       author.setAuthorBio("知道是个什么");
       author.setRegisterTime(new Date());        int num=authorMapper.addAuthor(author);
       System.out.println("num="+num);
       System.out.println("authorId="+author.getAuthorId());
sqlSession.commit();         //Close the session                                                                                                                                                                                             ‐                                                             to SqlSession sqlSession=sqlSessionFactory.openSession(); //Mapper API Author.setAuthorUserName("Got it");
Author.setAuthorPassword("456789 ");
                     author.setAuthorEmail("456789@qq.com"); int num=authorMapper.updateAuthor(author) )                                                                                                                                                                                          but sql sqlSession.close( ;
; //Mapper interface
AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper. class);             //Call method
       
            int num=authorMapper.deleteAuthor(10);
        System.out.println("num="+num); //Close session sqlSession.close( ;

package com.pb.mybatis.po;/**

* @Title: Blog.java

* @Package com.pb.mybatis.po

* @ClassName Blog

* @Description: TODO(Blog)

* @author Liu Nan

* @date 2015 -10-29 9:32:56 AM

* @version V1.0

*/public class Blog {    //博客ID
   private int blogId;    
   //标题
   private String blogTitle;    
   //博客作者
   private Author author;    /**
    * @return the blogId    */
   public int getBlogId() {        return blogId;
   }    /**
    * @param blogId the blogId to set    */
   public void setBlogId(int blogId) {        this.blogId = blogId;
   }    /**
    * @return the blogTitle    */
   public String getBlogTitle() {        return blogTitle;
   }    /**
    * @param blogTitle the blogTitle to set    */
   public void setBlogTitle(String blogTitle) {        this.blogTitle = blogTitle;
   }    /**
    * @return the author    */
   public Author getAuthor() {        return author;
   }    /**
    * @param author the author to set    */
   public void setAuthor(Author author) {        this.author = author;
   }    /**(non Javadoc)
   
    *

Title: toString


   
    *

Description: 重写toString方法


   
    * @return
   
    * @see java.lang.Object#toString()    */
   @Override    public String toString() {        return "Blog [blogId=" + blogId + ", blogTitle=" + blogTitle                + ", author=" + author + "]";
   }
   
   
}

 

4.2、建立BlogMapper接口

 

/***/package com.pb.mybatis.mapper;import java.util.List;import com.pb.mybatis.po.Author;import com.pb.mybatis.po.Blog;/**

* @Title: BlogMapper.java

* @Package com.pb.mybatis.mapper

* @ClassName BlogMapper

* @Description: TODO (describe what this file does in one sentence)

* @author Liu Nan

* @date 2015-10-29 11:13:21 AM

* @version V1.0

*/public interface BlogMapper {    /**
*
* @Title: findBlogById

* @Description: TODO (Find BLOG based on ID)

* @param id
* @return Blog*/
   public Blog findBlogById(int id);    
   /**
*
* @Title: findByName

* @Description: TODO (search based on blog name)

* @param name
* @return List*/
   public List findBlogByName(String blogTitle);    
   /**
*
* @Title: addBlog

* @Description: TODO(add blog)

* @param blog
* @return int*/
   public int addBlog(Blog blog);    
   
   /**
*
* @Title: updateBlog

* @Description: TODO (modify blog)

* @param blog
* @return int*/
   public int updateBlog(Blog blog);    
   /**
*
* @Title: deleteBlog

* @Description: TODO (delete blog)

* @param id
* @return int*/
   public int deleteBlog(int id);
}

 

4.3、建立mapper.xml

 

br/> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis .org/dtd/mybatis-3-mapper.dtd">< ;/resultMap>< ;id property="authorId" column="author_id"/>INSERT INTO blog(blog_title,author_id )
VALUES(#{blogTitle},#{author.authorId})
UPDATE blog
SET blog_title=#{blogTitle},
author_id=#{author.authorId}
WHERE blog_id=#{blogId}
delete from blog where blog_id=#{blogId}

4.1, test class

package com.pb.mybatis.mapper;import static org.junit.Assert.*;import java.io.InputStream;import java.util.List;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Before;import org.junit.Test;import com.pb.mybatis.po.Author;import com.pb.mybatis.po.Blog;/**

* @Title: BlogMapperTest.java

* @Package com.pb.mybatis.mapper

* @ClassName BlogMapperTest

* @Description: TODO (describe what this file does in one sentence)

* @author Liu Nan

* @date 2015-10-29 3:12:52 pm

* @version V1.0

*/public class BlogMapperTest {    private SqlSessionFactory sqlSessionFactory;

   
   @Before    public void setUp() throws Exception {
       String resource="configuration.xml";
       InputStream in=Resources.getResourceAsStream(resource);        //获取会话工厂
       sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
   }    /**
    * Test method for {@link com.pb.mybatis.mapper.BlogMapper#findBlogById(int)}.    */
   @Test    public void testFindBlogById() {        //获取会话
               SqlSession sqlSession=sqlSessionFactory.openSession();                //Mapper接口
               BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class);                //调用方法
               Blog blog=blogMapper.findBlogById(2);
               System.out.println(blog);                //关闭会话                sqlSession.close();
   }    /**
    * Test method for {@link com.pb.mybatis.mapper.BlogMapper#findBlogByName(java.lang.String)}.    */
   @Test    public void testFindBlogByName() {        //获取会话
       SqlSession sqlSession=sqlSessionFactory.openSession();        //Mapper接口
       BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class);        //调用方法
       List blogs=blogMapper.findBlogByName("小");
       System.out.println(blogs);        //关闭会话        sqlSession.close();
   }    /**
    * Test method for {@link com.pb.mybatis.mapper.BlogMapper#addBlog(com.pb.mybatis.po.Blog)}.    */
   @Test    public void testAddBlog() {        //获取会话
               SqlSession sqlSession=sqlSessionFactory.openSession();                //Mapper接口
               BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class);
               Blog blog=new Blog();
               blog.setBlogTitle("倚天屠龙记");
               AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class);                //调用方法
               Author author=authorMapper.findAuthorById(2);
               blog.setAuthor(author);                int num=blogMapper.addBlog(blog);
               System.out.println("num="+num);
               System.out.println(blog.getBlogId());
               sqlSession.commit();
               sqlSession.close();
   }    /**
    * Test method for {@link com.pb.mybatis.mapper.BlogMapper#updateBlog(com.pb.mybatis.po.Blog)}.    */
   @Test    public void testUpdateBlog() {        //获取会话
       SqlSession sqlSession=sqlSessionFactory.openSession();        //Mapper接口
       BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class);        //调用方法
       Blog blog=blogMapper.findBlogById(8);
       blog.setBlogTitle("笑傲江湖");
       Author author=blog.getAuthor();
       author.setAuthorUserName("金庸");
       AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class);        int authorNum=authorMapper.updateAuthor(author);        int num=blogMapper.updateBlog(blog);
       System.out.println("authorNum="+authorNum);
       System.out.println("num="+num);
       sqlSession.commit();        //关闭会话        sqlSession.close();
   }    /**
    * Test method for {@link com.pb.mybatis.mapper.BlogMapper#deleteBlog(int)}.    */
   @Test    public void testDeleteBlog() {        //获取会话
               SqlSession sqlSession=sqlSessionFactory.openSession();                //Mapper接口
               BlogMapper blogMapper=sqlSession.getMapper(BlogMapper.class);                int num=blogMapper.deleteBlog(11);
               System.out.println("num="+num);
               sqlSession.commit();
               sqlSession.close();
       }

}

 

 

 

五、一对多

5.1、建立Posts类

 

package com.pb.mybatis.po;import java.util.Date;/**

* @Title: Posts.java

* @Package com.pb.mybatis.po

* @ClassName Posts

* @Description: TODO (Blog article)

* @author Liu Nan

* @date 2015-10-29 9:31:22 AM

* @version V1.0

*/public class Posts {    //文章ID
   private int postId;    
   //文件主题
   private String postSubject;    
   //主体内容
   private String postBody;    //文章建立时间
   private Date createTime;    
   
   /**
    * @return the postId    */
   public int getPostId() {        return postId;
   }    /**
    * @param postId the postId to set    */
   public void setPostId(int postId) {        this.postId = postId;
   }    /**
    * @return the postSubject    */
   public String getPostSubject() {        return postSubject;
   }    /**
    * @param postSubject the postSubject to set    */
   public void setPostSubject(String postSubject) {        this.postSubject = postSubject;
   }    /**
    * @return the postBody    */
   public String getPostBody() {        return postBody;
   }    /**
    * @param postBody the postBody to set    */
   public void setPostBody(String postBody) {        this.postBody = postBody;
   }   /**
    * @return the createTime    */
   public Date getCreateTime() {        return createTime;
   }    /**
    * @param createTime the createTime to set    */
   public void setCreateTime(Date createTime) {        this.createTime = createTime;
   }    /**(non Javadoc)
   
    *

Title: toString


   
    *

Description:重写toString方法


   
    * @return
   
    * @see java.lang.Object#toString()    */
   @Override    public String toString() {        return "Posts [postId=" + postId + ", postSubject=" + postSubject                + ", postBody=" + postBody +", createTime="
               + createTime + "]";
   }

   
   
   
   
}

 

5.2、在blog类中添加List

 

package com.pb.mybatis.po;import java.util.List;/**

* @Title: Blog.java

* @Package com.pb.mybatis.po

* @ClassName Blog

* @Description: TODO(Blog)

* @author Liu Nan

* @date 2015 -10-29 9:32:56 AM

* @version V1.0

*/public class Blog {    //博客ID
   private int blogId;    
   //标题
   private String blogTitle;    
   //博客作者
   private Author author;    
   //文章List
   private List posts;    /**
    * @return the blogId    */
   public int getBlogId() {        return blogId;
   }    /**
    * @param blogId the blogId to set    */
   public void setBlogId(int blogId) {        this.blogId = blogId;
   }    /**
    * @return the blogTitle    */
   public String getBlogTitle() {        return blogTitle;
   }    /**
    * @param blogTitle the blogTitle to set    */
   public void setBlogTitle(String blogTitle) {        this.blogTitle = blogTitle;
   }    /**
    * @return the author    */
   public Author getAuthor() {        return author;
   }    /**
    * @param author the author to set    */
   public void setAuthor(Author author) {        this.author = author;
   }    /**
    * @return the posts    */
   public List getPosts() {        return posts;
   }    /**
    * @param posts the posts to set    */
   public void setPosts(List posts) {        this.posts = posts;
   }    /**(non Javadoc)
   
    *

Title: toString


   
    *

Description:


   
    * @return
   
    * @see java.lang.Object#toString()    */
   @Override    public String toString() {        return "Blog [blogId=" + blogId + ", blogTitle=" + blogTitle                + ", author=" + author + ", posts=" + posts + "]";
   }

   
   
   
}

 

5.3、修改blogMapper.xml

 

br/> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis .org/dtd/mybatis-3-mapper.dtd">< ;resultMap type="Posts" id="postsResultMap">INSERT INTO blog(blog_title,author_id)
VALUES(#{blogTitle},#{author.authorId})
UPDATE blog
SET blog_title=#{blogTitle},
author_id=#{author.authorId}
WHERE blog_id=#{blogId}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn