创建两个类 User 和 article
package entity;
public class User {
private int id;
public User(){
}
public User(int id, String userName, String userAge, String userAddress) {
super();
this.id = id;
this.userName = userName;
this.userAge = userAge;
this.userAddress = userAddress;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String userName;
private String userAge;
private String userAddress;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAge() {
return userAge;
}
public void setUserAge(String userAge) {
this.userAge = userAge;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
}
package entity;
public class Article {
private int id;
private User user;
private String title;
private String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
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 Article(){
}
public Article(int id, User user, String title, String content) {
super();
this.id = id;
this.user = user;
this.title = title;
this.content = content;
}
}
数据库创建代码
Drop TABLE IF EXISTS `article`;
Create TABLE `article` (
`id` int(11) NOT NULL auto_increment,
`userid` int(11) NOT NULL,
`title` varchar(100) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- 添加几条测试数据
-- ----------------------------
Insert INTO `article` VALUES ('1', '1', 'test_title', 'test_content');
Insert INTO `article` VALUES ('2', '1', 'test_title_2', 'test_content_2');
Insert INTO `article` VALUES ('3', '1', 'test_title_3', 'test_content_3');
Insert INTO `article` VALUES ('4', '1', 'test_title_4', 'test_content_4');
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`userName` varchar(50) DEFAULT NULL COMMENT '名称',
`userAge` int(11) DEFAULT NULL COMMENT '年龄',
`userAddress` varchar(200) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES (1, '查看', 23, '精彩内容');
INSERT INTO `user` VALUES (2, '查看', 23, '精彩内容');
INSERT INTO `user` VALUES (3, '查看', 23, '精彩内容');
INSERT INTO `user` VALUES (4, '查看', 23, '精彩内容');
INSERT INTO `user` VALUES (5, '查看', 23, '精彩内容');
多对一的配置文件编写方式 : 引入了association元素
<resultMap id="resultUserArticleList" type="entity.Article">
<id property="id" column="aid"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<association property="user" javaType="entity.User">
<id property="id" column="id"/>
<result property="userName" column="userName"/>
<result property="userAddress" column="userAddress"/>
</association>
</resultMap>
<select id="getUserArticles" parameterType="int" resultMap="resultUserArticleList">
select user.id , user.userName , user.userAddress , article.id aid ,article.title ,article.content from
user,article where user.id = article.userid and user.id=#{id}
</select>
一对多的文件配置方式:
<resultMap type="entity.User" id="resultListUser">
<id column = "id" property="id"/>
<result column="userName" property="userName"/>
<result column="userAge" property="userAge"/>
<result column="userAddress" property="userAddress"/>
</resultMap>
<!-- User联合文件进行查询 方法之二的配置(多对一的方式)--》
<resultMap id="resultUserArticleList-2" type="Article">
<id property="id" column="aid"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<association property="user" javaType="User" resultMap="resultListUser"/>
</resultMap>
<select id="getUserArticles" parameterType="int" resultMap="resultUserArticleList">
select user.id , user.userName ,user.userAddress ,article.id aid , article.title , article.content from user , article where user.id = article.userid and user.id=#{id}
</select>