MyBatis日记(四):MyBatis——insert、update、delete、select
MyBatis简单增删改查操作,此处所做操作,皆是在之前创建的MyBatis的Hello world的工程基础上所做操作。
首先在接口文件(personMapper.java)中,添加操作方法:
package com.Aiden.dao;
import java.util.List;
import com.Aiden.domain.Person;
public interface personMapper {
/**
* 添加人员
* @param person
*/
public void addPerson(Person person);
/**
* 删除人员
* @param id
*/
public void deletePerson(Integer id);
/**
* 更新人员
* @param person
*/
public void updatePerson(Person person);
/**
* 根据Id查找人员
* @param id
* @return
*/
public Person findPersonById(Integer id);
/**
* 查询所有人员
* @return
*/
public List<Person> findPerson();
}
然后在映射文件(personMapper.xml)中添加用于实现的标签及SQL语句:
<?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.Aiden.dao.personMapper"> <resultMap type="Person" id="resultPerson">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap> <insert id="addPerson" parameterType="Person">
insert into t_person values (null,#{name},#{age})
</insert> <update id="updatePerson" parameterType="Person">
update t_person set name=#{name},age=#{age} where id=#{id}
</update> <delete id="deletePerson" parameterType="Integer">
delete from t_person where id=#{id}
</delete> <select id="findPersonById" parameterType="Integer" resultType="Person">
select * from t_person where id=#{id}
</select> <select id="findPerson" resultType="Person">
select * from t_person
</select> </mapper>
最后在Junit测试类(MyBatisDemo01.java)中添加测试方法:
package com.Aiden.service; import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.Aiden.dao.personMapper;
import com.Aiden.domain.Person;
import com.Aiden.util.SqlSessionFactoryUtil; public class MybatisDemo01 {
private static Logger logger=Logger.getLogger(MybatisDemo01.class);
private static SqlSession sqlSession=null;
private static personMapper personMapper=null; @Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
personMapper=sqlSession.getMapper(personMapper.class);
} @After
public void tearDown() throws Exception {
sqlSession.close();
} @Test
public void testAddPerson() {
logger.info("添加人员");
Person person=new Person("波多野结衣",24);
personMapper.addPerson(person);
sqlSession.commit();
} @Test
public void testUpdatePerson() {
logger.info("修改人员");
Person person=new Person(2,"武藤兰",24);
personMapper.updatePerson(person);
sqlSession.commit();
} @Test
public void testFindPersonById() {
logger.info("根据ID查询人员");
Person person = personMapper.findPersonById(2);
System.out.println(person);
} @Test
public void testFindPerson() {
logger.info("查询所有人员");
List<Person> allPerson = personMapper.findPerson();
for (Person person : allPerson) {
System.out.println(person);
}
} @Test
public void testDeletePerson() {
logger.info("删除人员");
personMapper.deletePerson(2);
sqlSession.commit();
} }
MyBatis日记(四):MyBatis——insert、update、delete、select的更多相关文章
- mysql字符集,insert,update,delete,select
发现有错误:数据太长了.//查看数据库的所有编码:show variables like 'character%';-----+| character_set_client | utf8 ...
- mybatis select/insert/update/delete
这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...
- 关于MyBatis mapper的insert, update, delete返回值
这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...
- mybatis insert update delete返回都是整型 0,1,增,删,改要提交事物
mybatis insert update delete返回都是整型 0,1, 没有扔 增,删,改要提交事物
- mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干
1.mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干 2.一般来说,事务是必须满足4个条件(ACID): Atomicity(原子性).Con ...
- PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)
原文: PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...
- mysql数据恢复 insert\update\delete 工具MyFlash
一.简介MyFlash是由美团点评公司技术工程部开发维护的一个回滚DML操作的工具.该工具通过解析v4版本的binlog,完成回滚操作.相对已有的回滚工具,其增加了更多的过滤选项,让回滚更加容易. 该 ...
- [Hive - LanguageManual] DML: Load, Insert, Update, Delete
LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...
- sql中同一个Trigger里同时包含Insert,Update,Delete
sql中同一个Trigger里同时包含Insert,Update,Delete SQLServer是靠Inserted表和Deleted表来处理的,判断一下就可以了,只不过比ORACLE麻烦一点 cr ...
- LINQ体验(9)——LINQ to SQL语句之Insert/Update/Delete操作
我们继续讲解LINQ to SQL语句,这篇我们来讨论Insert/Update/Delete操作.这个在我们的程序中最为常用了.我们直接看例子. Insert/Update/Delete操作 插入( ...
随机推荐
- LeetCode 104. Maximum Depth of Binary Tree(二叉树深度)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- openvswitch2.11.0修改源码后重新编译
一:推文 https://www.jianshu.com/p/923f49c290f5(可以删除运行当中的DataPath内核) https://github.com/ebiken/doc-netwo ...
- EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器启用https服务申请免费证书
背景分析 目前想在 web 上使用 HTTPS 的话, 你需要获得一个证书文件, 该证书由一个受浏览器信任的公司所签署. 一旦你获得了它, 你就在你的 web 服务器上指定其所在的位置, 以及与你关联 ...
- The last packet successfully received from the server was 1,480 milliseconds ago.
场景:一个上传接口,需要上传几十M的文件,文件中包含10几W的数据,然后对10+W的数据进行同步批量插入,每次批量插入1W.最后返回结果. 项目上线一段时间后,上传接口出现问题,数据库用的MySQL5 ...
- TensorFlow 8 bit模型量化
本文基本参考自这篇文章:8-Bit Quantization and TensorFlow Lite: Speeding up mobile inference with low precision ...
- 【Docker学习之四】Docker自定义容器镜像
环境 docker-ce-19.03.1-3.el7.x86_64 centos 7 一.使用已有容器创建镜像-docker commit1.查看本地已有镜像 [root@node105 ~]# do ...
- Redis Sentinel分布式集群
helm部署Redis哨兵分布式集群 Redis Sentinel集群 介绍 Redis Sentinel集群是由若干Sentinel节点组成的分布式集群,可以实现故障发现.故障自动转移.配置中心和客 ...
- LeetCode70——爬楼梯
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- Ubuntu16.04环境下的硬盘挂载
需求:在Ubuntu16.04系统下,挂载一个新的硬盘 第一步:查看目前已经存在的分区的状态 命令:df -l 如上图所示,并未看到要挂载的硬盘(sda)的状态. 第二步:查看计算机硬盘的状态(包括格 ...
- 设计模式 AOP,OOP
AOP.OOP在字面上虽然非常类似,但却是面向不同领域的两种设计思想. 简单说,AOP面向动词领域,OOP面向名词领域 AOP: (Aspect Oriented Programming) 面向切面编 ...