一只倔强的笨猪

【MyBatis】映射案例的最佳解决方案

结构图:
《【MyBatis】映射案例的最佳解决方案》
1.创建实体类。

package com.bean;

import java.io.Serializable;

public class Goods implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4049728674182319106L;
	private Integer goodsId;
	private String goodsName;
	private Double goodsPrice;
	private Integer goodsNum;
	private GoodsType goodsType;
	
	public Goods() {
	}

	public Goods(Integer goodsId, String goodsName, Double goodsPrice,
			Integer goodsNum, GoodsType goodsType) {
		super();
		this.goodsId = goodsId;
		this.goodsName = goodsName;
		this.goodsPrice = goodsPrice;
		this.goodsNum = goodsNum;
		this.goodsType = goodsType;
	}

	public Integer getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(Integer goodsId) {
		this.goodsId = goodsId;
	}

	public String getGoodsName() {
		return goodsName;
	}

	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName;
	}

	public Double getGoodsPrice() {
		return goodsPrice;
	}

	public void setGoodsPrice(Double goodsPrice) {
		this.goodsPrice = goodsPrice;
	}

	public Integer getGoodsNum() {
		return goodsNum;
	}

	public void setGoodsNum(Integer goodsNum) {
		this.goodsNum = goodsNum;
	}

	public GoodsType getGoodsType() {
		return goodsType;
	}

	public void setGoodsType(GoodsType goodsType) {
		this.goodsType = goodsType;
	}

	@Override
	public String toString() {
		return "Goods [goodsId=" + goodsId + ", goodsName=" + goodsName
				+ ", goodsPrice=" + goodsPrice + ", goodsNum=" + goodsNum
				+ ", goodsType=" + goodsType + "]";
	}
	
}
package com.bean;

import java.util.List;

public class GoodsType {
	private Integer typeId;
	private String typeName;
	private List<Goods> goodsList;
	
	public GoodsType() {
	}

	public GoodsType(Integer typeId, String typeName, List<Goods> goodsList) {
		super();
		this.typeId = typeId;
		this.typeName = typeName;
		this.goodsList = goodsList;
	}

	public Integer getTypeId() {
		return typeId;
	}

	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public List<Goods> getGoodsList() {
		return goodsList;
	}

	public void setGoodsList(List<Goods> goodsList) {
		this.goodsList = goodsList;
	}

	@Override
	public String toString() {
		return "GoodsType [typeId=" + typeId + ", typeName=" + typeName
				+ ", goodsList=" + goodsList + "]";
	}
}

2.编写Mapper接口。

package com.mapper;

import java.util.List;

import com.bean.Goods;

public interface GoodsMapper {
	public List<Goods> findAll();
	/**
	 * 给关联关系对象(商品类型)提供查找其下商品的方法
	 * 如果要查某个商品类型信息的时候要带出相关的商品信息
	 * @param typeId 某个商品类型的ID
	 * @return 返回某个商品类型下的商品集
	 */
	public List<Goods> findByType(int typeId);
}
package com.mapper;

import java.util.List;

import com.bean.GoodsType;

public interface GoodsTypeMapper {
	public List<GoodsType> findAll(); 
	public GoodsType findById(int typeId);
}

3.编写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.mapper.GoodsMapper">
	<select id="findAll" resultMap="BaseResultMap">
		select * from goods
		left join goods_type
		on goods_type.typeId = goods.goods_type
	</select>
	<select id="findByType" parameterType="java.lang.Integer" resultMap="BaseResultMap">
		select * from goods
		left join goods_type
		on goods_type.typeId = goods.goods_type
		where goods_type = #{id}
	</select>
	<resultMap type="com.bean.Goods" id="BaseResultMap">
		<id column="goods_id" property="goodsId" jdbcType="INTEGER" />
		<result column="goods_name" property="goodsName" jdbcType="VARCHAR" />
		<result column="goods_price" property="goodsPrice" jdbcType="DOUBLE" />
		<result column="goods_num" property="goodsNum" jdbcType="INTEGER" />
		<association property="goodsType" javaType="com.bean.GoodsType" resultMap="com.mapper.GoodsTypeMapper.BaseResultMap" />
	</resultMap>
</mapper>
<?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.mapper.GoodsTypeMapper">
	<select id="findAll" resultMap="BaseResultMap">
		select * from goods_type
	</select>
	<select id="findById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
		select * from goods_type
		where typeId = #{id}
	</select>
	<resultMap type="com.bean.GoodsType" id="BaseResultMap">
		<id property="typeId" column="typeId" javaType="java.lang.Integer" />
		<result column="typeName" property="typeName" jdbcType="VARCHAR" />
	</resultMap>
</mapper>
<?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>
	<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://127.0.0.1:3306/spring?useUnicode=true&amp;characterEncoding=utf8" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/mapper/GoodsMapper.xml" />
		<mapper resource="com/mapper/GoodsTypeMapper.xml" />
	</mappers>
</configuration>

4.编写service层。

package com.service;

import java.io.Reader;
import java.util.List;

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.bean.Goods;
import com.bean.GoodsType;
import com.mapper.GoodsMapper;
import com.mapper.GoodsTypeMapper;

public class GoodsTypeService {
	public GoodsType findById(int typeId){
		SqlSession sqlSession = null;
		try{
			String resource = "mybatis.xml";
			Reader reader = Resources.getResourceAsReader(resource);
			SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
			sqlSession = ssf.openSession();
			GoodsTypeMapper goodsTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
			GoodsType goodsType = goodsTypeMapper.findById(typeId);
			GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
			List<Goods> goodsList = goodsMapper.findByType(typeId);
			// 组装
			goodsType.setGoodsList(goodsList);
			return goodsType;
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally{
			sqlSession.close();
		}
	}
}

5.编写测试类。

package com.test;

import org.junit.Before;
import org.junit.Test;

import com.bean.GoodsType;
import com.service.GoodsTypeService;


public class test {
	
	
	public void test01() {
		GoodsTypeService goodsTypeService = new GoodsTypeService();
		GoodsType goodsType = goodsTypeService.findById(1);
		System.out.println(goodsType);
	}
	
	
	
	@Test 
	public void testF(){
		test01();
	}
	
	@Before
	public void before() {
		
	}
}

数据库下载: 本地下载
项目代码: 本地下载

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注