mybatis的基础小案例

1. 准备好一张名为student的表

1.首先准备一张表(student),表结构如下

表名student在这里插入图片描述

2. 建立maven工程,导入jar包

在工程的pom.xml下添加依赖

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
        <!-- 日志 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <!-- 测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

3. 根据数据库中的表,建立相关的实体类

建立实体类Student
在这里插入图片描述

package com.hzit.pojo;

public class Student {
    private String s_id;
    private String s_name;
    private String s_birth;
    private String s_sex;

    public Student() {
    }

    public Student(String s_id, String s_name, String s_birth, String s_sex) {
        this.s_id = s_id;
        this.s_name = s_name;
        this.s_birth = s_birth;
        this.s_sex = s_sex;
    }

    public String getS_id() {
        return s_id;
    }

    public void setS_id(String s_id) {
        this.s_id = s_id;
    }

    public String getS_name() {
        return s_name;
    }

    public void setS_name(String s_name) {
        this.s_name = s_name;
    }

    public String getS_birth() {
        return s_birth;
    }

    public void setS_birth(String s_birth) {
        this.s_birth = s_birth;
    }

    public String getS_sex() {
        return s_sex;
    }

    public void setS_sex(String s_sex) {
        this.s_sex = s_sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "s_id='" + s_id + '\'' +
                ", s_name='" + s_name + '\'' +
                ", s_birth='" + s_birth + '\'' +
                ", s_sex='" + s_sex + '\'' +
                '}';
    }
}

4. 在resources下面建立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>
    <!-- 配置 mybatis 的环境 -->
    <environments default="mysql">   <!-- 配置 mysql 的环境 -->
        <environment id="mysql">    <!-- 配置事务的类型 -->
            <transactionManager type="JDBC"></transactionManager>    <!-- 配置连接数据库的信息:用的是数据源(连接池) -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/jiujiang01"/>
                <property name="username" value="root"/>
                <property name="password" value="mima101010"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 告知 mybatis 映射配置的位置 -->
    <mappers>
        <mapper resource="com/hzit/dao/StudentDao.xml"/>
    </mappers>
</configuration>

5. 建立dao层的接口,用来定义方法

这里定义一个方法 findAll()
在这里插入图片描述

package com.hzit.dao;

import com.hzit.pojo.Student;

import java.util.List;

public interface StudentDao {
    List<Student> findAll();
}

6. 建立xml映射文件,用来写SQL语句

在这里插入图片描述==
这里要注意StudentDao.xml的路径要和第五步的dao层接口路径保持一致,名字也要一样==

<?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.hzit.dao.StudentDao">
    <select id="findAll" resultType="com.hzit.pojo.Student">
        select * from student
    </select>

</mapper>

namespae应为dao层接口类的路径

最后建立一个Test类

package com.hzit;

import com.hzit.dao.StudentDao;
import com.hzit.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 java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        SqlSession session = factory.openSession();
        StudentDao studentDao = session.getMapper(StudentDao.class);
        List<Student> students = studentDao.findAll();
        for (Student student:students){
            System.out.println(student);
        }
        session.close();
        in.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值