Mybatise的学习笔记

搭建环境

1.下载IDEA

2.下载Maven

3.如果没有下载JDK,还需要下载JDK

学习文档

mybatise中文学习文档链接:https://mybatis.org/mybatis-3/zh/getting-started.html

创建项目

创建

打开IDEA,创建一个Maven项目,这里记得导入自己下载的maven

 

配置

在pop.xml中添加依赖,我数据库使用的是postgresql,如果你使用的是mysql,那么导入mysql即可。

        <!-- postgresql驱动-->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.3.6</version>
        </dependency>

        <dependency>
            <!-- mybatis驱动-->
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
<groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>

 构建 SqlSessionFactory

新建utils文件夹,在文件夹中构建 SqlSessionFactory

 

 

 

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 {
            //获取mybatis第一步
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
              sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }catch (IOException e){
            e.printStackTrace();
        }
   }
    public static SqlSession getSqlSeeion(){
        SqlSession  sqlSession= sqlSessionFactory.openSession();
        return sqlSession;
    }
}

配置mybatis-config.xml文件

 

 

 

<?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="dp.properties"/>
<!--    <typeAliases>-->
<!--        <typeAlias type="com.pojo.Student" alias="Student"/>-->
<!--    </typeAliases>-->
    <typeAliases>
        <package name="com.pojo"/>
    </typeAliases>
        <environment id="test">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="org.postgresql.Driver"/>
                <property name="url" value="jdbc:postgresql://localhost:5432/postgres?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>//自己数据库的名字
                <property name="password" value="root"/>//自己数据库的密码
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/dao/StudentMapper.xml"/>
    </mappers>
</configuration>

实体类

 

 

 

package com.pojo;

import org.apache.ibatis.type.Alias;

import java.util.Date;

@Alias("stu")
public class Student {
    private int id;
    private Date creat_time;
    private String name;
    private int score;

    public Student() {
    }

    public Student(int id, Date creat_time, String name, int score) {
        this.id = id;
        this.creat_time = creat_time;
        this.name = name;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getCreat_time() {
        return creat_time;
    }

    public void setCreat_time(Date creat_time) {
        this.creat_time = creat_time;
    }

    public String getName() {
        return name;
    }

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

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
    public String toString()
    {
        return "Student{"+"id="+id+",name="+name+",create_tie="+creat_time+",score="+score+"}";
    }
}

数据层

实体层接口

 

 

 

import com.pojo.Student;

import java.util.List;

public interface StudentMapper {
  Student getStudentById(int id);
}

实体层链接数据

 

 

<?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.dao.StudentMapper">

    <select id="getStudentById" parameterType="int" resultType="com.pojo.Student">
        select  id,creat_time,name as named,score from study_student where id=#{id};
    </select>

</mapper>

在mybatise-config.xml中添加mapper配置

  <mappers>
        <mapper resource="com/dao/StudentMapper.xml"/>
  </mappers>

测试类

 

 

import com.dao.StudentMapper;
import com.pojo.Student;
import com.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class StudentTest {
    @Test
    public  void getStudentById(){
        //第一步,获取sqlSessiond对象
        SqlSession sqlsession= MybatisUtils.getSqlSeeion();
        StudentMapper studentdao= sqlsession.getMapper(StudentMapper.class);
        Student student=studentdao.getStudentById(2);
        System.out.println(student);
        sqlsession.close();
    }

}

至此一个完整的mybatise项目就完成了。

 

posted @ 2022-09-15 10:38  萍2樱释  阅读(22)  评论(0)    收藏  举报