1.新建conf文件
2.map映射文件
3.dao接口类
package dao;
import modole.User;
public interface Userdao {
public User GetUserByID(int id) throws Exception;
}
4.dao接口实现
package dao;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import modole.User;
public class Userdaoimp implements Userdao{
private SqlSessionFactory sqlSessionFactory;
public Userdaoimp(SqlSessionFactory sqlSessionFactory){
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public User GetUserByID(int id) {
// TODO Auto-generated method stub
SqlSession session=sqlSessionFactory.openSession();
User user=session.selectOne("test.GetUserByID", id);
session.close();
return user;
}
}
4.测试
package test;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import dao.Userdao;
import dao.Userdaoimp;
import modole.User;
public class selectid {
private static SqlSessionFactory sqlSessionFactory;
private static String path ="conf.xml";
public static void main(String[] args) throws Exception {
InputStream is =Resources.getResourceAsStream(path);
sqlSessionFactory= new SqlSessionFactoryBuilder().build(is);
Userdao userdao = new Userdaoimp(sqlSessionFactory);
User user=userdao.GetUserByID(1);
System.out.println(user.getName());
}
}
5测试结果