zoukankan      html  css  js  c++  java
  • MyBatis日记(四):MyBatis——insert、update、delete、select

    MyBatis简单增删改查操作,此处所做操作,皆是在之前创建的MyBatis的Hello world的工程基础上所做操作。

    首先在接口文件(personMapper.java)中,添加操作方法:

     1 package com.Aiden.dao;
     2 
     3 import java.util.List;
     4 
     5 import com.Aiden.domain.Person;
     6 
     7 public interface personMapper {
     8     /**
     9      * 添加人员
    10      * @param person
    11      */
    12     public void addPerson(Person person);
    13     /**
    14      * 删除人员
    15      * @param id
    16      */
    17     public void deletePerson(Integer id);
    18     /**
    19      * 更新人员
    20      * @param person
    21      */
    22     public void updatePerson(Person person);
    23     /**
    24      * 根据Id查找人员
    25      * @param id
    26      * @return
    27      */
    28     public Person findPersonById(Integer id);
    29     /**
    30      * 查询所有人员
    31      * @return
    32      */
    33     public List<Person> findPerson();
    34 }

    然后在映射文件(personMapper.xml)中添加用于实现的标签及SQL语句:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE mapper
     3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     5 
     6 <mapper namespace="com.Aiden.dao.personMapper">
     7 
     8     <resultMap type="Person" id="resultPerson">
     9         <id property="id" column="id"/>
    10         <result property="name" column="name"/>
    11         <result property="age" column="age"/>
    12     </resultMap>
    13 
    14     <insert id="addPerson" parameterType="Person">
    15         insert into t_person values (null,#{name},#{age})
    16     </insert>
    17     
    18     <update id="updatePerson" parameterType="Person">
    19         update t_person set name=#{name},age=#{age} where id=#{id}
    20     </update>
    21     
    22     <delete id="deletePerson" parameterType="Integer">
    23         delete from t_person where id=#{id}
    24     </delete>
    25     
    26     <select id="findPersonById" parameterType="Integer" resultType="Person">
    27         select * from t_person where id=#{id}
    28     </select>
    29     
    30     <select id="findPerson" resultType="Person">
    31         select * from t_person
    32     </select>
    33     
    34 </mapper>

    最后在Junit测试类(MyBatisDemo01.java)中添加测试方法:

     1 package com.Aiden.service;
     2 
     3 import java.util.List;
     4 
     5 import org.apache.ibatis.session.SqlSession;
     6 import org.apache.log4j.Logger;
     7 import org.junit.After;
     8 import org.junit.Before;
     9 import org.junit.Test;
    10 
    11 import com.Aiden.dao.personMapper;
    12 import com.Aiden.domain.Person;
    13 import com.Aiden.util.SqlSessionFactoryUtil;
    14 
    15 public class MybatisDemo01 {
    16     private static Logger logger=Logger.getLogger(MybatisDemo01.class);
    17     private static SqlSession sqlSession=null;
    18     private static personMapper personMapper=null;
    19 
    20     @Before
    21     public void setUp() throws Exception {
    22         sqlSession=SqlSessionFactoryUtil.openSession();
    23         personMapper=sqlSession.getMapper(personMapper.class); 
    24     }
    25 
    26     @After
    27     public void tearDown() throws Exception {
    28         sqlSession.close();
    29     }
    30 
    31     @Test
    32     public void testAddPerson() {
    33         logger.info("添加人员");
    34         Person person=new Person("波多野结衣",24);
    35         personMapper.addPerson(person);
    36         sqlSession.commit();
    37     }
    38     
    39     @Test
    40     public void testUpdatePerson() {
    41         logger.info("修改人员");
    42         Person person=new Person(2,"武藤兰",24);
    43         personMapper.updatePerson(person);
    44         sqlSession.commit();
    45     }
    46     
    47     @Test
    48     public void testFindPersonById() {
    49         logger.info("根据ID查询人员");
    50         Person person = personMapper.findPersonById(2);
    51         System.out.println(person);
    52     }
    53     
    54     @Test
    55     public void testFindPerson() {
    56         logger.info("查询所有人员");
    57         List<Person> allPerson = personMapper.findPerson();
    58         for (Person person : allPerson) {
    59             System.out.println(person);
    60         }
    61     }
    62     
    63     @Test
    64     public void testDeletePerson() {
    65         logger.info("删除人员");
    66         personMapper.deletePerson(2);
    67         sqlSession.commit();
    68     }
    69 
    70 }
  • 相关阅读:
    Map的迭代操作
    Vector/Arraylist与Linklist的区别
    Notepad++ 快捷键 大全
    集成 Tomcat 插件到 Eclipse 的过程
    十个最好的Java性能故障排除工具
    eclipse删除空行
    java解析xml文件四种方式
    Java解析XML文档(简单实例)——dom解析xml
    Log4j 2.0 使用说明
    根据引用jar包路径查找原JAR包
  • 原文地址:https://www.cnblogs.com/guoxiangyue/p/10796208.html
Copyright © 2011-2022 走看看