【Java企业级开发技术】【MyBatis入门程序】上机实验

112 阅读2分钟

Java企业级开发技术-MyBatis入门程序-上机实验

1 实验目的

  1. 掌握Mybatis环境配置
  2. 熟悉Mybatis基本开发流程
  3. 能够开发简单的入门程序

2 实验内容

创建1个学生(student)表,包括学号(num),姓名(name),年龄(age),通过Mybatis框架实现如下数据库访问功能:

  1. 根据num查询某学生信息并输出;
  2. 找出所有年龄大于指定参数的学生记录并输出。

3 实验分析、代码及运行结果

  1. Student.java
package cn.lzjtu.pojo;

public class Student {
    private int num;             //学生学号
    private String name;       //学生姓名
    private int age;            //学生年龄

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

  1. StudentMapper.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="cn.lzjtu.pojo.Student">

    <select id="findByNum" parameterType="int"
            resultType="cn.lzjtu.pojo.Student">
        select * from student where num = #{num}
    </select>

    <select id="findAll" resultType="cn.lzjtu.pojo.Student">
        select * from student
    </select>

    <select id="findByAge" parameterType="int"
            resultType="cn.lzjtu.pojo.Student">
        select * from student where age > #{age}
    </select>
</mapper>
  1. StudentTest.java
package Test;

import cn.lzjtu.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.Reader;
import java.util.List;

public class StudentTest {
    @Test
    public void studentFindByNumTest()
    {
        //读取文件名
        String resources = "mybatis-config.xml";
        //创建流
        Reader reader = null;

        try {
            //读取mybatis-config.xml文件内容到reader对象中
            reader = Resources.getResourceAsReader(resources);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //初始化mybatis数据库,创建SqlSessionFactory类的实例
        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
        //创建SqlSession实例
        SqlSession session = sqlMapper.openSession();
        //传入参数查询,返回结果
        Student student = session.selectOne("findByNum", 3);
        //输出结果
        System.out.println("学号为3的学生信息如下:");
        System.out.println("姓名:"+student.getName()+",年龄:"+student.getAge());
        System.out.println();
        //关闭session
        session.close();
    }
    @Test
    public void studentFindByAgeTest()
    {
        //读取文件名
        String resources = "mybatis-config.xml";
        //创建流
        Reader reader = null;

        try {
            //读取mybatis-config.xml文件内容到reader对象中
            reader = Resources.getResourceAsReader(resources);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //初始化mybatis数据库,创建SqlSessionFactory类的实例
        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
        //创建SqlSession实例
        SqlSession session = sqlMapper.openSession();
        //传入参数查询,返回结果
        List<Student> students = session.selectList("findByAge", 20);
        //输出结果
        System.out.println("年龄大于20岁的学生有:");
        for (Student student : students){
            System.out.println(student.getName());
        }
        //关闭session
        session.close();
    }
}

测试结果

总结:

Student类,位于包cn.lzjtu.pojo,包含三个属性:num(学生学号)、name(学生姓名)和age(学生年龄),并提供了相应的getter和setter方法。

映射代码定义了一个MyBatis映射文件,位于命名空间cn.lzjtu.pojo.Student,包含了三个SQL查询映射:findByNum用于根据学号查询单个学生信息,findAll用于查询所有学生信息,findByAge用于根据年龄查询符合条件的学生信息。

测试代码位于Test包下,通过MyBatis框架从mybatis-config.xml配置文件中读取数据库信息,并提供了两个测试方法:studentFindByNumTest用于根据学号查询学生信息,studentFindByAgeTest用于根据年龄查询学生信息列表,查询结果会被打印出来。

本文作者:鸿·蒙

文档工具:Typora

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 鸿·蒙 !