轻松学会Java框架之MyBatis(第三章)3-4 有参数的数据库添加数据

本文详细介绍了在Java框架MyBatis中如何使用有参数的方法向MySQL数据库的student表添加数据。通过创建MyBatisStudent实体类和MyBatisStudentDao,调用addStudentByEntity方法并传入学生对象。讲解了parameterType属性,对比了#和$的区别,解释了#{}预编译处理避免SQL注入,而${}进行字符串替换可能导致的安全问题。
摘要由CSDN通过智能技术生成

在这里插入图片描述
本节对student表进行数据添加 由于学生的数据比较多

所以我们通过student表来学习如何用有参方法往数据库添加数据

老样子,按照之前的步骤创建student的实体类

MyBatisStudent

package com.teikin.mybatis.dao.entity;

public class MyBatisStudent {
    private Long studentId;
    private String studentName;
    private Integer studentSex;
    private String birthday;
    private Double height;
    private Double weight;
    private Long classId;

    public Long getStudentId() {
        return studentId;
    }

    public void setStudentId(Long studentId) {
        this.studentId = studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Integer getStudentSex() {
        return studentSex;
    }

    public void setStudentSex(Integer studentSex) {
        this.studentSex = studentSex;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public Double getHeight() {
        return height;
    }

    public void setHeight(Double height) {
        this.height = height;
    }

    public Double getWeight() {
        return weight;
    }

    public void setWeight(Double weight) {
        this.weight = weight;
    }

    public Long getClassId() {
        return classId;
    }

    public void setClassId(Long classId) {
        this.classId = classId;
    }

    @Override
    public String toString() {
        return "MyBatisStudent{" +
                "studentId=" + studentId +
                ", studentName='" + studentName + '\'' +
                ", studentSex=" + studentSex +
                ", birthday='" + birthday + '\'' +
                ", height=" + height +
                ", weight=" + weight +
                ", classId=" + classId +
                '}';
    }
}


MyBatisStudentDao
在调用addStudentByEntity方法时,要求传一个MyBatisStudent对象(学生对象)。
注意了,之前我们使用sqlSession.insert只是传递一个字符串,其实可以再传一个参数。
这个参数可以是对象,也可以是集合、数组等。
这里我们传一个对象进去,这个对象就是学生对象,将包含学生的所有信息。

package com.teikin.mybatis.dao;

import com.teikin.mybatis.dao.entity.MyBatisStudent;
import com.teikin.mybatis.util.MyBatisSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

public class MyBatisStudentDao {
    private SqlSession sqlSession=null;

    public int addStudentByEntity(MyBatisStudent myBatisStudentParam){
        sqlSession= MyBatisSqlSessionFactory.getSqlSession();
        int rowCount=sqlSession.insert("com.teikin.mybatis.dao.mapper.MyBatisStudentDao.addStudentByEntity",myBatisStudentParam);
        sqlSession.commit();
        sqlSession.close();

        return rowCount;
    }
}


MyBatisStudentDao.xml
parameterType:参数类型
这里我们需要传进去一个MyBatisStudent对象(学生对象)

<mapper namespace="com.teikin.mybatis.dao.mapper.MyBatisStudentDao">
    <insert id="addStudentByEntity" parameterType="com.teikin.mybatis.dao.entity.MyBatisStudent">
        insert into student
            set student_name=#{studentName},
                student_sex=#{studentSex},
                birthday='${birthday}',
                height=#{height},
                weight=#{weight},
                class_id=#{classId}
    </insert>
</mapper>

这里先这样写,后面会介绍#和$的作用。

Test类

package com.teikin.mybatis.dao;
import com.teikin.mybatis.dao.entity.MyBatisStudent;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;


public class MyBatisStudentDaoTest {
    private MyBatisStudentDao myBatisStudentDao = null;
    @Test
    public void testAddStudentByEntity() {
        MyBatisStudent myBatisStudent = new MyBatisStudent();
        myBatisStudent.setStudentName("陈同学");
        myBatisStudent.setStudentSex(2);
        myBatisStudent.setBirthday("1990-06-25");
        myBatisStudent.setHeight(162D);
        myBatisStudent.setWeight(115D);
        myBatisStudent.setClassId(1L);
        myBatisStudentDao = new MyBatisStudentDao();
        //通过有参方法添加数据
        int rowCount = myBatisStudentDao.addStudentByEntity(myBatisStudent);
        System.out.println("addStudentByEntity影响的行数:" + rowCount);
    }

成功运行结果:
在这里插入图片描述
此时返回数据库查看student表将得到一条新的关于陈同学的数据。

我们来看一下结果中的这条sql语句
在这里插入图片描述
如图所示,要插入的数据除了birthday外都显示为一个问号,在sql语句中“?”表示一个占位符。
在这里插入图片描述
回到MyBatisStudentDao.xml的insert中查看,只有birthday使用了$符号。
由此可以看出,使用了#{ }方式在sql语句中会以"?"表示,使用了${ }方式在sql语句中则会直接将要插入的数据显示出来。


查询条件有两种方式表达
使用“${}”来传递参数
使用“#{}”来传递参数

“#{}”是预编译处理
当MyBatis在处理“#{}”时,会将sql中的“#{}”替换为“?”,然后调用PreparedStatement的set方法来赋值,传入字符串后则会在字符串的值两边加上“‘”单引号。

“${}”字符串替换

当MyBatis在处理“${}”时,会将sql中的“${}”替换为变量的值,所传入的数据不会在两边加上单引号。
注:使用“${}”会导致sql注入,不利于系统的安全性。

SQL注入是什么?
通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。
常见的有匿名登陆(在登陆框输入恶意的字符串)、借助异常获取数据库信息等。




这就是MyBatis中用过有参方法的方式往数据库里添加数据啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值