转载请注明出处哈: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); } } } }
相关推荐
Mybatis-Mate为 MyBatis-Plus 企业级模块,支持分库分表、数据审计、字段加密、数据绑定、数据权限、表结构自动生成 SQL 维护等高级特性。 介绍: mybatis-plus 企业(数据优雅处理)模块 营造良好的软件生态,合规...
mybatis-generatorjar包,生成的实体类自动带上swagger和lombok注解标签
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.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。 压缩包内容: mybatis-3.5.7.zip mybatis-3-mybatis-3.5.7.zip ...
mybatis-plus-annotation.jar MP 注解 jar 包 mybatis-plus 的注解 jar 包,包含了 mybatis-plus 注解功能 各个版本
Intellij Idea Mybatis插件主要功能: 提供Mapper接口与配置文件中对应SQL的导航 编辑XML文件时自动补全 根据Mapper接口, 使用快捷键生成xml文件及SQL标签 ResultMap中的property支持自动补全,支持级联...
使用mybatis时快捷下载,基本配置已经成功配置,可以直接使用并且...MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
Intellij Idea Mybatis插件主要功能: 提供Mapper接口与配置文件中对应SQL的导航 编辑XML文件时自动补全 根据Mapper接口, 使用快捷键生成xml文件及SQL标签 ResultMap中的property支持自动补全,支持级联...