MyBatis系列目录--2. Mybatis的简单CRUD(含操作视频)
转载请注明出处哈:http://carlosfu.iteye.com/blog/2238662
1. 定义sql映射xml文件:
playerMapper.xml
<?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.sohu.tv.mapper.PlayerDao"> <select id="getPlayerById" parameterType="int" resultType="com.sohu.tv.bean.Player"> select id,name,age from players where id=#{id} </select> <insert id="savePlayer" parameterType="com.sohu.tv.bean.Player"> insert into players(name,age) values(#{name}, #{age}); </insert> <delete id="deletePlayer" parameterType="int"> delete from players where id=#{id} </delete> <update id="updatePlayer" parameterType="com.sohu.tv.bean.Player"> update players set name=#{name},age=#{age} where id=#{id} </update> <select id="selectAllPlayers" resultType="com.sohu.tv.bean.Player"> select id,name,age from players </select> </mapper>
2. Dao
package com.sohu.tv.mapper; import java.util.List; import com.sohu.tv.bean.Player; /** * 注解方式实现PlayerDao * * @author leifu * @Date 2015年7月28日 * @Time 上午10:16:39 */ public interface PlayerDao { public int savePlayer(Player player); public int deletePlayer(int id); public int updatePlayer(Player player); public Player getPlayerById(int id); public List<Player> selectAllPlayers(); } |
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. 操作视频:
相关推荐
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
dongxurr 2020-08-08
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
Dullonjiang 2020-07-30
jimgreatly 2020-07-27
liqiancao 2020-07-26
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26
helloxusir 2020-07-25
牧场SZShepherd 2020-07-20