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

在IDEA中用Mybatis插入数据

程序员文章站 2022-07-10 21:13:35
在IDEA中用Mybatis插入数据1.首先,我们在Maven中引入相关的Mybatis的jar包: mysql

在IDEA中用Mybatis插入数据

1.首先,我们在Maven中引入相关的Mybatis的jar包:

<dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

我这里用的是mysql8.0的,用5.7的小伙伴们导入对应的版本就行
2.导入的jar后我们就需要写一个用来制造SqlSession对象的工厂:

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 java.io.IOException;
import java.io.InputStream;

public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            String resource="mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();

    }
}

在这里我们用到了mybatis的核心配置文件
3.编写mybatis的核心配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/school?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/pxy/dao/GradeMapper.xml"/>
    </mappers>
</configuration>

最下面的mapper是用来映射对应的需要执行的接口
4.我们对数据库中的Graed表进行实体类的封装:

public class Grade {
    private int gradeId;
    private String gradeName;

    public int getGradeId() {
        return gradeId;
    }

    public void setGradeId(int gradeId) {
        this.gradeId = gradeId;
    }

    public String getGradeName() {
        return gradeName;
    }

    public void setGradeName(String gradeName) {
        this.gradeName = gradeName;
    }

    public Grade() {
    }

    @Override
    public String toString() {
        return "Grade{" +
                "gradeId=" + gradeId +
                ", gradeName='" + gradeName + '\'' +
                '}';
    }

    public Grade(int gradeId, String gradeName) {
        this.gradeId = gradeId;
        this.gradeName = gradeName;
    }
}

5.在dao中写需要插入的接口方法:

import com.pxy.pojo.Grade;

import java.util.List;

public interface GradeMapper {
    List<Grade> getGradeList();


    int addGrade(Grade grade);
}

6.编写接口的xml配置;

<?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.pxy.dao.GradeMapper">
    <select id="getGradeList" resultType="com.pxy.pojo.Grade">
    select * from school.grade;
  </select>
    <insert id="addGrade" parameterType="com.pxy.pojo.Grade">
        insert into school.grade(gradeid,gradename) values (#{gradeId},#{gradeName});
    </insert>
</mapper>

这里的namespace会对我们上一步写到的接口进行一个映射
下面的id对应的就是接口中的方法名
result是接口的返回值
parameterType是参数类型
最后在<></>中写sql就好了
7.最后编写一个Test类测试一下:

 @Test
    public void addGrade(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GradeMapper mapper = sqlSession.getMapper(GradeMapper.class);
        mapper.addGrade(new Grade(55,"研一百"));
        sqlSession.commit();
        System.out.println("插入成功!");
        sqlSession.close();
    }

在IDEA中用Mybatis插入数据
在IDEA中用Mybatis插入数据
插入成功!

这是我第一次写博客,大佬们有什么不对的地方欢迎指出来,谢谢!

本文地址:https://blog.csdn.net/weixin_46005468/article/details/107351987