目录
数据库
1、category_表
插入数据:
建表sql文:
USE mybatistest1;
CREATE TABLE category_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(32) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2、product_表
插入数据:
建表sql文:
use mybatistest1;
create table product_(
id int NOT NULL AUTO_INCREMENT,
name varchar(30) DEFAULT NULL,
price float DEFAULT 0,
cid int ,
PRIMARY KEY (id)
)AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
代码
Category实体类
package com.mybatis.pojo;
import java.util.List;
public class Category {
private int id;
private String name;
//List<Product> products;
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 List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}*/
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
}
Product实体类
package com.mybatis.pojo;
public class Product {
private int id;
private String name;
private float price;
private Category category;
public int getId() {
return id;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}
CategoryMapper类
package com.mybatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.mybatis.pojo.Category;
public interface CategoryMapper {
@Select(" select * from category_ where id=#{id}")
public Category getByProduct(int id);
}
ProductMapper类
package com.mybatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.mybatis.pojo.Product;
public interface ProductMapper {
@Select("select * from product_")
@Results({
@Result(property="id",column="cid"),
@Result(property="category",column="cid",one=@One(select="com.mybatis.mapper.CategoryMapper.getByProduct"))
})
public List<Product> list();
}
全局配置文件
<?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>
<package name="com.mybatis.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatistest1?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="zhao8888"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.mybatis.mapper.CategoryMapper"/>
<mapper class="com.mybatis.mapper.ProductMapper"/>
</mappers>
</configuration>
测试类
package com.mybatis;
import java.awt.event.ItemEvent;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.text.html.HTMLDocument.HTMLReader.ParagraphAction;
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.mapper.CategoryMapper;
import com.mybatis.mapper.ProductMapper;
import com.mybatis.pojo.*;
public class testMybatis {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml"; //获取全局配置信息
InputStream inputStream = Resources.getResourceAsStream(resource); //全局配置信息装入输入流
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //建立sql会话工厂,将输入流装入
SqlSession session=sqlSessionFactory.openSession(); //开启一个会话
//CategoryMapper categorymapper=session.getMapper(CategoryMapper.class);
ProductMapper productmapper=session.getMapper(ProductMapper.class);
listAll(productmapper);
session.commit(); //更新到数据库
session.close();
}
private static void listAll(ProductMapper mapper) {
List<Product> ps=mapper.list();
for(Product p:ps) {
System.out.println(p);
Category c=p.getCategory();
System.out.println("\t的分类是:\t"+c.getName());
}
}
}