`
carlosfu
  • 浏览: 571924 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
Ba8b5055-9c58-3ab0-8a1c-e710f0495d2c
BigMemory实战与理...
浏览量:30026
53b2087e-c637-34d2-b61d-257846f73ade
RedisCluster开...
浏览量:149142
C9f66038-7478-3388-8086-d20c1f535495
缓存的使用与设计
浏览量:122714
社区版块
存档分类
最新评论

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

  

2
2
分享到:
评论

相关推荐

    mybatis-mate-examples-master.zip

    Mybatis-Mate为 MyBatis-Plus 企业级模块,支持分库分表、数据审计、字段加密、数据绑定、数据权限、表结构自动生成 SQL 维护等高级特性。 介绍: mybatis-plus 企业(数据优雅处理)模块 营造良好的软件生态,合规...

    mybatis-generator-core-1.3.7.jar

    mybatis-generatorjar包,生成的实体类自动带上swagger和lombok注解标签

    mybatis-plus-generator.rar

    3. 集成API文档接口工具Swagger2,只需根据实际情况修改@ApiModel注解的Value和description内容即可; ##开发环境 - Springboot 2.2.2.RELEASE - mybatis-plus-generator 3.3.0 - mysql-connector-java 8.0.18 ...

    mybatis-plus-annotation-3.4.3.3.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.5.3.2.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.5.3.1.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.5.3.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.0-RC3.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.0.3.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.1.0.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.2.0.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.0.6.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.3.0.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.5.0.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis-plus-annotation-3.4.3.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    mybatis3.5.7.zip

    MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。 压缩包内容: mybatis-3.5.7.zip mybatis-3-mybatis-3.5.7.zip ...

    mybatis-plus-annotation-3.0-RELEASE.jar

    mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本

    MybatisX-idea.0.1.0.jar

    Intellij Idea Mybatis插件主要功能: 提供Mapper接口与配置文件中对应SQL的导航 编辑XML文件时自动补全 根据Mapper接口, 使用快捷键生成xml文件及SQL标签 ResultMap中的property支持自动补全,支持级联...

    mybatis-3-mybatis-3.5.4.zip

    使用mybatis时快捷下载,基本配置已经成功配置,可以直接使用并且...MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

    MybatisX-idea.0.1.0.jarMapper找到XML

    Intellij Idea Mybatis插件主要功能: 提供Mapper接口与配置文件中对应SQL的导航 编辑XML文件时自动补全 根据Mapper接口, 使用快捷键生成xml文件及SQL标签 ResultMap中的property支持自动补全,支持级联...

Global site tag (gtag.js) - Google Analytics