Mybatis(dao層)基於注解模式的配置方式


1.還是要新建Shop.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.dao.ShopInforDao">

</mapper>

2.將它引入mybatis配置文件:

<mappers>
         <mapper resource="mapper/ShopMapper.xml"/>
         <!-- <package name="com.dao"/> -->
</mappers>

3.新建dao代碼ShopDao(一般放在dao包下):

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.FetchType;

import com.po.Shop;

//基於注解模式的配置方式
public interface ShopDao {
    
    //查詢單個商品信息
    @Select("select * from shop where id = #{id}")
    @Results({
        @Result(id=true,    column="id",    property="id"),
        @Result(column="name",                property="name"),
        @Result(column="information",        property="information"),
        @Result(column="sum",                property="sum"),
        @Result(column="price",                property="price")
    })
    Shop SelectShopById(Integer id);
    
    //查詢所有商品
    @Select("select * from shop")
    List<Shop> SelectShopAll();
    
    //插入單個商品
    @Insert("insert into shop(name,information,sum,price) values(#{name},#{information},#{sum},#{price})")
    @Options(useGeneratedKeys=true,keyProperty="id")
    int saveShop(Shop shop);
    
    //刪除單個商品
    @Delete("delete from shop where id = #{id}")
    void DeleteShopById(Integer id);
    
    //更新商品信息
    @Update("update shop set name=#{name},information=#{information},sum=#{sum},price=#{price} where id = #{id}")
    void UpdateShopById(Shop shop);
    
    //一對一查詢
    @Select("select * from shop where id = #{id}")
    @Results({
        @Result(id=true,    column="id",    property="id"),
        @Result(column="name",                property="name"),
        @Result(column="information",        property="information"),
        @Result(column="price",                property="price"),
        @Result(column="id",                property="shopInfor",    //id為傳過去的參數,shopInfor為shop表中屬性
        one=@One(
                select="yh.mapper.ShopInforDao.SelectShopInforByShopId",
                fetchType=FetchType.EAGER))        //立即加載
    })
    Shop OneToOneSelectShopAndShop_inforById(Integer id);
    
    
}

4.使用MybatisUtil工具類測試。

public class ShopTest {
    
    //查詢單個商品
    @Test
    public void TestSelectShopById() throws IOException{
        SqlSession session=MybatisUntil.openSession();
        ShopDao shopDao=session.getMapper(ShopDao.class);
        Shop shop=shopDao.SelectShopById(19);
        System.out.println(shop);
        session.close();
    }
}

5.測試結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM