一、基本原理
1. 应用程序找Mybatis要数据
2. mybatis从数据库中找来数据
2.1 通过mybatis-config.xml 定位哪个数据库
2.2 通过Category.xml执行对应的select语句
2.3 基于Category.xml把返回的数据库记录封装在Category对象中
2.4 把多个Category对象装在一个Category集合中
3. 返回一个Category集合
//POJO类
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 + "]";
}
}
<!-- 定义SQL语句 -->
<?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="pojo">
<resultMap type="Category" id="categoryBean">
<id column="cid" property="id" />
<result column="cname" property="name" />
<!-- 一对多的关系 -->
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<collection property="products" ofType="Product">
<id column="pid" property="id" />
<result column="pname" property="name" />
<result column="price" property="price" />
</collection>
</resultMap>
<!-- 关联查询分类和产品表 -->
<select id="listCategory1" resultMap="categoryBean">
select c.*, p.*, c.id 'cid', p.id 'pid', c.name 'cname', p.name 'pname' from category_ c left join product_ p on c.id = p.cid
</select>
<select id="listCategory" resultType="Category">
select * from category_
</select>
<insert id="addCategory" parameterType="Category" >
insert into category_ ( name ) values (#{name})
</insert>
<delete id="deleteCategory" parameterType="Category" >
delete from category_ where id= #{id}
</delete>
<select id="getCategory" parameterType="_int" resultType="Category">
select * from category_ where id= #{id}
</select>
<update id="updateCategory" parameterType="Category" >
update category_ set name=#{name} where id=#{id}
</update>
<select id="listCategoryByName" parameterType="string" resultType="Category">
select * from category_ where name like concat('%',#{0},'%')
</select>
<select id="listCategoryByIdAndName" parameterType="map" resultType="Category">
select * from category_ where id> #{id} and name like concat('%',#{name},'%')
</select>
<select id="listProduct" resultType="Product">
select * from product
</select>
</mapper>
<!-- MyBatis配置文件 -->
<?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="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/how2java?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="g4885885"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="pojo/Category.xml"/>
</mappers>
</configuration>
//主程序
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 pojo.Category;
import pojo.Product;
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);
SqlSession session=sqlSessionFactory.openSession();
//增
Category Ca = new Category();
Ca.setName("新增加的Category");
session.insert("addCategory",Ca);
//删
Category Cb = new Category();
Cb.setId(3);
session.delete("deleteCategory",Cb);
//查
Category Cc= session.selectOne("getCategory",2);
System.out.println("查 "+Cc.getName());
//改(先查后改)
Category Cd= session.selectOne("getCategory",2);
Cd.setName("修改了的Category名稱");
session.update("updateCategory",Cd);
//模糊查询
List<Category> Ce = session.selectList("listCategoryByName","cat");
for (Category c : Ce) {
System.out.println("模糊查询 "+c.getName());
}
//多条件查询
Map<String,Object> params = new HashMap<>();
params.put("id", 1);
params.put("name", "cat");
List<Category> Cf = session.selectList("listCategoryByIdAndName",params);
for (Category c : Cf) {
System.out.println("多条件查询"+c.getName());
}
//全部查询
List<Category> cs=session.selectList("listCategory");
for (Category c : cs) {
System.out.println("全部查询"+c.getName());
System.out.println("全部查询"+c.getId());
}
//一对多两表查询
List<Category> Cg = session.selectList("listCategory1");
for (Category c : Cg) {
System.out.println(c);
List<Product> ps = c.getProducts();
for (Product p : ps) {
System.out.println("\t"+p);
}
}
List<Product> cs1=session.selectList("listProduct");
for (Product c : cs1) {
System.out.println(c.getName());
System.out.println(c.getId());
}
}
}