MyBatis中的全局配置文件

目录:

  • 如何xml,sql语句传值

  • 返回集合List,resultTYPE为集合中元素类全名

  • 返回单个对象resultTYPE vo类全名

首先先做一些准备工作:

1.配置mybatis.xml文件




    
        
            
            
                
                
                
                
            
        
    
    
        
    

2.导入jar包

3.把工程建成类似这样

MyBatis中的全局配置文件_第1张图片

4.创建 Student类

package com.jd.student.vo;

public class Student {

	private String id;
	private String name;
	private String mobile;
	private String address;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
}

1.通过一个参数,确定一个对象:

  IStudentDao中:

public interface IStudentDao {
        //getName方法,通过id传参
	String getName(String id);
	
}

student.xml中:






    

Test中:

public class Test {

	public static void main(String[] args) throws Exception {
		InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
                //传入数据库中对象的id
		System.out.println(studentDao.getName("328cc546-03d4-4e90-8b22-1ed66ffac217"));
		sqlSession.close();
	}
}

2.通过两个参数,确定一个对象

  IStudentDao中:

public interface IStudentDao {

        //传多个参数时要加上@Param
	String getName(@Param("id")String id,@Param("mobile")String mobile);
	
}

 student.xml中:






    

Test中: 

public class Test {

	public static void main(String[] args) throws Exception {
		InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
                //传入id,mobile
		System.out.println(studentDao.getName("328cc546-03d4-4e90-8b22-1ed66ffac217","123"));
		sqlSession.close();
	}
}

3.如果数据库和class类不照应:

  IStudentDao中:

public interface IStudentDao {

	List select(String name);
}

  student.xml中:





	
    
            
		
		
		
	

    


Test中:

public class Test {

	public static void main(String[] args) throws Exception {
		InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
		System.out.println(studentDao.getClass().getName());
		for(Student student:studentDao.select("王%")) {
			System.out.println(student.getName());
		}
		sqlSession.close();
	}
}

 

 

 

 

 

你可能感兴趣的:(MyBatis中的全局配置文件)