1 2 5 67 8 18 19 209 1710 11 1612 13 14 15 21 2422 23
package demo2;import entity.Employee;public interface EmployeeMapper {public Employee getById(int id);}
1 2 5 6 78 12 15
1 package demo2; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;10 import entity.Employee;11 12 public class Demo2 {13 14 public static void main(String[] args) throws IOException {15 // 1、获取sessionFactory16 String resource = "mybatis-config.xml";17 InputStream inputStream = Resources.getResourceAsStream(resource);18 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()19 .build(inputStream);20 // 2、获取 session21 SqlSession session = sqlSessionFactory.openSession();22 // 3、获取接口的是实现类对象 自动代理23 EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);24 25 Employee employee = mapper.getById(1);26 System.out.println(employee.toString());27 session.close();28 }29 }