[successbox title=”Mapper是什么”]
在实际开发中,不直接使用SqlSession的实例,而是使用Mapper接口,构建dao层。映射器是你创建的绑定映射语句的接口(interface),映射器接口的实例可以从SqlSession中获得。
[/successbox]
1.创建实体类(数据库也要对应创建)。
[sourcecode language=”java” title=”Goods.java”]
package com.bean;
public class Goods {
private Integer goodsId;
private String goodsName;
private Double goodsPrice;
private Integer goodsNum;
private Integer goodsType;
public Goods() {
}
public Goods(Integer goodsId, String goodsName, Double goodsPrice,
Integer goodsNum, Integer 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 Integer getGoodsType() {
return goodsType;
}
public void setGoodsType(Integer goodsType) {
this.goodsType = goodsType;
}
@Override
public String toString() {
return "Goods [goodsId=" + goodsId + ", goodsName=" + goodsName
+ ", goodsPrice=" + goodsPrice + ", goodsNum=" + goodsNum
+ ", goodsType=" + goodsType + "]";
}
}
[/sourcecode]
2.实体类对应的xml文件。
[sourcecode language=”xml” title=”Goods.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.bean.Goods">
<select id="findAll" resultMap="resultGoods">
select * from goods
</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="java.lang.Double" />
<result property="goodsNum" column="goods_num" javaType="int" />
<result property="goodsType" column="goods_type" javaType="int" />
</resultMap>
</mapper>
[/sourcecode]
3.在mybatis.xml注册Goods.xml。
[sourcecode language=”xml” title=”mybatis.xml”]
<!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" />
</mappers>
</configuration>
[/sourcecode]
4.编写Mapper接口。
[sourcecode language=”xml” title=”GoodsMapper.java”]
package com.mapper;
import java.util.List;
import com.bean.Goods;
public interface GoodsMapper {
public List<Goods> findAll();
}
[/sourcecode]
5.编写GoodsMapper接口的实现类(重点)。
[sourcecode language=”xml” title=”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="resultGoods">
select * from goods
</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>
[/sourcecode]
6.测试代码。
[sourcecode language=”xml” title=”test.java”]
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.Goods;
import com.mapper.GoodsMapper;
public class test {
private SqlSession sqlSession;
public void test03() {
try {
GoodsMapper goodsMapper = this.sqlSession.getMapper(GoodsMapper.class);
List<Goods> goodsList = goodsMapper.findAll();
for (Goods goods : goodsList) {
System.out.println(goods);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
this.sqlSession.close();
}
}
@Test
public void testF(){
test03();
}
@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();
}
}
}
[/sourcecode]
源代码下载(注意实体类源代码里面GoodsType实体类和实体类的xml文件都是没有用到的)。[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/源代码/MB003.rar[/bdbtn]