mybatis的增删改查
一、配置文件方式
mapper.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.hy.mybatis.test.News"> <insert id="insertNews" parameterType="com.hy.mybatis.test.News">
insert into news(title,content,author,createdate) values(#{title},#{content},#{author},#{createdate})
</insert> <delete id="deleteNews" parameterType="int">
delete from news where id= #{id}
</delete> <update id="updateNews" parameterType="com.hy.mybatis.test.News">
update news set author = #{author} where id = #{id}
</update> <select id="getAllNews" resultType="com.hy.mybatis.test.News">
select * from news
</select> <select id="getNews" parameterType="int" resultType="com.hy.mybatis.test.News">
select * from news where id=#{id}
</select>
</mapper>
引入:
<mappers>
<mapper resource="com/hy/mybatis/test/News.xml"/>
</mappers>
测试:
public static SqlSession getSqlSession() {
InputStream is = MyBatisUtil.class.getClassLoader().getResourceAsStream("mybatis.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
return factory.openSession(true);
}
封装获取sqlSession的方法,这里采用自动提交事务的方式。
@Test
public void testAddNews() {
SqlSession session = MyBatisUtil.getSqlSession();
String statement = "com.hy.mybatis.test.News.insertNews";
int insert = session.insert(statement, new News(-1, "android学习", "学习",
"李博", new Date()));
System.out.println(insert);
} @Test
public void testDeleteNews() {
SqlSession session = MyBatisUtil.getSqlSession();
String statement = "com.hy.mybatis.test.News.deleteNews";
int delete = session.delete(statement, 2);
System.out.println(delete);
} @Test
public void testUpdateNews() {
SqlSession session = MyBatisUtil.getSqlSession();
String statement = "com.hy.mybatis.test.News.updateNews";
int update = session.update(statement, new News(3, "", "", "xx",
new Date()));
System.out.println(update);
} @Test
public void testgetAllNews() {
SqlSession session = MyBatisUtil.getSqlSession();
String statement = "com.hy.mybatis.test.News.getAllNews";
List<News> list = session.selectList(statement);
System.out.println(list);
}
二、注解方式
写一个mapperInterface:
package com.hy.mybatis.test; import java.util.List; import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; public interface NewsInterface { @Insert("insert into news(title,content,author,createdate) values(#{title},#{content},#{author},#{createdate})")
public int add(News news); @Delete("delete from news where id= #{id}")
public int deleteById(int id); @Update("update news set author = #{author} where id = #{id}")
public int update(News news); @Select("select * from news")
public List<News> getAllNews(); }
测试代码:
@Test
public void testAddNews2() {
SqlSession session = MyBatisUtil.getSqlSession();
NewsInterface newsInterface= session.getMapper(NewsInterface.class);
int add = newsInterface.add(new News(-1, "android学习", "学习",
"李博", new Date()));
System.out.println(add);
}
先获取一个mapperInterface的示例,然后调用就行了!!
mybatis的增删改查的更多相关文章
- 学习MyBatis必知必会(5)~了解myBatis的作用域和生命周期并抽取工具类MyBatisUtil、mybatis执行增删改查操作
一.了解myBatis的作用域和生命周期[错误的使用会导致非常严重的并发问题] (1)SqlSessionFactoryBuilder [ 作用:仅仅是用来创建SqlSessionFactory,作用 ...
- MyBatis的增删改查。
数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改,并且对程序接口做了一些调整,以及对一些问题进行了解答. 1.调整后的结构图: 2.连接数据库文件配置分离: 一般的程序都会把连 ...
- MyBatis批量增删改查操作
前文我们介绍了MyBatis基本的增删该查操作,本文介绍批量的增删改查操作.前文地址:http://blog.csdn.net/mahoking/article/details/43673741 ...
- 上手spring boot项目(三)之spring boot整合mybatis进行增删改查的三种方式。
1.引入依赖. <!--springboot的web起步依赖--><dependency> <groupId>org.springframework.boot< ...
- 上手spring boot项目(三)之spring boot整合mybatis进行增删改查
使用mybatis框架进行增删改查大致有两种基础方式,一种扩展方式.两种基础方式分别是使用xml映射文件和使用方法注解.扩展方式是使用mybatis-plus的方式,其用法类似于spring-data ...
- 从0开始完成SpringBoot+Mybatis实现增删改查
1.准备知识: 1)需要掌握的知识: Java基础,JavaWeb开发基础,Spring基础(没有Spring的基础也可以,接触过Spring最好),ajax,Jquery,Mybatis. 2)项目 ...
- Spring Boot入门系列(六)如何整合Mybatis实现增删改查
前面介绍了Spring Boot 中的整合Thymeleaf前端html框架,同时也介绍了Thymeleaf 的用法.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/z ...
- Mybatis实例增删改查(二)
创建实体类: package com.test.mybatis.bean; public class Employee { private Integer id; private String las ...
- mybatis的增删改查返回值小析(六)
本文验证了通过mybatis访问数据库时的,增删改查的返回值情况. 直接看代码. 1.service层 /** *@Author: Administrator on 2020/3/12 15:15 * ...
- ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)
ssm 框架实现增删改查 SpringBoot 项目整合 一.项目准备 1.1 ssm 框架环境搭建 1.2 项目结构图如下 1.3 数据表结构图如下 1.4 运行结果 二.项目实现 1. Emplo ...
随机推荐
- Xcode 7 ImageNamed 方法加载jpg图片失败
更新XCode7后 原来的Image.xcassets文件夹变成了Assets.xcassets 把01.jpg,02.jpg,03.png拖入这个文件夹中 UIImage* test1=[UIIma ...
- Java 异常处理机制和集合框架
一.实验目的 掌握面向对象程序设计技术 二.实验环境 1.微型计算机一台 2.WINDOWS操作系统,Java SDK,Eclipse开发环境 三.实验内容 1.Java异常处理机制涉及5个关键字:t ...
- 【转】第5篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:自动注册JS脚本+委托回调方法分析
作者: 牛A与牛C之间 时间: 2013-11-19 分类: 技术文章 | 暂无评论 | 编辑文章 主页 » 技术文章 » 第5篇:Xilium CefGlue 关于 CLR Object 与 JS ...
- 传递给函数的隐含参数:arguments及递归函数的实现
传递给函数的隐含参数:arguments当进行函数调用时,除了指定的参数外,还创建一个隐含的对象——arguments.arguments是一个类似数组但不是数组的对象,说它类似是因为它具有数组一样的 ...
- python ImportError: No module named 的问题
https://my.oschina.net/leejun2005/blog/109679 python中,每个py文件被称之为模块,每个具有__init__.py文件的目录被称为包.只要模块或者包所 ...
- jQuery Mobile_页面事件
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...
- 初试体验java多线程
现在个人电脑以及服务器都是多核cpu,如何提高多核cpu的利用率,就要用到多线程技术了. public class TestThread1 { ; static class PThread exten ...
- (五)Linux引导流程解析
目录 Linux引导流程 Linux运行级别 Linux启动服务管理 GRUB配置与应用 启动故障分析与解决 Linux引导流程 Linux系统引导流程如下图: 固件(Firmware)就是写入ERO ...
- Innodb 表修复(转)
摘要: 突然收到MySQL报警,从库的数据库挂了,一直在不停的重启,打开错误日志,发现有张表坏了.innodb表损坏不能通过repair table 等修复myisam的命令操作.现在记录下 ...