mybatis

目录

🌸创建一个新的Maven项目

🌸引入依赖

🍂查看mysql版本

🍂pom.xml文件

🌸数据源(properties)文件

🌸mybatis的配置文件(基本cv即可)

🍂声明为config

🍂配置

🌸数据库学生表

🌸java目录创建

🌸映射文件配置

🌸完整的config配置文件

🌸测试

🍂查询所有学生信息

🍀接口

🍀mapper.xml映射文件

🍀测试类

🍀结果

🍂添加一条学生信息

🍀接口

🍀mapper.xml映射文件

🍀测试类

🍀结果

🍂修改&删除学生信息

🍀接口

🍀mapper.xml映射文件

🍀测试类的主要操作方法

🍀结果


maven项目中使用mybatis对数据库中student表进行增删改查案例

创建一个新的Maven项目

引入依赖

使用mybatis需要引入两个依赖,MySQL的依赖和mybatis的依赖(当然,如果有人需要使用junit单元测试,也可以引入对应的依赖)

注意:MySQL的依赖版本需要和本机使用的MySQL的版本一致(大版本一致即可)

查看mysql版本

pom.xml文件

 <dependencies>
    <!-- MyBatis 依赖 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.11</version>
    </dependency>
    <!-- 数据库驱动依赖 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.27</version>
    </dependency>
  </dependencies>

数据源(properties)文件

创建一个properties文件

mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/world?serverTimezone=GMT
mysql.username=root
mysql.password=root

mybatis的配置文件(基本cv即可)

声明为config

配置

数据库学生表

java目录创建

其中entity包中的Student实体类中的属性对应我们数据库中student表中的字段

也是为了后面存放我们查询返回的学生信息。

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="com.soft.mapper.StudentMapper">
<!--    数据库操作-->

</mapper>

同时在配置文件中将mapper.xml引入

完整的config配置文件

<?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>
    <!--    数据源-->
    <properties resource="jdbc.properties"></properties>
    <!--    环境配置-->
    <environments default="batis">
        <environment id="batis">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${mysql.driver}"/>
                <property name="url" value="${mysql.url}"/>
                <property name="username" value="${mysql.username}"/>
                <property name="password" value="${mysql.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!--    mapper-->
    <mappers>
<!--       与接口目录保持一致-->
        <mapper resource="com/soft/mapper/mapper.xml"></mapper>
    </mappers>
</configuration>

测试

查询所有学生信息

接口

mapper.xml映射文件

测试类

public class TestClass {
    public static void main(String[] args) throws IOException {
        //根据文件路径创建流对象
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis_conf.xml");
        //根据流对象创建session工厂
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //开启session会话
        SqlSession sqlSession = build.openSession();
        //获取mapper实例
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        //操作数据库
        //查询所有学生信息
        selectTest(studentMapper);
    }
    public static void selectTest(StudentMapper studentMapper){
        List<Student> students = studentMapper.selectAll();
        //遍历打印
        for (Student s:students) {
            System.out.println(s);
        }
    }
}

结果

添加一条学生信息

接口

mapper.xml映射文件

#{value}可以动态读取传入类型中的参数进行添加

测试类

 public static void main(String[] args) throws IOException, ParseException {
        //根据文件路径创建流对象
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis_conf.xml");
        //根据流对象创建session工厂
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //开启session会话
        SqlSession sqlSession = build.openSession();
        //获取mapper实例
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        //操作数据库
        //查询所有学生信息
//        selectTest(studentMapper);
        //添加学生
        insertTest(sqlSession,studentMapper);
    }
    public static void insertTest( SqlSession sqlSession,StudentMapper studentMapper) throws ParseException {
        //Date--String--SimpleDateFormat
        Date date = new Date();
        String birth = "2002-01-17 12:00:00";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        date = simpleDateFormat.parse(birth);
        //创建学生对象
        Student student = new Student(1,"挥发的石灰","男",date,"1111");
        int i = studentMapper.insertStu(student);
        if(i>0){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
        //提交和关闭session
        sqlSession.commit();
        sqlSession.close();
    }

结果

修改&删除学生信息

接口

mapper.xml映射文件

测试类的主要操作方法

结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值