你的位置:首页 > Java教程

[Java教程]MyBatis系列目录


 

1. PlayerDao注解方式实现

Java代码 复制代码 收藏代码

  1. package com.sohu.tv.mapper;  
  2. import java.util.List;  
  3. import org.apache.ibatis.annotations.Delete;  
  4. import org.apache.ibatis.annotations.Select;  
  5. import org.apache.ibatis.annotations.Update;  
  6. import org.apache.ibatis.annotations.Insert;  
  7. import com.sohu.tv.bean.Player;  
  8. /** 
  9.  * 注解方式实现PlayerDao 
  10.  * @author leifu 
  11.  * @Date 2015年7月28日 
  12.  * @Time 上午10:16:39 
  13.  */  
  14. public interface PlayerAnnotationDao {  
  15.     @Insert("insert into players(name, age) values(#{name}, #{age})")  
  16.     public int savePlayer(Player player);  
  17.     
  18.     @Delete("delete from players where id=#{id}")  
  19.     public int deletePlayer(int id);  
  20.        
  21.     @Update("update players set name=#{name},age=#{age} where id=#{id}")  
  22.     public int updatePlayer(Player player);  
  23.        
  24.     @Select("select id,name,age from players where id=#{id}")  
  25.     public Player getPlayerById(int id);  
  26.        
  27.     @Select("select id,name,age from players")  
  28.     public List<Player> selectAllPlayers();  
  29. }  

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. 收藏代码

  1. <mappers>  
  2.     <mapper class="com.sohu.tv.mapper.PlayerAnnotationDao"/>  
  3. </mappers>  

<mappers>  <mapper /></mappers>

 

【java框架源码下载】

3. 单元测试:

Java代码 复制代码 收藏代码

  1. package com.sohu.tv.test.mapper;  
  2. import java.util.List;  
  3. import org.apache.ibatis.session.SqlSession;  
  4. import org.junit.After;  
  5. import org.junit.Before;  
  6. import org.junit.Test;  
  7. import com.sohu.tv.bean.Player;  
  8. import com.sohu.tv.mapper.PlayerAnnotationDao;  
  9. /** 
  10.  * mybatis-annotation实现方式配置 
  11.  *  
  12.  * @author leifu 
  13.  * @Date 2015年7月28日 
  14.  * @Time 上午9:54:07 
  15.  */  
  16. public class PlayerMapperAnnotationTest extends BaseTest {  
  17.     private SqlSession sqlSession;  
  18.     @Before  
  19.     public void before() {  
  20.         sqlSession = sessionFactory.openSession(true);  
  21.     }  
  22.     @After  
  23.     public void after() {  
  24.         sqlSession.close();  
  25.     }  
  26.     @Test  
  27.     public void testGetPlayer() {  
  28.         PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);  
  29.         Player player = playerAnnotationDao.getPlayerById(1);  
  30.         System.out.println(player);  
  31.     }  
  32.     @Test  
  33.     public void testInsertPlayer() {  
  34.         PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);  
  35.         playerAnnotationDao.savePlayer(new Player(-1, "carlos", 34));  
  36.     }  
  37.     @Test  
  38.     public void testDeletePlayer() {  
  39.         PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);  
  40.         playerAnnotationDao.deletePlayer(4);  
  41.     }  
  42.     @Test  
  43.     public void testUpdatePlayer() {  
  44.         PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);  
  45.         playerAnnotationDao.updatePlayer(new Player(3, "ronaldo", 39));  
  46.     }  
  47.     @Test  
  48.     public void testSelectAllPlayers() {  
  49.         PlayerAnnotationDao playerAnnotationDao = sqlSession.getMapper(PlayerAnnotationDao.class);  
  50.         List<Player> playerList = playerAnnotationDao.selectAllPlayers();  
  51.         if (playerList != null && !playerList.isEmpty()) {  
  52.             System.out.println("playerList size: " + playerList.size());  
  53.             for (Player player : playerList) {  
  54.                 System.out.println(player);  
  55.             }  
  56.         }  
  57.     }  
  58. }  

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);      }    }  }}