转载请注明出处哈:http://carlosfu.iteye.com/blog/2238662
1. 定义sql映射xml文件:
3. 单元测试:
package com.sohu.tv.test.base; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.BeforeClass; /** * mybatis测试基类 * * @author leifu * @Date 2015年7月28日 * @Time 上午10:59:03 */ public class BaseTest { protected static SqlSessionFactory sessionFactory; /** * mybatis基础配置 */ protected final static String MYBATIS_CONF = "mybatis-base.xml"; @BeforeClass public static void setUp() throws IOException { Reader reader = Resources.getResourceAsReader(MYBATIS_CONF); sessionFactory = new SqlSessionFactoryBuilder().build(reader); } }
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.PlayerDao; /** * mybatis-xml方式配置 * * @author leifu * @Date 2015年7月28日 * @Time 上午9:54:07 */ public class PlayerMapperXmlTestV2 extends BaseTest { private SqlSession sqlSession; @Before public void before() { sqlSession = sessionFactory.openSession(true); } @After public void after() { sqlSession.close(); } @Test public void testGetPlayer() { PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class); Player player = playerDao.getPlayerById(2); System.out.println(player); } @Test public void testInsertPlayer() { PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class); playerDao.savePlayer(new Player(-1, "cr7", 30)); } @Test public void testDeletePlayer() { PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class); playerDao.deletePlayer(3); } @Test public void testUpdatePlayer() { PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class); playerDao.updatePlayer(new Player(3, "cafu", 45)); } @Test public void testSelectAllPlayers() { PlayerDao playerDao = sqlSession.getMapper(PlayerDao.class); List<Player> playerList = playerDao.selectAllPlayers(); if (playerList != null && !playerList.isEmpty()) { System.out.println("playerList size: " + playerList.size()); for (Player player : playerList) { System.out.println(player); } } } }
4. 操作视频: