MyBatis(增删改)

dao层映射接口
package com.mybatis.dao;

import com.mybatis.pojo.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public interface StudentMapper {
    //查询
    public List<Student> queryStudent(Integer id);
    //增加
    public void insertStudent(ArrayList<Student> arrayList);
    //更新
    public void updateStudentById(Map<String,Object> map);
    //删除
    public void deleteStudentById(Integer id);
}

Mybatis映射文件
<?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">

<!-- Student SQL映射文件,文件名必须和dao中的映射接口类名相同 -->

<mapper namespace="com.mybatis.dao.StudentMapper"> <!-- namespace = 名称空间 -->
        <select id="queryStudent" resultType="student"> <!-- resultType = 结果的类型 ,类型可有别名 -->
            SELECT `id`, `username`, `password`,`role` FROM `user` WHERE `id` = #{id}
        </select>

        <insert id="insertStudent"  parameterType="java.util.ArrayList">
            <!--插入1条记录-->
	        <!-- insert into `user` values(#{id},#{username},#{password},#{role}) -->
            <!--插入多条记录-->
            insert into `user` values
            <foreach collection="list" item="item" separator=",">
                (#{item.id},#{item.username},#{item.password},#{item.role})
            </foreach>

        </insert>

        <update id="updateStudentById">
            update `user` set username=#{newUsername} where id=#{id}
        </update>

        <delete id="deleteStudentById">
            delete from `user` where id=#{id}
        </delete>

</mapper>
测试类文件
import com.mybatis.dao.StudentMapper;
import com.mybatis.pojo.Student;
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.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentTest {
    @Test
    public void studentTest(){
        SqlSession sqlSession=getSqlSession();
        try {
            // 1.这个写法不推荐, 因为没有使用映射接口, 只是用了映射的配置文件
            List<Student> student=sqlSession.selectList("com.mybatis.dao.StudentMapper.queryStudent",2);

            // 2.推荐这种写法, 也是使用mybatis 绝大部分写得最多的语句
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //查询
            List<Student> student1 = mapper.queryStudent(2);
            System.out.println(student1);
            //增加
            //增加多条记录
            ArrayList<Student> arrayList =new ArrayList<>();
            arrayList.add(new Student(null,"小梅","555",0));
            arrayList.add(new Student(null,"钉钉","587",0));
            arrayList.add(new Student(null,"阿臭","123",0));
            mapper.insertStudent(arrayList);
            sqlSession.commit();
            //增加一条记录
//            mapper.insertStudent(new Student(....));
//            sqlSession.commit();
            //更新
            //可用参数 are [0, 1, param1, param2]
            //因为myBatis 他会封装传入的参数为一个 Hashmap
            Map<String,Object> map =new HashMap<>();
            map.put("id",5);
            map.put("newUsername","小钟");
            mapper.updateStudentById(map);
            sqlSession.commit();
             //删除
            mapper.deleteStudentById(6);
            sqlSession.commit();

        }finally {
            sqlSession.close();
        }
    }
    public SqlSession getSqlSession(){
        //拿到SqlSessionFactory 工厂对象
        //1. 拿到全局配置文件的路径跟文件名
        String mybatisConfigFile = "MybatisConfig.xml";

        try {
            //2. 把1. 用Resources getResourceAsStream 方法取得一个输入流的对象
            InputStream inputStream = Resources.getResourceAsStream(mybatisConfigFile);

            //3. 把2. 用来实例化SqlSessionFactory 对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            return sqlSessionFactory.openSession();

        }catch (IOException e){
            e.printStackTrace();
        }

        return null;

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值