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();
} }

最新文章

  1. CentOS-7下安装MySQL5.6.22
  2. jquery.ajax 跨域请求webapi,设置headers
  3. The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
  4. udev:renamed network interface eth0 to eth1
  5. HDU 1204 基础DP 非连续字段的最大和
  6. 用C#操作IIS创建虚拟目录和网站
  7. springmvc的ModelAttribute注解
  8. Yii2 设计模式——静态工厂模式
  9. SpringBoot集成netty实现客户端服务端交互和做一个简单的IM
  10. css之文本两端对齐
  11. iReport数据库连接找不到驱动
  12. OpenACC 书上的范例代码(Jacobi 迭代),part 2
  13. sql server 数据库学习
  14. summernote 文本编辑器使用时,选择上传图片、链接、录像时,弹出的对话框被遮挡住
  15. MacBook pro new 触控板手势及快捷键
  16. 如何使用千千静听为MP3添加专辑封面和文字信息
  17. SpringMVC可以配置多个拦截后缀*.action和.do等
  18. 196. Delete Duplicate Emails
  19. php代码审计5审计命令执行漏洞
  20. HDU2546饭卡---(DP 经典背包)

热门文章

  1. LeetCode 101. Symmetric Tree(镜像树)
  2. 安装 python 爬虫框架 Scrapy
  3. FFmpeg 的bug
  4. [转]10 Best GTK Themes for Ubuntu 18.04
  5. Gerrit - 一些基本用法
  6. 【layui】获取layui弹窗的index并关闭
  7. Rust基础
  8. TCP Socket 套接字 和 粘包问题
  9. libevent源码分析二--timeout事件响应
  10. Scala 系列(三)—— 流程控制语句