1.依赖jar包
mybatis-3.2.2.jar sqlserver2008.jar
2.代码
package ttyouni.model;public class Student { public int ID; public String UserName; public String Password; public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getUserName() { return UserName; } public void setUserName(String userName) { UserName = userName; } public String getPassword() { return Password; } public void setPassword(String password) { Password = password; }}
package ttyouni.main;import java.io.InputStream;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import ttyouni.model.Student;public class AppMain { public static void main(String[] args) { // TODO Auto-generated method stub //mybatis的配置文件 String resource = "Configuration.xml"; //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件) InputStream is = AppMain.class.getClassLoader().getResourceAsStream(resource); //构建sqlSession的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件) //Reader reader = Resources.getResourceAsReader(resource); //构建sqlSession的工厂 //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //创建能执行映射文件中sql的sqlSession SqlSession session = sessionFactory.openSession(); /** * 映射sql的标识字符串, * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL */ String statement = "ttyouni.model.StudentMapper.getStudent";//映射sql的标识字符串 //执行查询返回一个唯一user对象的sql Student student = session.selectOne(statement, 1); System.out.println(student.getUserName()); }}
3.文件结构