首页 > 其他 > 详细

Mybatis 学习-2

时间:2016-01-27 22:48:00      阅读:258      评论:0      收藏:0      [点我收藏+]

创建基于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;
    }
}

Mybatis 学习-2

原文:http://www.cnblogs.com/MrzhangKk/p/5164649.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!