概述
1、问题:当查询新闻的时候我们一般情况下需要新闻的类型以及作者的信息。根据表设计的规范我们要把文章,文章类型,作者信息分别设计成一张表,其中文章中分别存在指向文章类型和作者信息的外键。我们的问题就是怎么使用ibatis把表空间数据映射为java的实体类属性。
2、表DDL,
用户表
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(25) default NULL,
’password` varchar(32) default NULL,
`gender` int(11) default '1',
`birth` date default NULL,
`user_type` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
文章类型表
CREATE TABLE `article_class` (
`acid` int(11) NOT NULL auto_increment,
`name` varchar(25) default NULL,
`class_desc` varchar(255) default NULL,
PRIMARY KEY (`acid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
文章表
CREATE TABLE `article` (
`aid` int(11) NOT NULL auto_increment,
`title` varchar(36) default NULL,
`content` text,
`classid` int(11) default NULL,
`userid` int(11) default NULL,
PRIMARY KEY (`aid`),
KEY `classid` (`classid`),
KEY `userid` (`userid`),
CONSTRAINT `article_ibfk_1` FOREIGN KEY (`classid`) REFERENCES `article_class` (`acid`),
CONSTRAINT `article_ibfk_2` FOREIGN KEY (`userid`) REFERENCES `users` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
3、实体类
import java.sql.Date;
public class User {
private Integer id;
private String username;
private String password;
private int gender;
private Date birth;
private int userType = 1;
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 int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public int getUserType() {
return userType;
}
public void setUserType(int user_type) {
this.userType = user_type;
}
}
文章类型类
public class ArticleClass {
private Integer id;
private String name;
private String desc;
public ArticleClass() {
}
public ArticleClass(int id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
文章类
public class Article {
private Integer id;
// private int classid;
// private int userid;
private String title;
private String content;
private ArticleClass articleClass;
private User user;
public Article() {
}
/* public int getClassid() {
return classid;
}
public void setClassid(int classid) {
this.classid = classid;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}*/
public Article(String title, String content) {
this.title = title;
this.content = content;
}
public Integer getId() {
return id;
}
public void setId(Integer 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 ArticleClass getArticleClass() {
return articleClass;
}
public void setArticleClass(ArticleClass articleClass) {
this.articleClass = articleClass;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
4、解决方案
问题主要发生在查找文章的时候,如果把文章类型信息和用户信息也查出来并存储在article对象的user和articleClass中
article映射文件如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://java.plugbase.org/dtd/sql-map-2.dtd">
<sqlMap namespace="article">
<typeAlias alias="Article" type="ebiz.sql.article.Article"/>
<typeAlias alias="ArticleClass" type="ebiz.sql.article.ArticleClass"/>
<typeAlias alias="User" type="ebiz.sql.user.User"/>
<resultMap class="Article" id="articleResult">
<result property="id" column="aid"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<result property="articleClass" select="getArticleClassById" column="classid"/>
<result property="user" select="getUserById" column="userid"/>
</resultMap>
<!-- select sql -->
<select id="getArticleAllCascade" resultMap="articleResult">
select aid,title,content,classid, userid from article
</select>
<select id="getArticleByIdCascade" parameterClass="java.lang.Integer" resultMap="articleResult">
select aid,title,content,classid,userid from article
where article.aid = #id#
</select>
<!-- insert sql -->
<insert id="insertArticle" parameterClass="Article">
insert into article (title, content, classid, userid)
values(#title#, #content#, #articleClass.id#, #user.id#)
</insert>
<!-- update sql -->
<update id="updateArticle" parameterClass="Article">
update article
set title=#title#, content=#content#, classid=#articleClass.id#, userid=#user.id#
where aid=#id#
</update>
<!-- delete sql -->
<delete id="deleteArticleById" parameterClass="java.lang.Integer">
delete from article where aid = #id#
</delete>
</sqlMap>
user映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://java.plugbase.org/dtd/sql-map-2.dtd">
<sqlMap namespace="users">
<typeAlias alias="User" type="ebiz.sql.user.User"/>
<resultMap class="User" id="userResult">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="birth" column="birth"/>
<result property="gender" column="gender"/>
<result property="userType" column="user_type"/>
</resultMap>
<!-- select sql -->
<select id="getUsersAll" resultMap="userResult">
select id, username, password,birth,gender,user_type
from users
order by id desc
</select>
<select id="getUserById" parameterClass="java.lang.Integer" resultMap="userResult">
select id, username, password,birth,gender,user_type
from users
where id=#id#
</select>
<select id="getUserByNameAndPassword" parameterClass="User" resultClass="java.lang.Integer">
select count(*)
from users
where username=#username# and password=#password#
</select>
<select id="getUserByName" parameterClass="java.lang.String" resultMap="userResult">
select id, username, password,birth,gender,user_type
from users
where username=#username#
</select>
<!-- insert sql -->
<insert id="insertUser" parameterClass="User">
insert into users (username, password,birth,gender,user_type)
values(
#username#, #password#, #birth#, #gender#, #userType#
)
</insert>
<!-- update sql -->
<update id="updateUser" parameterClass="User">
update users
set username=#username#, password=#password#, birth=#birth#, gender=#gender#, user_type=#userType#
where id=#id#
</update>
<!-- delete sql -->
<delete id="deleteUserById" parameterClass="java.lang.Integer">
delete from users where id = #id#
</delete>
</sqlMap>
articleClass映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://java.plugbase.org/dtd/sql-map-2.dtd">
<sqlMap namespace="article_class">
<typeAlias alias="ArticleClass" type="ebiz.sql.article.ArticleClass"/>
<resultMap class="ArticleClass" id="articleClassResult">
<result property="id" column="acid"/>
<result property="name" column="name"/>
<result property="desc" column="class_desc"/>
</resultMap>
<!-- select sql -->
<select id="getArticleClassById" parameterClass="java.lang.Integer" resultMap="articleClassResult">
select acid, name, class_desc from article_class where acid = #classid#
</select>
</sqlMap>
5、解释:当使用"getArticleByIdCascade"查询查询文章的时候,查出来之后分别把classid和userid作为参数调用"getUserById"和"getArticleClassById"
最后
以上就是友好八宝粥为你收集整理的ibatis多对一表关联映射的解决过程的全部内容,希望文章能够帮你解决ibatis多对一表关联映射的解决过程所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复