介绍

在实际开发中,不直接使用SqlSession的实例,而是使用Mapper接口,构建dao层。映射器是你创建的绑定映射语句的接口(interface),映射器接口的实例可以从SqlSession中获得。

案例代码下载(注意实体类源代码里面GoodsType实体类和实体类的xml文件都是没有用到的):本地下载

案例

1.创建实体类

Goods.java

java
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
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 + "]"; } }

2.实体类对应的xml文件

Goods.xml

xml
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
<?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>

3.在mybatis.xml注册Goods.xml

mybatis.xml

xml
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
<!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" /> </mappers> </configuration>

4.编写Mapper接口

GoodsMapper.java

java
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
package com.mapper; import java.util.List; import com.bean.Goods; public interface GoodsMapper { public List<Goods> findAll(); }

5.编写GoodsMapper接口的实现类

重点

GoodsMapper.xml

xml
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
<?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>

6.测试代码

test.java

java
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
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(); } } }