Chinaunix首页 | 论坛 | 博客
  • 博客访问: 488426
  • 博文数量: 111
  • 博客积分: 3146
  • 博客等级: 中校
  • 技术积分: 939
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-07 11:23
个人简介

Nathing

文章分类

全部博文(111)

文章存档

2016年(2)

2015年(1)

2014年(31)

2012年(2)

2011年(9)

2010年(36)

2009年(30)

我的朋友

分类: Java

2010-11-01 23:09:07

1.sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"">
<!-- Always ensure to use the correct XML header as above! -->
<sqlMapConfig>
    <properties resource="jdbc.properties " />
    <!--
    <settings
        cacheModelsEnabled="true"
        enhancementEnabled="true"
        lazyLoadingEnabled="true"
        maxRequests="32"
        maxSessions="10"
        maxTransactions="5"
        useStatementNamespaces="false"
    />
    -->
    <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="com/jj/ibatis/Student.xml" />
    
</sqlMapConfig>


2.jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/lab
username=root
password=root


3.model.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
     PUBLIC "-//ibatis.apache.org//DTD SQL Mao 2.0//EN"
     "">
     
<sqlMap>     
    
    <typeAlias alias="Student" type="com.jj.ibatis.Student"/>
    
    <select id="selectAllStudent" resultClass="Student">
        select *
        from student
    </select>
    
    <select id="selectStudentById" resultClass="Student"
                                 parameterClass="int">
        select *
        from student
        where id=#id#
    </select>
    
    <!-- values中的参数对应POJO类中的get方法,所以不能随便起名,必须和get方法同名 -->
    <insert id="insertStudent" parameterClass="Student">
        insert into
        student(id, name, grade_id, photo, sex)
        values (#id#,#name#,#grade_id#,#photo#,#sex#)
    </insert>
    
    <delete id="deleteStudentById" parameterClass="int">
        delete
        from Student
        where id=#id#
    </delete>
    
    <!-- parameterClass不分大小写 -->
    <update id="updateStudent" parameterClass="student">
        update student
        set name=#name#,
         grade_id=#grade_id#,
         photo=#photo#,
         sex=#sex#
        where id=#id#
    </update>
    
    <select id="selectStudentByName" parameterClass="String"
                                     resultClass="Student">
        select id, name, grade_id, photo, sex
        from student
        where name like '%$name$%'                                
    </select>
    
    <insert id="insertStudentBySequence" parameterClass="Student">
        <selectKey resultClass="int" keyProperty="id">
            select studentPKSequence.nextVal
            from dual
        </selectKey>
        insert into
        Student(id, name, grade_id, photo, sex)
        values (#id#,#name#,#grade_id#,#photo#,#sex#)
    </insert>
    
</sqlMap>


4.CRUD

package com.jj.ibatis;

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;

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 void addStudent(Student student)
    {
        try
        {
            sqlMapClient.insert("insertStudent",student);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

    }

    public void deldteStudentById(int id)
    {
        try
        {
            int i = sqlMapClient.delete("deleteStudentById", id);
            System.out.println(i);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

    }

    @SuppressWarnings("unchecked")
    public List<Student> queryAllStudent()
    {
        List<Student> studentList = null;
        try
        {
            studentList = sqlMapClient.queryForList("selectAllStudent");
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return studentList;
    }

    public Student queryStudentById(int id)
    {
        Student student = null;
        try
        {
            student = (Student)sqlMapClient.queryForObject("selectStudentById", id);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return student;
    }

    @SuppressWarnings("unchecked")
    public List<Student> queryStudentByName(String name)
    {
        List<Student> studentList = null;
        try
        {
            studentList = sqlMapClient.queryForList("selectStudentByName", name);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return studentList;
    }

    public void updateStudent(Student student)
    {
        try
        {
            System.out.println(sqlMapClient.update("updateStudent", student));
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
 
}


阅读(654) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-02 16:58:57

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com