iBATIS入门之二:实现增删改查、模糊查询、序列增长
SQL脚本:
package com.rt.ibatisdemo.dao;import java.io.IOException;import java.io.Reader;import java.sql.SQLException;import java.util.List;import com.ibatis.sqlmap.client.SqlMapClient;import com.rt.ibatisdemo.vo.Student;public class StudentDAOImpl implements IStudentDAO{private static SqlMapClient smc = null;//SqlMapClient带有很多增删改查的方法static//静态初始化一次就够了{try {Reader reader = com.ibatis.common.resources.Resources.getResourceAsReader("SqlMapConfig.xml");//借助Reader读入xml配置,注意位置smc = com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);reader.close();//不再需要Reader了,关之} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void addStudent(Student stu) {try {smc.insert("Stu_namespace.insertStudent",stu);System.out.println("插入时返回的对象=》"+stu.getStudentid());//打印返回的值} catch (SQLException e) {e.printStackTrace();}}@Overridepublic void addStudentBySequence(Student stu) {try {smc.insert("Stu_namespace.insertStudentBySequence",stu);System.out.println("插入时返回的对象=》"+stu.getStudentid());//打印返回的值} catch (SQLException e) {e.printStackTrace();}}@Overridepublic void delStudentById(int id) {int deletedCount = 0;try {deletedCount = smc.delete("Stu_namespace.deleteStudentById",id);System.out.println("deleteCount=>"+deletedCount);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void updateStudent(Student stu) {int updatedCount = 0;try {updatedCount = smc.update("Stu_namespace.updateStudent", stu);System.out.println("updatedCount=>"+updatedCount);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic Student selectStudentById(int id) {Student stu = null;try {stu =(Student) smc.queryForObject("Stu_namespace.selectStudentById",id);} catch (SQLException e) {e.printStackTrace();}return stu;}@Overridepublic List<Student> selectStudentByName(String name) {List<Student> stus = null;try {stus =smc.queryForList("Stu_namespace.selectStudentByName",name);} catch (SQLException e) {e.printStackTrace();}return stus;}@Overridepublic List<Student> selectAll() {List<Student> stus = null;try {stus =smc.queryForList("Stu_namespace.selectAllStudent");} catch (SQLException e) {e.printStackTrace();}return stus;}}忘了一个,有大于小于号的时候xml认不了,所以要写成下边这样:
<![CDATA[SELECT *
FROM student