栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MyBatis入门(select查)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis入门(select查)

前言:

打开MySQL,创建数据库创建一个学生表

导入如下jar包(mybatis所需jar包,连接MySQL所需jar包,单元测试类所需jar包)

1.创建pojo实体层Student学生类
package com.mybatis.pojo;

public class Student {
    private Integer id;
    private String username;
    private String password;
    private Integer role;

  

    @Override
    public String toString() {
        return
                "{id=" + id +
                ", username='" + username + ''' +
                ", password='" + password + ''' +
                ", role=" + role+ "}";
    }
}

2.创建dao数据层StudentMapper学生映射类

要拿什么数据做什么

package com.mybatis.dao;

import com.mybatis.pojo.Student;

public interface StudentMapper {
    public Student queryStudent(int role);
}

3.创建test包、resources包,在项目根目录下创建并且配置好路径(这样就方便了写文件的路径)

4.在resources文件夹下创建MybatisConfig.xml配置文件
  • 一定要有文件头
  • 连接MySQL数据库
  • 映射dao





 
     
         
             
             
                 
                
                
                
            
        
    

     
             
    

5.在resources文件夹下创建StudentMapper.xml文件(文件名要和dao层类名一致)





 
		
         
            SELECT `id`, `username`, `password`,`role` FROM `user` WHERe `role` = #{role}
        

6.测试,在test文件夹中创建StudentTest测试类
import com.mybatis.dao.StudentMapper;
import com.mybatis.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.InputStream;
import java.util.ArrayList;
import java.util.List;

public class StudentTest {
    @Test
    public void studentTest(){
        SqlSession sqlSession=getSqlSession();   //SQLSession是线程不安全的,要及时关闭
        try {
            // 1.这个写法不推荐, 因为没有使用映射接口, 只是用了映射的配置文件
            List student=sqlSession.selectList("com.mybatis.dao.StudentMapper.queryStudent",2);

            // 2.推荐这种写法, 也是使用mybatis 绝大部分写得最多的语句
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            Student student1 = mapper.queryStudent(2);

            System.out.println(student1);
        }finally {
            sqlSession.close();
        }
    }
    public SqlSession getSqlSession(){
        //拿到SqlSessionFactory 工厂对象
        //1. 拿到全局配置文件的路径跟文件名
        String mybatisConfigFile = "MybatisConfig.xml";

        try {
            //2. 把1. 用Resources getResourceAsStream 方法取得一个输入流的对象
            InputStream inputStream = Resources.getResourceAsStream(mybatisConfigFile);

            //3. 把2. 用来实例化SqlSessionFactory 对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            return sqlSessionFactory.openSession();

        }catch (IOException e){
            e.printStackTrace();
        }

        return null;

    }
}

import com.mybatis.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.InputStream;
import java.util.List;

public class StudentTest {
    @Test
    public void studentTest(){
        SqlSession sqlSession=getSqlSession();
        //由于根据role查询返回的记录数可能超过1,这里我们采用list集合装进来
        List student=sqlSession.selectList("com.mybatis.dao.StudentMapper.queryStudent",2);//参1:dao层映射类的方法全路径;参2:传入的参数值
        System.out.println(student);
    }

	//封装获取SqlSeesion对象方法
    public SqlSession getSqlSession(){
        //拿到SqlSessionFactory 工厂对象
        //1. 拿到全局配置文件的路径跟文件名
        String mybatisConfigFile = "MybatisConfig.xml";

        try {
            //2. 用Resources getResourceAsStream 方法取得一个输入流的对象
            InputStream inputStream = Resources.getResourceAsStream(mybatisConfigFile);

            //3. 用来实例化SqlSessionFactory 对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            return sqlSessionFactory.openSession();
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }
}
结果:

另外,我们可以配置好日志的文件,在resources文件夹下创建log4j.xml文件





    
        
        
            
        
    
    
        
    
    
        
    
    
        
        
    

就可以得到结果图里面的日志了

全局配置文件补充





 

    
    

    
    
        
    

    
    


                

    

    


    
    
    

     
         
             
             
                 
                
                
                
            
        

    

     




    
    


dbconfigproperties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/yeujuan?useSSL=false
username=root
password=root
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/846011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号