init.sql:
use bbs; /*帖子表,存储帖子的信息*/ drop table if exists article; create table article( id int primary key auto_increment, title varchar(100), biboid int, smboid int, userid int, createtime datetime, updatetime datetime, context text, goodcount int, badcount int, reward int, score int, ispay bit, islocked bit )ENGINE=InnoDB DEFAULT CHARSET=utf8; /*用户表*/ drop table if exists user; create table user( id int primary key auto_increment, username varchar(50) unique, password varchar(100), nickname varchar(50), salt varchar(100), locked boolean )engine=InnqDB default charset=utf8; INSERT INTO `user` VALUES (1,'pwpw1218','pwpw1218','King-pan',NULL,1),(5,'pwpw121822','pwpw1218','King-Pan',NULL,1),(6,'pwpw121833','pwpw1218','King-Pan',NULL,1); INSERT INTO `article` VALUES (1,'bbs通知222',0,0,0,'2014-06-04 10:56:07','2014-06-04 11:11:06','文章 马伊琍 离婚吧',0,0,0,0,'\0','\0'),(2,'bbs通知222',0,0,0,'2014-06-04 11:10:41','2014-06-04 11:16:18','文章 马伊琍 离婚吧',0,0,0,0,'\0','\0'),(3,'bbs通知',0,0,1,'2014-06-06 15:48:51',NULL,'文章 马伊琍 离婚吧',0,0,0,0,'\0','\0'),(4,'bbs通知',0,0,1,'2014-06-06 15:48:56',NULL,'文章 马伊琍 离婚吧',0,0,0,0,'\0','\0'),(5,'bbs通知',0,0,1,'2014-06-06 15:49:00',NULL,'文章 马伊琍 离婚吧',0,0,0,0,'\0','\0'),(6,'bbs通知',0,0,1,'2014-06-06 15:49:04',NULL,'文章 马伊琍 离婚吧',0,0,0,0,'\0','\0'),(7,'bbs通知',0,0,1,'2014-06-06 15:49:07',NULL,'文章 马伊琍 离婚吧',0,0,0,0,'\0','\0'),(8,'bbs通知',0,0,1,'2014-06-06 15:49:12',NULL,'文章 马伊琍 离婚吧',0,0,0,0,'\0','\0');
User:
package com.mscncn.bbs.core.model; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * 用户表 * @author king-pan * */ public class User implements Serializable { private static final long serialVersionUID = 1L; /** * 用户id */ private Integer id; /** * 用户名 */ private String userName; /** * 用户密码 */ private String password; /** * 用户昵称 */ private String nickName; /** * 加密密码的盐 */ private String salt; /** * 账号是否锁住 */ private List<Article> articles=new ArrayList<Article>(); private Boolean locked = Boolean.FALSE; public Integer getId() { return id; } public void setId(Integer 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; } public String getSalt() { return salt; } public void setSalt(String salt) { this.salt = salt; } public Boolean getLocked() { return locked; } public void setLocked(Boolean locked) { this.locked = locked; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", nickName=" + nickName + ", salt=" + salt + ", locked=" + locked + "]"; } public List<Article> getArticles() { return articles; } public void setArticles(List<Article> articles) { this.articles = articles; } }
Article:
package com.mscncn.bbs.core.model; import java.io.Serializable; import java.util.Date; /** * 帖子,文章 * @author king-pan * */ public class Article implements Serializable { private static final long serialVersionUID = 1L; /** * 文章的id */ private int id; /** * 文章的标题 */ private String title; /** * 文章的内容 */ private String context; /** * BiBoid 帖子所属父版块id int */ private int biboId; /** * SmBoid 帖子所属子版块id int */ private int smboId; /** * admin 发帖者姓名 nvarchar */ private int userId; /** * createtime 发帖时间 datetime */ private Date createTime; /** * updatetime 更新时间 datetime */ private Date updateTime; /** * goodcount 帖子的好评数 int */ private int goodCount; /** * badcount 帖子的坏评数 int */ private int badCount; /** * reward 帖子的总共悬赏分(吸引浏览) int */ private int reward; /** * score 帖子悬赏分所剩下的分数 int */ private int score; /** * ispay 是否结贴 bit */ private boolean isPay; /** * islocked 是否帖子被锁定 bit */ private boolean isLocked; 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 getContext() { return context; } public void setContext(String context) { this.context = context; } public int getBiboId() { return biboId; } public void setBiboId(int biboId) { this.biboId = biboId; } public int getSmboId() { return smboId; } public void setSmboId(int smboId) { this.smboId = smboId; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public int getGoodCount() { return goodCount; } public void setGoodCount(int goodCount) { this.goodCount = goodCount; } public int getBadCount() { return badCount; } public void setBadCount(int badCount) { this.badCount = badCount; } public int getReward() { return reward; } public void setReward(int reward) { this.reward = reward; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public boolean isPay() { return isPay; } public void setPay(boolean isPay) { this.isPay = isPay; } public boolean isLocked() { return isLocked; } public void setLocked(boolean isLocked) { this.isLocked = isLocked; } @Override public String toString() { return "Article [id=" + id + ", title=" + title + ", content=" + context + ", biboId=" + biboId + ", smboId=" + smboId + ", userId=" + userId + ", createTime=" + createTime + ", updateTime=" + updateTime + ", goodCount=" + goodCount + ", badCount=" + badCount + ", reward=" + reward + ", score=" + score + ", isPay=" + isPay + ", isLocked=" + isLocked + "]"; } }
UserMapper.java:
public interface UserMapper { void add(User user); void delete(@Param(value="id") Integer id); void update(User user); User queryUser(Map<String,Object> params); User queryArticleByUser(@Param(value="id")Integer id); }
UserMapper.xml:
<?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"> <!-- 这里namespace必须是PostsMapper接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry”--> <mapper namespace="com.mscncn.bbs.mybatis.mapper.UserMapper"> <select id="queryUser" parameterType="java.util.Map" resultType="User"> select * from user <where> <if test="userName!=null"> and userName=#{userName} </if> <if test="1==1"> and 1=1 </if> </where> </select> <insert id="add" parameterType="User"> insert into user (username,password,nickname,salt,locked) values (#{userName},#{password},#{nickName},#{salt},#{locked}) </insert> <update id="update" parameterType="User"> update user <set> <if test="password!=null"> password=#{password}, </if> <if test="nickName!=null"> nickname=#{nickName}, </if> </set> where id=#{id} </update> <delete id="delete" parameterType="int"> delete from user <choose> <when test="id!=0"> where id=#{id} </when> <otherwise> where 1=2 </otherwise> </choose> </delete> <select id="queryArticleByUser" parameterType="int" resultMap="UserResultMap"> select u.id u_id, u.username,u.password,u.salt,a.id a_id,a.userid,a.title,a.context from user u join article a on(u.id=a.userid) and u.id=#{id} </select> <!-- private String userName; private String password; private String nickName; private String salt; --> <resultMap type="User" id="UserResultMap"> <id column="u_id" javaType="int" property="id" /> <result property="userName" column="username" javaType="String" /> <result property="password" column="password" javaType="String"/> <result property="nickName" column="nickname" javaType="String"/> <result property="salt" column="salt" javaType="String"/> <collection property="articles" ofType="Article"> <id property="id" column="a_id" /> <result property="title" column="title" javaType="String" /> <result property="context" column="context" javaType="String"/> </collection> </resultMap> </mapper>