【MyBatis】映射案例的最佳解决方案
本文最后更新于 1741 天前,其中的信息可能已经有所发展或是发生改变。

结构图:


1.创建实体类。

[sourcecode language=”java” title=”com.bean.Goods.java”]
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 + "]";
}

}
[/sourcecode]

[sourcecode language=”java” title=”com.bean.GoodsType.java”]
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 + "]";
}
}
[/sourcecode]

2.编写Mapper接口。

[sourcecode language=”java” title=”com.mapper.GoodsMapper.java”]
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);
}
[/sourcecode]

[sourcecode language=”java” title=”com.mapper.GoodsTypeMapper.java”]
package com.mapper;

import java.util.List;

import com.bean.GoodsType;

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

3.编写XML文件。

[sourcecode language=”xml” title=”com.mapper.GoodsMapper.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>
[/sourcecode]

[sourcecode language=”xml” title=”com.mapper.GoodsTypeMapper.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.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>
[/sourcecode]

[sourcecode language=”xml” title=”mybatis.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">
<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>
[/sourcecode]

4.编写service层。

[sourcecode language=”java” title=”com.service.GoodsTypeService.java”]
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();
}
}
}
[/sourcecode]

5.编写测试类。

[sourcecode language=”java” title=”com.test.teat.java”]
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() {

}
}
[/sourcecode]

数据库下载:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/数据库/spring3.sql[/bdbtn]
项目代码:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/源代码/MB008.rar[/bdbtn]

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇