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