创建基于session的util类,在线程中共享SqlSession
package cn.smartapp.blogs.pojo; import java.io.Serializable; import java.util.HashSet; import java.util.Set; /** * User实体类 * @author smart * 对应blog_user表 */ public class User implements Serializable { //对应表的id字段 private int id; //对应表的user_name字段 private String userName; //对应表的user_pass字段 private String password; //对应表的nick_name字段 private String nickName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } }
创建blog_user表对应的实体类User
package cn.smartapp.blogs.pojo; import java.io.Serializable; import java.util.HashSet; import java.util.Set; /** * User实体类 * @author smart * 对应blog_user表 */ public class User implements Serializable { //对应表的id字段 private int id; //对应表的user_name字段 private String userName; //对应表的user_pass字段 private String password; //对应表的nick_name字段 private String nickName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } }
创建blog_category表对应的实体类Category
package cn.smartapp.blogs.pojo; import java.io.Serializable; import java.util.HashSet; import java.util.Set; /** * Category实体类 * @author smart * 与blog_category表对应的实体类 */ public class Category implements Serializable { //对应表中的主键id private int id; //对应表中的cate_name字段 private String cateName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCateName() { return cateName; } public void setCateName(String cateName) { this.cateName = cateName; } }
创建blog_article表对应的实体类Article
package cn.smartapp.blogs.pojo; import java.io.Serializable; import java.util.Date; /** * Article实体类 * @author smart * 对应blog_article表的实体类 */ public class Article implements Serializable { //对应表中的主键id private int id; //对应表中的art_title字段 private String title; //对应表中的art_content字段 private String content; //对就表中的art_pubtime字段 private Date pubTime; //对应表中的pub_user_id字段 private int userId; //对应表中的cate_id字段 private int cateId; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getPubTime() { return pubTime; } public void setPubTime(Date pubTime) { this.pubTime = pubTime; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public int getCateId() { return cateId; } public void setCateId(int cateId) { this.cateId = cateId; } }
在核心配置文件中为实体类别名
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 配置文件新增的部分,为实体类别名 ---> <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> <!-- ========================== --> <environments default="news_res"> <environment id="news_res"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/blogs_db?characterEncoding=utf-8" /> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> </configuration>
blog_user表与blog_article表存在主从关系,所以为User类与Article类建立一对多关联关系
package cn.smartapp.blogs.pojo; import java.io.Serializable; import java.util.HashSet; import java.util.Set; /** * User实体类 * @author smart * 对应blog_user表 */ public class User implements Serializable { //对应表的id字段 private int id; //对应表的user_name字段 private String userName; //对应表的user_pass字段 private String password; //对应表的nick_name字段 private String nickName;
//此处是新增的代码 //为User与Article建立一对多关联 private Set<Article> articles = new HashSet<Article>(); public Set<Article> getArticles() { return articles; } public void setArticles(Set<Article> articles) { this.articles = articles; } //=================================
public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } }
blog_category表与blog_article表存在主从关系,所以为Category类与Article类建立一对多关联关系
package cn.smartapp.blogs.pojo; import java.io.Serializable; import java.util.HashSet; import java.util.Set; /** * Category实体类 * @author smart * 与blog_category表对应的实体类 */ public class Category implements Serializable { //对应表中的主键id private int id; //对应表中的cate_name字段 private String cateName; //此处为新增代码 //为Category与Article类建立一对多关联 private Set<Article> articles = new HashSet<Article>(); public Set<Article> getArticles() { return articles; } //========================== public void setArticles(Set<Article> articles) { this.articles = articles; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCateName() { return cateName; } public void setCateName(String cateName) { this.cateName = cateName; } }
Article类与User类和Category类实现多对一关联
package cn.smartapp.blogs.pojo; import java.io.Serializable; import java.util.Date; /** * Article实体类 * @author smart * 对应blog_article表的实体类 */ public class Article implements Serializable { //对应表中的主键id private int id; //对应表中的art_title字段 private String title; //对应表中的art_content字段 private String content; //对就表中的art_pubtime字段 private Date pubTime; //对应表中的pub_user_id字段 private int userId; //对应表中的cate_id字段 private int cateId; //此处为新增代码 //实现多对一关联关系 private User user; private Category category; public User getUser() { this.setUserId(user.getId()); //外键关联 return user; } public void setUser(User user) { this.user = user; } public Category getCategory() { this.setCateId(category.getId()); //外键关联 return category; } public void setCategory(Category category) { this.category = category; } //==================================== public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getPubTime() { return pubTime; } public void setPubTime(Date pubTime) { this.pubTime = pubTime; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public int getCateId() { return cateId; } public void setCateId(int cateId) { this.cateId = cateId; } }
原文:http://www.cnblogs.com/MrzhangKk/p/5164649.html