mapper

<?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.songyan.mapper.Customer">
<insert id="insertCustomer" parameterType="customer">
insert into tb_customer(id,username,job,phone)
values (#{id},#{username},#{job},#{phone})
</insert>
<delete id="deleteCustomer" parameterType="String">
delete from tb_customer where id=#{value}
</delete>
<update id="updateCustomer" parameterType="customer">
update tb_customer set name= #{username} where id=#{id}
</update>
</mapper>

test

    @Test
public void insert() throws IOException
{
//读取配置信息
String resource="applicationContext.xml";
//根据配置文件构建sqlsessionFactory
InputStream in=Resources.getResourceAsStream(resource);
//通过sqlsessionFactory创建sqlsession
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession =sqlSessionFactory.openSession();
//sqlsession执行sql并返回执行结果
Customer customer=new Customer();
customer.setId(4);
customer.setJob("job3");
customer.setPhone("22222");
customer.setUsername("zhangsan");
int num=sqlSession.insert("com.songyan.mapper.Customer.insertCustomer",customer);
//提交事务
sqlSession.commit();
//关闭sqlsession
sqlSession.close();
} @Test
public void delete() throws IOException
{
//读取配置信息
String resource="applicationContext.xml";
//根据配置文件构建sqlsessionFactory
InputStream in=Resources.getResourceAsStream(resource);
//通过sqlsessionFactory创建sqlsession
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession =sqlSessionFactory.openSession();
//sqlsession执行sql并返回执行结果
int num=sqlSession.delete("com.songyan.mapper.Customer.deleteCustomer",1);
//提交事务
sqlSession.commit();
//关闭sqlsession
sqlSession.close();
} @Test
public void update() throws IOException
{
//读取配置信息
String resource="applicationContext.xml";
//根据配置文件构建sqlsessionFactory
InputStream in=Resources.getResourceAsStream(resource);
//通过sqlsessionFactory创建sqlsession
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession =sqlSessionFactory.openSession();
//sqlsession执行sql并返回执行结果
Customer customer=new Customer();
customer.setId(4);
customer.setJob("job3");
customer.setPhone("22222");
customer.setUsername("zhaan");
System.out.println("1111");
int num=sqlSession.update("com.songyan.mapper.Customer.updateCustomer",customer);
System.out.println("222");
//提交事务
sqlSession.commit(true);
//关闭sqlsession
sqlSession.close();
}

Mybatis添加&&删除&&更新的更多相关文章

  1. 利用 SQL Server Audit 审核哪些用户添加删除更新SQL Agent Job

    有的时候我们需要下放权限给不用的用户,让他们自己能管理一部分SQL Agent Job,此时需要详细记录谁在什么时间修改了Job 甚至删除了Job, 我们可以使用SQL Server 的Audit帮助 ...

  2. ASP.NET MVC对WebAPI接口操作(添加,更新和删除)

    昨天<怎样操作WebAPI接口(显示数据)>http://www.cnblogs.com/insus/p/5670401.html 既有使用jQuery,也有使作HttpClient来从数 ...

  3. 解决:错误 14274: 无法添加、更新或删除 SQL 服务器上的作业的问题一例

      今天在修改 SQL服务器上 以前的作业任务,打算把作业的执行时间修改成新的时间,结果修改好点确定, 结果出现了以下错误! --------------------------- Microsoft ...

  4. ASP.NET网页动态添加、更新或删除数据行

    ASP.NET网页动态添加.更新或删除数据行 看过此篇<ASP.NET网页动态添加数据行> http://www.cnblogs.com/insus/p/3247935.html的网友,也 ...

  5. SQL Server 如何添加删除外键、主键,以及更新自增属性

    1.添加删除主键和外键 例如: -----删除主键约束DECLARE @NAME SYSNAMEDECLARE @TB_NAME SYSNAMESET @TB_NAME = 'Date'SELECT ...

  6. MySQL<添加、更新与删除数据>

    添加.更新与删除数据 添加数据 为表中所有字段添加数据 INSERT INTO 表名(字段名1,字段名2,……) VALUES(值1,值2,……); insert into 表名 values(值1, ...

  7. iOS仿网易新闻栏目拖动重排添加删除效果

    仿网易新闻栏目选择页面的基本效果,今天抽了点时间教大家如何实现UICollectionView拖动的效果! 其实实现起来并不复杂,这里只是基本的功能,没有实现细节上的修改,连UI都是丑丑的样子,随手画 ...

  8. SQL语句添加删除修改字段及一些表与字段的基本操作

    用SQL语句添加删除修改字段 1.增加字段     alter table docdsp    add dspcode char(200)2.删除字段     ALTER TABLE table_NA ...

  9. Java ArrayList正确循环添加删除元素方法及分析

    在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体原因,本文就来深入分析一下该规定背后的思考. 一.foreach循环 foreach循环(Foreach loop)是计算机编程 ...

随机推荐

  1. 设置eclipse控制台上的信息输入到某个文件

    转摘自:http://binary.duapp.com/2013/09/1511.html Run->Run Configurations->Common->File

  2. spoj p104 Matrix-Tree定理

    这个问题就是经典的生成树记数问题,题目为spoj p104 highway. 首先我们引入Matrix-Tree定理,由kirchhoff证明,定理的概述为,对于图G,我们定义若干个矩阵, D[G], ...

  3. 虚拟机vmware10.0.0里设置Suse Linux Enterprise 11系统静态IP上网

    http://blog.csdn.net/usbdrivers/article/details/50035615 首次在虚拟机里安装Suse Linux Enterprise 11,采用NET方式能够 ...

  4. python 微博评论获取的时候日期格式化

    # -*- coding: utf-8 -*- # @Time : 2018/03/05 10:57 # @Author : cxa # @File : testDataTime.py # @Soft ...

  5. 热安装NGINX并支持多站点SSL

    https://www.moonfly.net/801.html http://www.centoscn.com/image-text/config/2015/0423/5251.html 1.查看n ...

  6. PYTHON代理IP

    import urllib.request url = 'http://www.whatismyip.com.tw/' proxy_support = urllib.request.ProxyHand ...

  7. 架设Hmailserver+webmail邮件服务器

    架设Hmailserver+webmail邮件服务器   在安裝Hmailserver前先安裝Apache.php.mysql,如果你想懶點直接到http://www.phpnow.org下載phpn ...

  8. C# 调试程序时如何输入命令行参数

    调试程序时如何输入命令行参数http://www.a769.com/archives/320.html 开发命令行程序时,我们会疑惑,从那里输入参数呢?请看下面的教程,让你摆脱困扰. 1.点击菜单栏: ...

  9. [BZOJ3585]mex 主席树

    3585: mex Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1252  Solved: 639[Submit][Status][Discuss] ...

  10. Asp.net中的一个判断session是否合法的做法

    1 if (Session["UserID"] == "" || Session["UserID"] == null) 2 { 3     ...