MyBatis应属于一种轻量级的java持久层技术,它通过简单的SQL xml或注解,将数据库数据映射到接口与POJO。最近项目要用到mybatis,所以学习之后在这里做个总结,文中的示例以xml配置为主,mybatis也支持注解的方式。
测试数据
先给出demo所使用的表结构,以典型的用户(1)-文章(n)的关系表做demo数据
1 # 2 # mysql数据库:数据库名 :dblog 3 # 4 5 DROP TABLE IF EXISTS m_category; 6 CREATE TABLE m_category ( 7 id int(11) NOT NULL AUTO_INCREMENT, 8 name varchar(64) NOT NULL COMMENT '分类名称', 9 parent_id INT NOT NULL , 10 level INT NOT NULL DEFAULT 0, 11 path VARCHAR(64) NOT NULL COMMENT '栏目路径,rootId-xxId-xxId', 12 PRIMARY KEY (id) 13 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 14 15 DROP TABLE IF EXISTS m_post; 16 CREATE TABLE m_post ( 17 id int(11) NOT NULL AUTO_INCREMENT, 18 category_id INT NOT NULL , 19 user_id INT NOT NULL , 20 title varchar(64) NOT NULL COMMENT '标题', 21 content text COMMENT '正文', 22 created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 23 updated_at timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 24 PRIMARY KEY (id) 25 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 26 27 DROP TABLE IF EXISTS m_user; 28 CREATE TABLE m_user ( 29 id int(11) NOT NULL AUTO_INCREMENT, 30 username varchar(64) NOT NULL, 31 password varchar(255) NOT NULL, 32 salt VARCHAR(32) NOT NULL , 33 avatar varchar(64) DEFAULT NULL, 34 type enum('customer','admin','root') NOT NULL DEFAULT 'customer', 35 remember_token varchar(128) DEFAULT NULL, 36 PRIMARY KEY (id) 37 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 38 39 INSERT INTO m_user(id,username, password, salt,type) 40 VALUE (1,'lvyahui','XXXXXXX','abcs','admin'); 41 42 DROP TABLE IF EXISTS m_post_comment; 43 CREATE TABLE m_post_comment( 44 id int(11) AUTO_INCREMENT PRIMARY KEY , 45 post_id INT NOT NULL , 46 user_id INT NOT NULL , 47 content VARCHAR(512) NOT NULL DEFAULT '', 48 created_at TIMESTAMP NOT NULL DEFAULT current_timestamp, 49 updated_at TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' 50 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
对应的实体类
Post
1 package org.lyh.java.mybatis.model; 2 3 import java.sql.Timestamp; 4 5 /** 6 * @author lvyahui (lvyahui8@gmail.com,lvyahui8@126.com) 7 * @since 2017/1/1 14:00 8 */ 9 @SuppressWarnings("unused") 10 public class Post extends BaseModel { 11 12 private String title; 13 private String content; 14 private Timestamp createdAt; 15 private Timestamp updatedAt; 16 17 private Integer userId; 18 private Integer categoryId; 19 20 private User user; 21 private Category category; 22 23 public String getTitle() { 24 return title; 25 } 26 27 public void setTitle(String title) { 28 this.title = title; 29 } 30 31 public String getContent() { 32 return content; 33 } 34 35 public void setContent(String content) { 36 this.content = content; 37 } 38 39 public Timestamp getCreatedAt() { 40 return createdAt; 41 } 42 43 public void setCreatedAt(Timestamp createdAt) { 44 this.createdAt = createdAt; 45 } 46 47 public Timestamp getUpdatedAt() { 48 return updatedAt; 49 } 50 51 public void setUpdatedAt(Timestamp updatedAt) { 52 this.updatedAt = updatedAt; 53 } 54 55 public Integer getUserId() { 56 return userId; 57 } 58 59 public void setUserId(Integer userId) { 60 this.userId = userId; 61 } 62 63 public Integer getCategoryId() { 64 return categoryId; 65 } 66 67 public void setCategoryId(Integer categoryId) { 68 this.categoryId = categoryId; 69 } 70 71 public User getUser() { 72 return user; 73 } 74 75 public void setUser(User user) { 76 this.user = user; 77 } 78 79 public Category getCategory() { 80 return category; 81 } 82 83 public void setCategory(Category category) { 84 this.category = category; 85 } 86 87 88 @Override 89 public String toString() { 90 return "Post{" + 91 "title='" + title + '\'' + 92 ", content='" + content + '\'' + 93 ", createdAt=" + createdAt + 94 ", updatedAt=" + updatedAt + 95 ", userId=" + userId + 96 ", categoryId=" + categoryId + 97 '}'; 98 } 99 }