MyBatis 5 两种取值符号以及ParameterType为简单、对象、嵌套对象类型

两种取值符号以及ParameterType为简单、对象、嵌套对象类型

项目结构

MyBatis 5 两种取值符号以及ParameterType为简单、对象、嵌套对象类型_第1张图片

1.

CREATE TABLE empinfo (
eid INT (10) PRIMARY KEY,
name VARCHAR (20),
age INT(10),
sex VARCHAR(5)
);

 

2.




	
	
	
	
		
		
		
		
	

	
	 
		 
			 
			 
			 	
				 
				 
				 
				 
			 
	 	
	 
	 
	 	
	 	
	 

3.

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/company
username=root
password=root

4.

package org.lanqiao.entity;
 
public class Empinfo {
	private int eid;
	private String name;
	private int age;
	private String sex;
	private String phone;
	
	//无参构造
	public Empinfo () {
	}
	//有参构造
	public Empinfo (int eid, String name, int age, String sex, String phone) {
		super();
		this.eid = eid;
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.phone = phone;
	}
	
	/**
	 * @return the eid
	 */
	public int getEid() {
		return eid;
	}
	/**
	 * @param eid the eid to set
	 */
	public void setEid(int eid) {
		this.eid = eid;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
	/**
	 * @return the sex
	 */
	public String getSex() {
		return sex;
	}
	/**
	 * @param sex the sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}
	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}
	/**
	 * @param phone the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return eid+"-"+name+"-"+age+"-"+sex+"-"+phone ;
	}
 
}

5.




	
 		
 	
 		
 		
 		
 		
 		
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	

6.

package org.lanqiao.mapper;


import java.util.List;

import org.lanqiao.entity.Empinfo;

public interface EmpinfoMapper {
	/*
	 * 1.方法名和mapper.xml文件中标签的id值相同
	 * 2.方法的输入参数和mapper.xml文件中标签的parameterType类型一致
	 * 3.方法的返回值 和mapper.xml文件中标签的resultType类型一致
	 * 
	 */
	//(1).按eid查询Empinfo
	Empinfo queryEmpinfoByeid(int eid);
	//(2).按name查询Empinfo
	Empinfo queryEmpinfoByname(String name);
	//(4).查询所有Empinfo并排序
	List queryAllEmpinfosOrderByColumn(String column);
	//().按年龄或名字查询Empinfo进行模糊查询
	List queryEmpinfoByageOrname(Empinfo empinfo);
}

7.

package org.lanqiao.test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

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.lanqiao.entity.Empinfo;
import org.lanqiao.mapper.EmpinfoMapper;

public class TestEmpinfo {
	//(1).按eid查询Empinfo
	public static void queryEmpinfoByeid() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		
		EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
		Empinfo empinfo=empinfoMapper.queryEmpinfoByeid(2017051133);
		System.out.println(empinfo);
		session.close();
	}
	//(2).按name查询Empinfo
	public static void queryEmpinfoByname() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		
		EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
		Empinfo empinfo=empinfoMapper.queryEmpinfoByname("lr");
		System.out.println(empinfo);
		session.close();
	}
	//(4).查询所有Empinfo并排序
	public static void queryAllEmpinfosOrderByColumn() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		//String statement = "org.lanqiao.entity.empinfoMapper."+"queryAllEmpinfos";		
		//List empinfos = session.selectList(statement);
		EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
		List empinfos=empinfoMapper.queryAllEmpinfosOrderByColumn("eid");
		System.out.println(empinfos);
		session.close();
	}
	//().按年龄或名字查询Empinfo进行模糊查询,两种方法
	public static void queryEmpinfoByageOrname() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		//String statement = "org.lanqiao.entity.empinfoMapper."+"queryAllEmpinfos";		
		//List empinfos = session.selectList(statement);
		EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
		
		Empinfo empinfo=new Empinfo();
		empinfo.setAge(22);
//		empinfo.setName("%f%");
		empinfo.setName("f");
		List empinfos=empinfoMapper.queryEmpinfoByageOrname(empinfo);
		System.out.println(empinfos);
		session.close();
	}
	public static void main(String[] args) throws IOException {
		//queryEmpinfoByeid();//按eid查询单个Empinfo
		//queryEmpinfoByname();
		//queryAllEmpinfosOrderByColumn();
		queryEmpinfoByageOrname();
	}
}

 

你可能感兴趣的:(MyBatis)