MyBatis系列目录--3. Mybatis注解

转载请注明出处哈:http://carlosfu.iteye.com/blog/2238662


 

1. PlayerDao注解方式实现
package com.sohu.tv.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Insert;
import com.sohu.tv.bean.Player;
/**
 * 注解方式实现PlayerDao
 * @author leifu
 * @Date 2015年7月28日
 * @Time 上午10:16:39
 */
public interface PlayerAnnotationDao {
    @Insert("insert into players(name, age) values(#{name}, #{age})")
    public int savePlayer(Player player);
  
    @Delete("delete from players where id=#{id}")
    public int deletePlayer(int id);
     
    @Update("update players set name=#{name},age=#{age} where id=#{id}")
    public int updatePlayer(Player player);
     
    @Select("select id,name,age from players where id=#{id}")
    public Player getPlayerById(int id);
     
    @Select("select id,name,age from players")
    public List<Player> selectAllPlayers();
}

 

2. mybatis-base.xml添加注解相关配置
<mappers>
    <mapper class="com.sohu.tv.mapper.PlayerAnnotationDao"/>
</mappers>

 

3. 单元测试:
package com.sohu.tv.test.mapper;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.sohu.tv.bean.Player;
import com.sohu.tv.mapper.PlayerAnnotationDao;
/**
 * mybatis-annotation实现方式配置
 * 
 * @author leifu
 * @Date 2015年7月28日
 * @Time 上午9:54:07
 */
public class PlayerMapperAnnotationTest extends BaseTest {
    private SqlSession sqlSession;
    @Before
    public void before() {
        sqlSession = sessionFactory.openSession(true);
    }
    @After
    public void after() {
        sqlSession.close();
    }
    @Test
    public void testGetPlayer() {
        PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);
        Player player = playerAnnotationDao.getPlayerById(1);
        System.out.println(player);
    }
    @Test
    public void testInsertPlayer() {
        PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);
        playerAnnotationDao.savePlayer(new Player(-1, "carlos", 34));
    }
    @Test
    public void testDeletePlayer() {
        PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);
        playerAnnotationDao.deletePlayer(4);
    }
    @Test
    public void testUpdatePlayer() {
        PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);
        playerAnnotationDao.updatePlayer(new Player(3, "ronaldo", 39));
    }
    @Test
    public void testSelectAllPlayers() {
        PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);
        List<Player> playerList = playerAnnotationDao.selectAllPlayers();
        if (playerList != null && !playerList.isEmpty()) {
            System.out.println("playerList size: " + playerList.size());
            for (Player player : playerList) {
                System.out.println(player);
            }
        }
    }
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值