Mybatis添加&&删除&&更新
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添加&&删除&&更新的更多相关文章
- 利用 SQL Server Audit 审核哪些用户添加删除更新SQL Agent Job
有的时候我们需要下放权限给不用的用户,让他们自己能管理一部分SQL Agent Job,此时需要详细记录谁在什么时间修改了Job 甚至删除了Job, 我们可以使用SQL Server 的Audit帮助 ...
- ASP.NET MVC对WebAPI接口操作(添加,更新和删除)
昨天<怎样操作WebAPI接口(显示数据)>http://www.cnblogs.com/insus/p/5670401.html 既有使用jQuery,也有使作HttpClient来从数 ...
- 解决:错误 14274: 无法添加、更新或删除 SQL 服务器上的作业的问题一例
今天在修改 SQL服务器上 以前的作业任务,打算把作业的执行时间修改成新的时间,结果修改好点确定, 结果出现了以下错误! --------------------------- Microsoft ...
- ASP.NET网页动态添加、更新或删除数据行
ASP.NET网页动态添加.更新或删除数据行 看过此篇<ASP.NET网页动态添加数据行> http://www.cnblogs.com/insus/p/3247935.html的网友,也 ...
- SQL Server 如何添加删除外键、主键,以及更新自增属性
1.添加删除主键和外键 例如: -----删除主键约束DECLARE @NAME SYSNAMEDECLARE @TB_NAME SYSNAMESET @TB_NAME = 'Date'SELECT ...
- MySQL<添加、更新与删除数据>
添加.更新与删除数据 添加数据 为表中所有字段添加数据 INSERT INTO 表名(字段名1,字段名2,……) VALUES(值1,值2,……); insert into 表名 values(值1, ...
- iOS仿网易新闻栏目拖动重排添加删除效果
仿网易新闻栏目选择页面的基本效果,今天抽了点时间教大家如何实现UICollectionView拖动的效果! 其实实现起来并不复杂,这里只是基本的功能,没有实现细节上的修改,连UI都是丑丑的样子,随手画 ...
- SQL语句添加删除修改字段及一些表与字段的基本操作
用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200)2.删除字段 ALTER TABLE table_NA ...
- Java ArrayList正确循环添加删除元素方法及分析
在阿里巴巴Java开发手册中,有这样一条规定: 但是手册中并没有给出具体原因,本文就来深入分析一下该规定背后的思考. 一.foreach循环 foreach循环(Foreach loop)是计算机编程 ...
随机推荐
- python构建一个项目
二.实验步骤 2.1 实验准备 我们的实验项目名为 factorial. $ mkdir factorial $ cd factorial/ 2.2 主代码 我们给将要创建的 Python 模块取名为 ...
- Linux设置虚拟内存-创建和启用Swap交换区
如果你的服务器的总是报告内存不足,并且时常因为内存不足而引发服务被强制kill的话,在不增加物理内存的情况下,启用swap交换区作为虚拟内存是一个不错的选择,如果是SSD硬盘,正常读写速度都在300M ...
- CSS中z-index全解析
一.z-index解释 z-index属性决定了一个HTML元素的层叠级别,元素层叠级别是相对于元素在Z轴上(与X轴Y轴相对照)的位置而言.一个更高的z-index值意味着这个元素在叠层顺序中会更靠近 ...
- spring boot修改内置容器tomcat的服务端口
方式一 在spring boot的web 工程中,可以使用内置的web container.有时需要修改服务端口,可以通过配置类和@Configuration注解来完成. // MyConfigura ...
- SQLSERVER数据库置疑、可疑、脱机、单用户、紧急模式等的修复
数据库出现置疑.可疑.脱机.单用户.紧急模式主要是因为数据库的日志文件除了问题,2000和2008修复方式不一样,2008的修复脚本在2000中不适用,主要是不被2000识别. 假设数据库名为:eis ...
- bzoj 2115 路径xor和最大值
首先我们可以将这张图转化为一棵树,其中可能有好多环,肯定是从1走到N,其中可能经过好多环, 因为我们不走N在的一支,最后还是会走回来,因为a xor b xor b=a,所以相当于没走,就是走了个环, ...
- ZOJ 3599 K倍动态减法游戏
下面的文字辅助理解来自http://blog.csdn.net/tbl_123/article/details/24884861 博弈论中的 K倍动态减法游戏,难度较大,参看了好多资料才懵懂! 此题可 ...
- Linux内核线程之深入浅出【转】
转自:http://blog.csdn.net/yiyeguzhou100/article/details/53126626 [-] 线程和进程的差别 线程的分类 1 内核线程 2 轻 ...
- Ubuntu服务器安装node
查看Ubuntu系统的是32位还是64位 harvey@harvey:/Application$ uname -m #x86_64表示这是64位的系统 x86_64 2. 在nodejs官网ht ...
- QPS、TPS、PV等网站业务关键字释义
QPS:Query Per Second TPS:Transaction Per Second PV:Page View RT:Response Time UV:Unique Visitor DAU: ...