<insert id="doCreate" parameterType="News"><!--添加数据-->
INSERT INTO news(title,content) VALUES (#{title},#{content}) ;
<selectKey keyColumn="nid" keyProperty="nid" order="AFTER" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
<update id="doEdit" parameterType="News"> <!--更新数据-->
UPDATE news SET title=#{title},content=#{content} WHERE nid=#{nid}
</update>
<delete id="doRemove" parameterType="java.lang.Long"><!--删除数据-->
DELETE FROM news WHERE nid=#{xiaoyu}
</delete>

查询数据(添加到News.xml)

  <select id="findById" parameterType="java.lang.Long" resultType="News"><查询数据>
SELECT nid,title,content FROM news WHERE nid=#{wodenid} ;
</select>
<select id="findAll" resultType="News">
SELECT nid,title,content FROM news ;
</select>
<select id="findAllTitle" resultType="News">
SELECT nid,title FROM news ;
</select>
<select id="findSplit" resultType="News" parameterType="java.util.Map"> <分页查询>
SELECT nid,title,content FROM news WHERE ${column} LIKE #{keyword} LIMIT #{start},#{lineSize} ;
</select>
<select id="getAllCount" resultType="java.lang.Long">
SELECT COUNT(*) FROM news ;
</select>

数据更新测试

     @Test
public void testDelete() throws Exception { //删除
System.out.println("数据删除:" + MyBatisSessionFactory.getSession()
.update("com.lion.mapper.NewsNS.doRemove", 9L));
MyBatisSessionFactory.getSession().commit();
MyBatisSessionFactory.close();
}
@Test
public void testEdit() throws Exception { //修改
News vo = new News();
vo.setTitle("人生最舒服的日子?");
vo.setContent("你是个屌丝,梦里成为富翁");
vo.setNid(9L); // 修改数据时需要设置准确的编号
System.out.println("数据修改:" + MyBatisSessionFactory.getSession().update("com.lion.mapper.NewsNS.doEdit", vo));
MyBatisSessionFactory.getSession().commit();
MyBatisSessionFactory.close();
MyBatisSessionFactory.getSession().commit();
MyBatisSessionFactory.close();
}
@Test
public void testSplit() throws Exception { //分页查询
Map<String,Object> splitParams = new HashMap<String,Object>() ; // 保存分页相关参数
splitParams.put("column", "title");
splitParams.put("keyword", "%%");
splitParams.put("start", 5); // (currentPage - 1) * lineSize
splitParams.put("lineSize", 5);
List<News> all = MyBatisSessionFactory.getSession().selectList("com.lion.mapper.NewsNS.findSplit",splitParams);
Long count = MyBatisSessionFactory.getSession().selectOne("com.lion.mapper.NewsNS.getAllCount",splitParams);
System.out.println("【分页数据】" + all);
System.out.println("【数据行统计】" + count);
MyBatisSessionFactory.close();
}

Mybatis数据基本操作的更多相关文章

  1. MyBatis基础:MyBatis数据基本操作(2)

    1. MyBatis映射器 2. MyBatis数据基本操作 示例项目结构: <project xmlns="http://maven.apache.org/POM/4.0.0&quo ...

  2. Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作

    详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...

  3. Mybatis数据操作

    Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作   详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) M ...

  4. Spring Boot 2.X(二):集成 MyBatis 数据层开发

    MyBatis 简介 概述 MyBatis 是一款优秀的持久层框架,支持定制化 SQL.存储过程以及高级映射.它采用面向对象编程的方式对数据库进行 CRUD 的操作,使程序中对关系数据库的操作更方便简 ...

  5. Mybatis数据的增删改查

    数据: Student{id int,name String ,age int} 配置mybatis-config.xml <?xml version="1.0" encod ...

  6. MyBatis数据持久化(十一)Mybatis3、Spring4、Struts2整合开发

    上一节我们將Mybatis和Spring4进行整合,本节向大家介绍Mybatis在Web开发中的应用,并与主流的MVC框架Struts2进行整合. 我们首先需要获取Struts2框架,Struts2官 ...

  7. MyBatis数据持久化(十)与Spring4整合

    前面几节介绍了mybatis的基本使用方法,本节主要介绍如何使用mybatis与主流的IoC容器Spring进行整合. 我们首先需要获取Spring框架的jar文件,在写本文时spring的最新Rel ...

  8. MyBatis数据持久化(七)多表连接查询

    本节继续以多表连接查询的案例介绍使用resultMap的好处,对于两张以上的表进行关联查询,当我们有选择的从不同表查询所需字段时,使用resultMap是相当方便的.例如我们有两张表,分别为用户表Us ...

  9. MyBatis数据持久化(六)resultMap使用

    resultMap是MyBatis最强大也是最重要的特性,使用它我们可以將查询结果或者sql输入条件和一个HashMap的key进行映射,大大简化了sql查询条件的输入,而且使得数据的查询变得非常灵活 ...

随机推荐

  1. Libreoffice/Office:禁止首字母自动大写功能

     造冰箱的大熊猫@cnblogs 2019/1/24 在LibreOffice(5.1.6.2)中,要禁止或者使能首字母自动大写功能,点击菜单项“Tools>>AutoCorrect Op ...

  2. 回调函数(callback) python / c++ 演示

    什么是回调函数? 我们绕点远路来回答这个问题. 编程分为两类:系统编程(system programming)和应用编程(application programming).所谓系统编程,简单来说,就是 ...

  3. (转载)Zookeeper的功能以及工作原理

    本文转载自:https://www.cnblogs.com/felixzh/p/5869212.html 1.ZooKeeper是什么?       ZooKeeper是一个分布式的,开放源码的分布式 ...

  4. 一、Git的一些命令操作----创建版本库、增加文件到Git库、时光机穿梭、远程仓库

    具体详细教程请链接:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 我这里只是记录 ...

  5. Linux 安全工具之extundelete误删除恢复

    一:前言 在Linux中,我们通常会因为误删除文件而感到烦恼,rm -rf +文件  rf参数简直就是每个运维的噩梦,但是大家想过没,为什么删除文件那么快呢,为什么我们copy文件的时候那么慢. 其实 ...

  6. kernel hacking的一些网站

    很全面的网站,下面的网站基本都可以从该地址找到. 新手必备 subscrible/unsubscrible mail list mail list archive kernel git mainlin ...

  7. 标准C++常用头文件及描述

    #include <algorithm> //STL 通用算法 #include <bitset> //STL 位集容器 #include <cctype> //字 ...

  8. #ifdef _WIN32的来源

    宏定义:#ifdef _WIN32   由编译器(ml.exe/ml64.exe)内部定义的.具体描述是: _WIN32:Defined for applications for Win32 and ...

  9. 内存数据库:Redis与Memcached的区别

    Redis与Memcached的区别 传统MySQL+ Memcached架构遇到的问题 实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都 ...

  10. Java界面程序实现图片的放大缩小

    Java界面程序实现图片的放大缩小.这个程序简单地实现了图片的打开.保存.放大一倍.缩小一倍和固定缩放尺寸,但是并没有过多的涵盖对图片的细节处理,只是简单地实现了图片大小的放缩. 思维导图如下: 效果 ...