mybatis接口用法
1、注意要點
1 namespace與接口全限定名一致
2 id和抽象函數保持一致
3 參數類型與返回類型保持一致
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//在方法前執行
public void setUp() {
try {
InputStream is= Resources.getResourceAsStream("mybatis-config.xml");//獲取主配置文件
factory=new SqlSessionFactoryBuilder().build(is);//創建工廠對象
sqlSession = factory.openSession(true);//獲取SqlSession(相當于Connection)
} catch (IOException e) {
e.printStackTrace();
}
}
@After//在方法執行后執行
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配置
創建一個db.properties文件,內容如下:
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日志,內容如下:
# 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>
<!-- 日志的實現類,可以不寫,會自動匹配 -->
<setting name="logImpl" value="LOG4J"/>
<!-- 將數據庫字段的下劃線自動轉為駝峰命名 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 自動映射,FULL表示無論是否關聯都進行自動映射 -->
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
別名
<!-- 指定包下面的所有實體類都會將類名作為別名 -->
<typeAliases>
<package name="com.study.entity"/>
</typeAliases>
<!-- 使用的使用直接使用別名Goods,不用寫全限定名 -->
<select id="selectGoodsById" resultType="Goods">
select * from goods where gid=4
</select>
輸出映射
resultType
當數據庫字段與屬性名一致時使用
resultMap
1、解決數據庫字段與屬性名不一致
2、關聯(可以使用別名來解決)
注意:resultType與resultMap不能同時使用!
關聯
關聯結果 resultMap與resultMap嵌套
public class Goods {
private GoodType goodsType;//屬性是pojo 普通的java對象
}
<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"/>
<!--關聯結果-->
<association property="goodsType" resultMap="goodTypeResultMap"/>
</resultMap>
<resultMap id="goodTypeResultMap" type="GoodType"/>
注意:resultMap的id和association的resultMap應該保持一致,resultMap中的type="GoodType",GoodType為創建的類
關聯查詢 resultMap與select嵌套
<resultMap id="goodsResultMap" type="Goods">
<id property="gid" column="gid"/>
<!--關聯結果-->
<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 ,遇到類似的錯誤提示就去xml文件中檢查id是否一致(建議最好復制粘貼)