我的实例实现了6个基本功能:
1.向数据库student表插入一条数据;
2.删除student表的所有数据
3.删除student表指定ID的数据
4.更新student表指定ID的数据
5.查询student表的所有数据
6.查询student表的指定ID数据
使用的是oracle数据库
我的编程实例从创建SqlMapConfig.xml文件开始
第一步:编写SqlMapConfig.xml文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
- <sqlMapConfig>
- <properties resource="SqlMapConfig.properties" />
- <transactionManager type="JDBC">
- <dataSource type="SIMPLE">
- <property name="JDBC.Driver" value="${driver}" />
- <property name="JDBC.ConnectionURL" value="${url}" />
- <property name="JDBC.Username" value="${username}" />
- <property name="JDBC.Password" value="${password}" />
- </dataSource>
- </transactionManager>
- <sqlMap resource="sqlMap_student.xml" />
- </sqlMapConfig>
SqlMapConfig.xml文件包含另外2个
资源文件SqlMapConfig.properties(SqlMapConfig.xml文件中引用这里定义的4个值,一般编程会把各种数据库的连接方法都写于此,以便下次调用各种不同数据库之间的使用)
- ##### Oracle #####
- driver=oracle.jdbc.driver.OracleDriver
- url=jdbc:oracle:thin:@localhost:1521:name
- usernameusername=username
- passwordpassword=password
资源文件sqlMap_student.xml(具体的sql文执行语句)
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
- <sqlMap namespace="Test">
- <statement id="insert_student"
- parameterClass="zh2208.dto.StudentDto">
- insert into student(
- id,name,age,sex,address) values(
- #id#,#name#,#age#,#sex#,#address# )
- </statement>
- <statement id="delete_all_student">
- delete from student
- </statement>
- <statement id="deleteByID_student"
- parameterClass="zh2208.dto.StudentDto">
- delete from student where
- id = #id#
- </statement>
- <statement id="updataStudent_test"
- parameterClass="zh2208.dto.StudentDto">
- update student set
- name=#name#,sex=#sex#,age=#age#,address=#address#
- where id = #id#
- </statement>
- <statement id="select_all_student"
- resultClass="zh2208.dto.StudentDto">
- select * from student order by id
- </statement>
- <statement id="selectByID_student"
- parameterClass="zh2208.dto.StudentDto"
- resultClass="zh2208.dto.StudentDto">
- select * from student
- where id = #id#
- order by id
- </statement>
- </sqlMap>
以上步骤完成了所有的xml文件的编写。
第二步编写dto文件
我用到了student表,里面包含了id,name,sex,age,address 5个字段
2.1 dto文件的内容为
- package zh2208.dto;
- /**
- * @author zh2208
- *
- */
- public class StudentDto {
- //学生ID
- private int id = 0;
- //学生姓名
- private String name = "";
- //学生性别
- private String sex = "";
- //学生年龄
- private int age = 0;
- //学生地址
- private String address = "";
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- }
2.2 接口文件的内容为
- package zh2208.dao;
- import java.util.ArrayList;
- import com.ibatis.sqlmap.client.SqlMapClient;
- import zh2208.dto.*;
- /**
- * @author zh2208
- *
- */
- public interface StudentDao {
- //添加student表的数据
- public void addStudent(SqlMapClient sqlMap,StudentDto studentdto);
- //删除student表的数据
- public void delStudent(SqlMapClient sqlMap);
- //删除student表的指定ID数据
- public void delStudentByID(SqlMapClient sqlMap,StudentDto studentdto);
- //更新student表的数据
- public void updataStudent(SqlMapClient sqlMap,StudentDto studentdto);
- //查询student表的所有数据
- public ArrayList selectStudent(SqlMapClient sqlMap);
- //查询student表的指定ID数据
- public StudentDto selectStudentByID(SqlMapClient sqlMap,StudentDto studentdto);
- }
2.3 实现接口的内容为
- package zh2208.impl;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import com.ibatis.sqlmap.client.SqlMapClient;
- import zh2208.dao.StudentDao;
- import zh2208.dto.StudentDto;
- /**
- * @author zh2208
- *
- */
- public class StudentImpl implements StudentDao {
- //添加student表的数据
- public void addStudent(SqlMapClient sqlMap, StudentDto studentdto) {
- try {
- sqlMap.insert("insert_student", studentdto);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- //删除student表的数据
- public void delStudent(SqlMapClient sqlMap) {
- try {
- sqlMap.delete("delete_all_student", null);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- //删除student表的指定ID数据
- public void delStudentByID(SqlMapClient sqlMap, StudentDto studentdto) {
- try {
- sqlMap.delete("deleteByID_student",studentdto );
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- //更新student表的数据
- public void updataStudent(SqlMapClient sqlMap, StudentDto studentdto) {
- try {
- sqlMap.update("updataStudent_test",studentdto );
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- //查询student表的所有数据
- public ArrayList selectStudent(SqlMapClient sqlMap) {
- //保存查询结果
- ArrayList rsList = new ArrayList();
- try {
- rsList = (ArrayList)sqlMap.queryForList("select_all_student","");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return rsList;
- }
- //查询student表的指定ID数据
- public StudentDto selectStudentByID(SqlMapClient sqlMap, StudentDto studentdto) {
- //返回后保存在info中
- StudentDto info = new StudentDto();
- try {
- info = (StudentDto)sqlMap.queryForObject("selectByID_student", studentdto);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return info;
- }
- }
2.4 测试类的内容为
- package zh2208.com;
- import java.io.IOException;
- import java.io.Reader;
- import java.util.ArrayList;
- import zh2208.dto.StudentDto;
- import zh2208.impl.StudentImpl;
- import com.ibatis.common.resources.Resources;
- import com.ibatis.sqlmap.client.SqlMapClient;
- import com.ibatis.sqlmap.client.SqlMapClientBuilder;
- /**
- * @author zh2208
- *
- */
- public class MainTest {
- public StudentImpl impl = new StudentImpl();
- public StudentDto info = new StudentDto();
- public static SqlMapClient sqlmapclient = null;
- static {
- try {
- //读取xml文件
- Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
- sqlmapclient = SqlMapClientBuilder.buildSqlMapClient(reader);
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String []args){
- MainTest stu = new MainTest();
- System.out.println("------------------------------- start ------------------------------");
- //以下为各种方法测试
- //添加student表的数据
- //stu.addStudent_test();
- //删除student表的数据
- //stu.delStudent_test();
- //删除student表的指定ID数据
- //stu.delStudentByID_test();
- //更新student表的数据
- //stu.updataStudent_test();
- //查询student表的所有数据
- //stu.selectStudent_test();
- //查询student表的所有数据
- //stu.selectStudentByID_test();
- System.out.println("------------------------------- end ------------------------------");
- }
- //添加student表的数据
- public void addStudent_test(){
- //把要插入的数据填入info对象中
- info.setId(5);
- info.setName("zh2208");
- info.setSex("男");
- info.setAge(24);
- info.setAddress("上海");
- impl.addStudent(sqlmapclient, info);
- }
- //删除student表的数据
- public void delStudent_test(){
- impl.delStudent(sqlmapclient);
- }
- //删除student表的指定ID数据
- public void delStudentByID_test(){
- //指定ID
- info.setId(1);
- impl.delStudentByID(sqlmapclient,info);
- }
- //更新student表的数据
- public void updataStudent_test(){
- //把要更新的数据填入info对象中
- info.setId(6);
- info.setName("zh2208up");
- info.setSex("男");
- info.setAge(20);
- info.setAddress("上海up");
- impl.updataStudent(sqlmapclient, info);
- }
- //查询student表的所有数据
- public void selectStudent_test(){
- StudentDto stu_dto = new StudentDto();
- //检索结果保存到list中
- ArrayList resultList = impl.selectStudent(sqlmapclient);
- for(int i = 0; i < resultList.size();i++){
- stu_dto = (StudentDto) resultList.get(i);
- //打印对象中的信息
- show(stu_dto);
- }
- }
- //查询student表的指定ID数据
- public void selectStudentByID_test(){
- StudentDto stu_dto = new StudentDto();
- info.setId(1);
- stu_dto = impl.selectStudentByID(sqlmapclient,info);
- if(stu_dto != null){
- show(stu_dto);
- }else{
- System.out.println("no data!!!!");
- }
- }
- //打印查询结果
- public void show(StudentDto stu_dto){
- System.out.print("学生ID :" + stu_dto.getId() + " ; ");
- System.out.print("学生姓名 :" + stu_dto.getName() + " ; ");
- System.out.print("学生性别 :" + stu_dto.getSex() + " ; ");
- System.out.print("学生年龄 :" + stu_dto.getAge() + " ; ");
- System.out.print("学生地址 :" + stu_dto.getAddress());
- System.out.println();
- }
- }