mybatis接口用法
1、注意要點(diǎn)
1 namespace與接口全限定名一致
2 id和抽象函數(shù)保持一致
3 參數(shù)類型與返回類型保持一致
4 java類名與xml文件名保存一致
舉例
public interface GoodsMapper {
Goods selectGoodsById();
int insertGoods(Goods goods);
}
<mapper namespace="com.study.mapper.GoodsMapper"><!--命名空間-->
<select id="selectGoodsById" resultType="com.study.entity.Goods">
select * from goods where gid=4
</select>
<insert id="insertGoods" parameterType="com.study.entity.Goods">
INSERT INTO Goods(gname,gprice,num,description,good_type,create_time)
VALUES (#{gname},#{gprice},#{num},#{description},#{goodtype},now())
</insert>
</mapper>
public class GoodsMapperTest {
SqlSessionFactory factory;
SqlSession sqlSession;
@Before//在方法前執(zhí)行
public void setUp() {
try {
InputStream is= Resources.getResourceAsStream("mybatis-config.xml");//獲取主配置文件
factory=new SqlSessionFactoryBuilder().build(is);//創(chuàng)建工廠對(duì)象
sqlSession = factory.openSession(true);//獲取SqlSession(相當(dāng)于Connection)
} catch (IOException e) {
e.printStackTrace();
}
}
@After//在方法執(zhí)行后執(zhí)行
public void tearDown() {
sqlSession.close();
}
@Test
public void selectGoodsById() throws Exception {
GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
Goods goods = mapper.selectGoodsById();
System.out.println(goods);
}
@Test
public void insertGoods() throws Exception {
GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
Goods goods = new Goods("lenovo", new BigDecimal(5000), "lianxiang", 200, 1);
mapper.insertGoods(goods);
}
}
mybatis-config.xml配置
創(chuàng)建一個(gè)db.properties文件,內(nèi)容如下:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///taobao?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=123
<!--加載配置文件-->
<properties resource="db.properties"/>
配置log4j日志,內(nèi)容如下:
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.study.mapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
<settings>
<!-- 日志的實(shí)現(xiàn)類,可以不寫,會(huì)自動(dòng)匹配 -->
<setting name="logImpl" value="LOG4J"/>
<!-- 將數(shù)據(jù)庫(kù)字段的下劃線自動(dòng)轉(zhuǎn)為駝峰命名 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 自動(dòng)映射,F(xiàn)ULL表示無(wú)論是否關(guān)聯(lián)都進(jìn)行自動(dòng)映射 -->
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
別名
<!-- 指定包下面的所有實(shí)體類都會(huì)將類名作為別名 -->
<typeAliases>
<package name="com.study.entity"/>
</typeAliases>
<!-- 使用的使用直接使用別名Goods,不用寫全限定名 -->
<select id="selectGoodsById" resultType="Goods">
select * from goods where gid=4
</select>
輸出映射
resultType
當(dāng)數(shù)據(jù)庫(kù)字段與屬性名一致時(shí)使用
resultMap
1、解決數(shù)據(jù)庫(kù)字段與屬性名不一致
2、關(guān)聯(lián)(可以使用別名來(lái)解決)
注意:resultType與resultMap不能同時(shí)使用!
關(guān)聯(lián)
關(guān)聯(lián)結(jié)果 resultMap與resultMap嵌套
public class Goods {
private GoodType goodsType;//屬性是pojo 普通的java對(duì)象
}
<select id="selectGoodsById" resultMap="goodsResultMap">
SELECT * FROM goods g INNER JOIN data_dictionary d ON g.good_type=d.value_id WHERE d.type_code='good_type' AND gid=4
</select>
<resultMap id="goodsResultMap" type="Goods">
<id property="gid" column="gid"/>
<!--關(guān)聯(lián)結(jié)果-->
<association property="goodsType" resultMap="goodTypeResultMap"/>
</resultMap>
<resultMap id="goodTypeResultMap" type="GoodType"/>
注意:resultMap的id和association的resultMap應(yīng)該保持一致,resultMap中的type="GoodType",GoodType為創(chuàng)建的類
關(guān)聯(lián)查詢 resultMap與select嵌套
<resultMap id="goodsResultMap" type="Goods">
<id property="gid" column="gid"/>
<!--關(guān)聯(lián)結(jié)果-->
<association property="goodsType" select="selectGoodType" column="good_type" javaType="GoodType"/>
</resultMap>
<select id="selectGoodsById" resultMap="goodsResultMap">
SELECT * FROM goods WHERE gid=6
</select>
<select id="selectGoods" resultMap="goodsResultMap">
SELECT * FROM goods
</select>
<select id="selectGoodsType" resultType="GoodType">
SELECT * FROM data_dictionary WHERE type_code="good_type" AND value_id=#{value}
</select>
@Test
public void selectGoodsById() throws Exception {
GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
Goods goods = mapper.selectGoodsById();
System.out.println(goods);
}
@Test
public void selectGoods() throws Exception {
GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
GoodType goodType = mapper.selectGoodsType(3);
System.out.println(goodType);
List<Goods> list = mapper.selectGoods();
for (Goods goods:list) {
System.out.println(goods);
}
}
public interface GoodsMapper {
Goods selectGoodsById();
int insertGoods(Goods goods);
List<Goods> selectGoods();
GoodType selectGoodsType(int value);
}
注意
Mapped Statements collection does not contain value for com.study.mapper.GoodsMapper.selectGoodType ,遇到類似的錯(cuò)誤提示就去xml文件中檢查id是否一致(建議最好復(fù)制粘貼)