ibatis学习笔记-查询所有对象

来源:互联网 发布:做美食经验知乎 编辑:程序博客网 时间:2024/06/07 11:13

辅助类有一个要求,保证有一个无参的构造方法(无论是ibatis还是hi****,不然可能会出问题)。

CRUD

借助SqlMapClient中的方法:

queryForObject

queryForList

insert

delete

update


Student.xml的配置

<sqlMap> <!-- 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 --> <typeAlias alias="Student" type="com.nitlib.check.entity.Student" /> <!-- 这样以后改了sql,就不需要去改java代码了 --> <!-- id表示select里的sql语句,resultClass表示返回结果的类型 --> <select id="selectAllStudent" resultClass="Student"> select * from Student</select> <select id = "selectStudentById" parameterClass="int" resultClass="Student">select * form Student where studentid=#studentid# </select></sqlMap> 



读取配置

private static SqlMapClient sqlMapClient = null;// 读取配置文件 static { try { Reader reader = Resources .getResourceAsReader("com/nitlib/check/SqlMapConfig.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } 



基本的CRUD操作

查询

public List<Student> queryAllStudent() {// TODO Auto-generated method stubList<Student> studentList = null;try {studentList = sqlMapClient.queryForList("selectAllStudent");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return studentList;}


测试

public static void main(String args[]){IStudentDAO dao = new IStudentDAOImpl();for(Student student:dao.queryAllStudent()){System.out.println(student);}}


模糊查询
自动主键selectKey
优点


思考使用ibatis的好处

0 0
原创粉丝点击