本文主要是通过接口+映射文件的方式实现项目
一.创建一个java项目,新建javaProject
二.添加mybatis和mysql链接的jar包
三.创建数据库,特别注意创建外键
四.编写mybstis-cofig.xml配置文件
<?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">
<!-- mybatis-cofig配置文件 -->
<configuration>
<!--配置别名 -->
<typeAliases>
<typeAlias type="com.mybatis.many.to.one.pojo.User" alias="user" />
<typeAlias type="com.mybatis.many.to.one.pojo.Post" alias="post" />
</typeAliases>
<!-- 创建数据层 运行环境 -->
<environments default="development">
<environment id="development">
<!-- JDBC事务管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 数据源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="110119" />
</dataSource>
</environment>
</environments>
<!-- 映射文件群 -->
<mappers>
<!-- 实体类对应的映射文件 -->
<mapper resource="com/mybatis/many/to/one/mapper/PostInterface.xml" />
</mappers>
</configuration>
五.创建实体类
1)User.java
/**
* @author wuchao
* @time 上午9:43:03
* @description TODO
*/
package com.mybatis.many.to.one.pojo;
import java.util.Date;
import java.util.List;
/**
* @author wuchao
* @time 上午9:43:03
*
*/
public class User {
private int id;
private String name;
private String dept;
private String phone;
private String website;
private Date createTimeDate;
private List postList;
/**
* @author wuchao
* @time 上午9:47:05
*/
public User() {
super();
// TODO Auto-generated constructor stub
}
/**
* @author wuchao
* @time 上午9:47:14
*/
public User(int id, String name, String dept, String phone, String website,
Date createTimeDate, List postList) {
super();
this.id = id;
this.name = name;
this.dept = dept;
this.phone = phone;
this.website = website;
this.createTimeDate = createTimeDate;
this.postList = postList;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public Date getCreateTimeDate() {
return createTimeDate;
}
public void setCreateTimeDate(Date createTimeDate) {
this.createTimeDate = createTimeDate;
}
public List getPostList() {
return postList;
}
public void setPostList(List postList) {
this.postList = postList;
}
}
2)Post.java
/**
* @author wuchao
* @time 上午9:47:42
* @description TODO
*/
package com.mybatis.many.to.one.pojo;
/**
* @author wuchao
* @time 上午9:47:42
*
*/
public class Post {
private int postId;
private String postName;
private String title;
private User user;
/**
* @author wuchao
* @time 下午2:28:43
*/
public Post() {
super();
// TODO Auto-generated constructor stub
}
/**
* @author wuchao
* @time 下午2:28:50
*/
public Post(int postId, String postName, String title, User user) {
super();
this.postId = postId;
this.postName = postName;
this.title = title;
this.user = user;
}
public int getPostId() {
return postId;
}
public void setPostId(int postId) {
this.postId = postId;
}
public String getPostName() {
return postName;
}
public void setPostName(String postName) {
this.postName = postName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
六.创建PostInteface接口
/**
* @author wuchao
* @time 上午9:51:07
* @description TODO
*/
package com.mybatis.many.to.one.dao;
import com.mybatis.many.to.one.pojo.Post;
/**
* @author wuchao
* @time 上午9:51:07
*
*/
public interface PostInterface {
public Post getPostById(int postId);
}
七.创建PostInterface.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">
<mapper namespace="com.mybatis.many.to.one.dao.PostInterface">
<resultMap type="post" id="resultPostMap">
<id property="postId" column="post_id"/>
<result property="postName" column="postName"/>
<result property="title" column="title" />
<association property="user" javaType="user">
<!-- 此处id的property是user实体类属性名,column是 -->
<id property="id" column="id" />
<result property="name" column="name" />
<result property="dept" column="dept" />
<result property="phone" column="phone" />
<result property="website" column="website" />
<result property="createTimeDate" column="createTime" />
</association>
</resultMap>
<!-- resultMap返回结果的数据类型,parameterType传入参数的数据类型 -->
<select id="getPostById" resultMap="resultPostMap"
parameterType="int">
select u.*,p.* from user u,post p where u.id = p.userId and p.post_id=#{postId}
</select>
</mapper>
配置文件中association代表一对多的一的实体类
八.测试类,测试结果和项目结构
/**
* @author wuchao
* @time 上午10:18:20
* @description TODO
*/
package com.mybatis.many.to.one.test;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.mybatis.many.to.one.dao.PostInterface;
import com.mybatis.many.to.one.pojo.Post;
import com.mybatis.many.to.one.pojo.User;
/**
* @author wuchao
* @time 上午10:18:20
*
*/
public class Test {
private static Reader reader;
private static SqlSessionFactory sqlSessionFactory;
static {
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public SqlSessionFactory getSession() {
return sqlSessionFactory;
}
public static void main(String args[]) {
SqlSession session = sqlSessionFactory.openSession();
try {
PostInterface postInterface = (PostInterface) session
.getMapper(PostInterface.class);
Post post = postInterface.getPostById(1);
System.out.println("查询到的id是:" + post.getPostId());
System.out.println("查询到的postTitle是:"+post.getTitle());
System.out.println("查询到的user创建时间是:"
+ post.getUser().getCreateTimeDate());
System.out.println("查询到的user名字是:"+post.getUser().getName());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
session.close();
}
}
}