数据库脚本:本地下载
案例代码:本地下载
案例
1.创建实体类
Goods.java
package com.bean;
public class Goods {
private int goodsId;
private String goodsName;
private Double goodsPrice;
private int goodsNum;
private int goodsType;
public Goods() {
}
public Goods(int goodsId, String goodsName, Double goodsPrice,
int goodsNum, int goodsType) {
super();
this.goodsId = goodsId;
this.goodsName = goodsName;
this.goodsPrice = goodsPrice;
this.goodsNum = goodsNum;
this.goodsType = goodsType;
}
public int getGoodsId() {
return goodsId;
}
public void setGoodsId(int 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 int getGoodsNum() {
return goodsNum;
}
public void setGoodsNum(int goodsNum) {
this.goodsNum = goodsNum;
}
public int getGoodsType() {
return goodsType;
}
public void setGoodsType(int 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 int typeId;
private String typeName;
private List<Goods> goodsList;
public GoodsType() {
}
public GoodsType(int typeId, String typeName, List<Goods> goodsList) {
super();
this.typeId = typeId;
this.typeName = typeName;
this.goodsList = goodsList;
}
public int getTypeId() {
return typeId;
}
public void setTypeId(int 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.编写GoodsTypeMapper接口
GoodsTypeMapper.java
package com.mapper;
import java.util.List;
import com.bean.GoodsType;
public interface GoodsTypeMapper {
public List<GoodsType> findAll();
}
3.编写XML文件
(1)第一种,连接查询所有商品信息
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">
<resultMap type="com.bean.Goods" id="resultGoods">
<id property="goodsId" column="goods_id" javaType="int" />
<result property="goodsName" column="goods_name" javaType="java.lang.String" />
<result property="goodsPrice" column="goods_price" javaType="double" />
<result property="goodsNum" column="goods_num" javaType="int" />
<result property="goodsType" column="goods_type" javaType="int" />
</resultMap>
</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="resultGoodsType">
select * from goods_type
left join goods
on goods.goods_type = goods_type.typeId
</select>
<resultMap type="com.bean.GoodsType" id="baseGoodsType">
<id property="typeId" column="typeId" javaType="int" />
<result property="typeName" column="typeName" javaType="java.lang.String" />
</resultMap>
<resultMap type="com.bean.GoodsType" id="resultGoodsType" extends="baseGoodsType">
<collection property="goodsList" javaType="java.util.List"
ofType="com.bean.Goods" resultMap="com.mapper.GoodsMapper.resultGoods"/>
</resultMap>
</mapper>
(2)第二种,调用关联关系对象的Mapper的查询方法。
GoodsMapper.java
package com.mapper;
import java.util.List;
import com.bean.Goods;
public interface GoodsMapper {
public List<Goods> findByType(int goodsTypeId);
}
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="findByType" parameterType="int" resultMap="resultGoods">
select * from goods where goods_type = #{id}
</select>
<resultMap type="com.bean.Goods" id="resultGoods">
<id property="goodsId" column="goods_id" javaType="int" />
<result property="goodsName" column="goods_name" javaType="java.lang.String" />
<result property="goodsPrice" column="goods_price" javaType="double" />
<result property="goodsNum" column="goods_num" javaType="int" />
<result property="goodsType" column="goods_type" javaType="int" />
</resultMap>
</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="resultGoodsType">
select * from goods_type
</select>
<resultMap type="com.bean.GoodsType" id="baseGoodsType">
<id property="typeId" column="typeId" javaType="int" />
<result property="typeName" column="typeName" javaType="java.lang.String" />
</resultMap>
<resultMap type="com.bean.GoodsType" id="resultGoodsType" extends="baseGoodsType">
<collection property="goodsList" javaType="java.util.List"
ofType="com.bean.Goods" select="com.mapper.GoodsMapper.findByType" column="typeId" />
</resultMap>
</mapper>
4.编写mybatis.xml
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&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>
5.编写测试类
package com.test;
import java.io.IOException;
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 org.junit.Before;
import org.junit.Test;
import com.bean.GoodsType;
import com.mapper.GoodsTypeMapper;
public class test {
private SqlSession sqlSession;
public void test01() {
try {
GoodsTypeMapper goodsTypeMapper = this.sqlSession.getMapper(GoodsTypeMapper.class);
List<GoodsType> GoodsTypeList = goodsTypeMapper.findAll();
for (GoodsType goodsType : GoodsTypeList) {
System.out.println(goodsType);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
this.sqlSession.close();
}
}
@Test
public void testF(){
test01();
}
@Before
public void before() {
try {
String resource = "mybatis.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
this.sqlSession = ssf.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
}
评论