Mybatis (dao layer) based on the configuration mode annotation

1. To create a new Shop.xml or file

<?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. It introduces mybatis profile:

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

3. New dao codes ShopDao (typically on the dao package):

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);
    
    
    List)
    the @Select ( "from the SELECT * Shop"query for all goods//<Shop> SelectShopAll (); 
    
    // insert individual commodity 
    @Insert ( "insert into shop (name , information, sum, price) values (# {name}, # {information}, # {sum}, # {price}) " ) 
    @Options (useGeneratedKeys = to true , the keyProperty =" ID " )
     int saveShop (Shop Shop); 
    
    // delete individual commodity 
    @Delete (" delete from Shop ID WHERE ID = # {} " )
     void DeleteShopById (ID Integer); 
    
    // update the product information 
    @Update ( "update Shop SET name = # {name}, information information} = {#, # SUM = SUM {}, {. price. price = #} # {WHERE ID = ID}" )
     void UpdateShopById (Shop Shop); 
    
    // one query
    @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",    ,= "yh.mapper.ShopInforDao.SelectShopInforByShopId"
                the SELECT@One (
        One =the above mentioned id parameter is passed in the past, shopInfor to shop table attribute//
                fetchType = FetchType.EAGER))         // load immediately 
    }) 
    Shop OneToOneSelectShopAndShop_inforById (ID Integer); 
    
    
}

4. MybatisUtil testing tools.

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. Test results:

 

Guess you like

Origin www.cnblogs.com/yanghe123/p/11708553.html