mybatis教程-简单的增删改查

1 mapper的配置文件

<?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="org.mybatis.example.mapper.StudentMapper">
    <select id="selectByName" resultType="org.mybatis.example.bean.Student">
        select * from student where sname = #{name,jdbcType=VARCHAR} limit 1
    </select>
    <insert id="insertStudent">
        insert into student(sname,sage,ssex) values(#{sname},#{sage},#{ssex})
    </insert>
    <delete id="deleteStudent">
        delete from student where sname = #{name, jdbcType=VARCHAR}
    </delete>
    <update id="updateStudent">
        update student set sname = #{sname},sage = #{sage}, ssex=#{ssex}
        where sid = #{sid}
    </update>
</mapper>

2 mapper对应的接口文件

package org.mybatis.example.mapper;

import org.mybatis.example.bean.Student;

public interface StudentMapper {
    Student selectByName(String name);
    void insertStudent(Student student);
    void deleteStudent(String name);
    void updateStudent(Student student);
}

3 单元测试

package org.mybatis.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mybatis.example.bean.Student;
import org.mybatis.example.mapper.StudentMapper;

import java.io.InputStream;

public class StudentTest {
    static SqlSessionFactory sqlSessionFactory;

    @BeforeClass
    public static void beforeClass() throws Exception {
        String config = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(config);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void selectStudentByName() {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = studentMapper.selectByName("joker");
        System.out.println(student);//Student{sid=1, sname='joker', sage=18, ssex='男'}
        sqlSession.close();
    }

    @Test
    public void insertStudent() {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setSname("tom");
        student.setSage(20);
        student.setSsex("男");
        studentMapper.insertStudent(student);
        System.out.println(studentMapper.selectByName("tom"));//Student{sid=2, sname='tom', sage=20, ssex='男'}
        sqlSession.close();
    }

    @Test
    public void updateStudent() {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = studentMapper.selectByName("tom");
        System.out.println(student);//Student{sid=2, sname='tom', sage=20, ssex='男'}
        student.setSage(30);
        studentMapper.updateStudent(student);
        System.out.println(student);//Student{sid=2, sname='tom', sage=30, ssex='男'}
        sqlSession.close();
    }

    @Test
    public void deleteStudent() {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        studentMapper.deleteStudent("tom");
        System.out.println(studentMapper.selectByName("tom"));//null
        sqlSession.close();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tian Joker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值