本文共 6418 字,大约阅读时间需要 21 分钟。
1.创建Maven项目,项目名称ibatisdemo,目录结构如图所示
4.0.0 com.mycompany ibatisdemo 0.0.1-SNAPSHOT org.apache.ibatis ibatis-sqlmap 2.3.0 mysql mysql-connector-java 5.1.38
package com.mycompany.entity;public class Student { private int sid; private String name; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return "id="+sid + "name="+name; }}
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/testusername=rootpassword=
package com.mycompany.dao;import java.util.List;import com.mycompany.entity.Student;public interface StudentDao { /** * 查询全部学生信息 * * @return 返回学生列表 */ public ListselectAllStudent(); /** * 根据学生ID查询学生信息 * @param sid * @return 学生对象 */ public Student selectStudentById(int sid); /** * 根据名称模糊查询 * @param name * @return 学生列表 */ public List selectStudentByName(String name); /** * 添加学生信息 * @param student * @return 是否添加成功 */ public boolean addStudent(Student student); /** * 根据ID删除学生信息 * @param sid * @return 删除是否成功 */ public boolean deleteStudentById(int sid); /** * 修改学生信息 * @param student * @return */ public boolean updateStudent(Student student);}
14.StudentDao的实现类StudentDaoImpl的内容如下
package com.mycompany.dao.impl;import java.io.IOException;import java.io.Reader;import java.sql.SQLException;import java.util.List;import com.ibatis.common.resources.Resources;import com.ibatis.sqlmap.client.SqlMapClient;import com.ibatis.sqlmap.client.SqlMapClientBuilder;import com.mycompany.dao.StudentDao;import com.mycompany.entity.Student;public class StudentDaoImpl implements StudentDao { private static SqlMapClient sqlMapClient = null; static{ try { Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 查询所有学生信息列表 */ public ListselectAllStudent() { List students = null; try { students = sqlMapClient.queryForList("selectAllStudent"); } catch (SQLException e) { e.printStackTrace(); } return students; } /** * 查询学生对象信息 */ public Student selectStudentById(int sid) { Student student = null; try { student = (Student) sqlMapClient.queryForObject("selectStudentById",sid); } catch (SQLException e) { e.printStackTrace(); } return student; } /** * 根据名称查询学生列表 */ public List selectStudentByName(String name) { List students = null; try { students = sqlMapClient.queryForList("selectStudentByName",name); } catch (SQLException e) { e.printStackTrace(); } return students; } /** * 添加学生信息 */ public boolean addStudent(Student student) { Object object = null; boolean flag = false; try { object = sqlMapClient.insert("addStudent",student); } catch (SQLException e) { e.printStackTrace(); } if(object != null){ flag = true; } return flag; } /** * 根据ID删除学生信息 */ public boolean deleteStudentById(int sid) { Object object = null; boolean flag = false; try { object = sqlMapClient.delete("deleteStudentById",sid); } catch (SQLException e) { e.printStackTrace(); } if(object != null){ flag = true; } return flag; } /** * 修改学生信息 */ public boolean updateStudent(Student student) { Object object = null; boolean flag = false; try { object = sqlMapClient.update("updateStudent",student); } catch (SQLException e) { e.printStackTrace(); } if (object != null) { flag = true; } return flag; }}
15.在src/test/java下创建测试类StudentTest,包名(com.mycompany.dao.impl),如图所示
16.测试类StudentTest的内容如下
package com.mycompany.dao.impl;import java.util.List;import com.mycompany.entity.Student;public class StudentTest { /** * @param args */ public static void main(String[] args) { StudentDaoImpl studentDaoImpl = new StudentDaoImpl(); //查询所有学生信息列表 Liststudents = studentDaoImpl.selectAllStudent(); for (Student student : students) { System.out.println(student); } //根据学生ID查询学生对象信息 Student student = studentDaoImpl.selectStudentById(2); System.out.println(student); //根据学生名称查询学生列表 List students2 = studentDaoImpl.selectStudentByName("xiao"); for (Student student2 : students2) { System.out.println(student2); } //添加学生信息 Student student3 = new Student(); student3.setName("demo"); boolean flag = studentDaoImpl.addStudent(student3); System.out.println(flag); //根据ID删除学生信息 boolean flag2 = studentDaoImpl.deleteStudentById(3); System.out.println(flag2); //修改学生信息 Student student4 = new Student(); student4.setSid(4); student4.setName("xiao4"); boolean flag3 = studentDaoImpl.updateStudent(student4); System.out.println(flag3); }}
17.表结构如下
CREATE TABLE `student` ( `sid` INT(10) NOT NULL AUTO_INCREMENT, `name` VARCHAR(50) DEFAULT NULL, PRIMARY KEY (`sid`)) ENGINE=INNODB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
本文转自 素颜猪 51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1916259
转载地址:http://qplul.baihongyu.com/