欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MyBatis3使用xml配置sql映射器

程序员文章站 2022-07-13 16:14:53
...
1. MyBatis3使用xml配置sql映射器

1) INSERT映射语句
2) UPDATE映射语句
3) DELETE映射语句
4) SELECT映射语句

引入junit
新建Folder,导入junit所用的jar包,右键 --> Build Path --> Add To Build Path
mybatis-3.2.8.jar


package com.andrew.mappers;
import java.util.List;
import com.andrew.model.Student;
public interface StudentMapper {
    public int add(Student student);
    public int update(Student student);
    public int delete(Integer id);
    public Student findById(Integer id);
    public List<Student> find();
}


<?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.andrew.mappers.StudentMapper">
    <resultMap type="Student" id="StudentResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
    </resultMap>
    <insert id="add" parameterType="Student">
        insert into t_student values(null, #{name}, #{age})
    </insert>
    <update id="update" parameterType="Student">
        update t_student set name=#{name},age=#{age} where id=#{id}
    </update>
    <delete id="delete" parameterType="Integer">
        delete from t_student where id=#{id}
    </delete>
    <select id="findById" parameterType="Integer" resultType="Student">
        select * from t_student where id=#{id}
    </select>
    <select id="find" resultMap="StudentResult">
        select * from t_student
    </select>
</mapper>


package com.andrew.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.andrew.mappers.StudentMapper;
import com.andrew.model.Student;
import com.andrew.util.SqlSessionFactoryUtil;
public class StudentJunitTest {
    private static Logger logger = Logger.getLogger(StudentJunitTest.class);
    private SqlSession sqlSession = null;
    private StudentMapper studentMapper = null;
    @Before
    public void setUp() throws Exception {
        sqlSession = SqlSessionFactoryUtil.openSession();
        studentMapper = sqlSession.getMapper(StudentMapper.class);
    }
    @After
    public void tearDown() throws Exception {
        sqlSession.close();
    }
    @Test
    public void testAdd() {
        logger.info("添加学生");
        Student student = new Student("王五",12);
        studentMapper.add(student);
        sqlSession.commit();
    }
    @Test
    public void testUpdate(){
        logger.info("修改学生");
        Student student = new Student(8, "王五2", 13);
        studentMapper.update(student);
        sqlSession.commit();
    }
    @Test
    public void testDelete(){
        logger.info("删除学生");
        studentMapper.delete(8);
        sqlSession.commit();
    }
    @Test
    public void testFindById(){
        logger.info("通过ID查找学生");
        Student s = studentMapper.findById(5);
        System.out.println("Student [id="+s.getId()+", name="+s.getName()+", age="+s.getAge()+"]");
    }
    @Test
    public void testFind(){
        logger.info("查找所有学生");
        List<Student> studentList = studentMapper.find();
        for (Student s:studentList) {
            System.out.println("Student [id="+s.getId()+", name="+s.getName()+", age="+s.getAge()+"]");
        }
    }
}
相关标签: mybatis