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();
- }
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>
<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);
- }
- }
- }
- }
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); } } } }
时间: 2024-10-20 02:19:49