ibatise


ibatis is the same as hibernate, the biggest difference is what is written in the mapping file

1, java class file
2, the xml file corresponding to the class Student.xml: 
equivalent to the entity class written in the map file of hibernate, and the corresponding Operation method, mapping relationship, do not write the corresponding relationship.

However, the mapping file of hibernate is to write the complete correspondence between entities and tables. Mapping relationship

3, SqlMapConfig.xml:
configure some database reference files, integrate the mapping file

The hibernate configuration file is to configure some data source references, integrate the mapping file, and configure the caching strategy

Equivalent to hibernate's configuration file ##

is a placeholder, corresponding to the parameter variables in the method

name=#name#, birth=#birth#, score=#score# Advantages


and disadvantages of Batis:

Advantages:

1. Reduce the amount of code, code Simple sql generation method

2. Performance enhancement;

3. Separation of Sql statement and program code;

4. Enhanced portability;

Disadvantages:

1. Compared with Hibernate, sql needs to be written by yourself;

2. The number of parameters can only be one, more than one Not very convenient for parameters;




    package com.iflytek.entity;
    import java.sql.Date;
    /**
    * @author xudongwang 2011-12-31
    *
    * Email:[email protected]
    *
    */ publicclass
    Student {
    // Note that there is a no-argument constructor here, because the mapping including Hibernate uses reflection, Problems can arise if there is no parameterless construct
    privateint id;
    private String name;
    private Date birth;
    privatefloat score;
    publicint getId() {
    return id;
    }
    publicvoid setId(int id) {
    this.id = id;
    }
    public String getName( ) {
    return name;
    }
    publicvoid setName(String name) {
    this.name = name;
    }
    public Date getBirth() {
    return birth;
    }
    publicvoid setBirth(Date birth) {
    this.birth = birth;
    }
    publicfloat getScore() {
    return score;
    }
    publicvoid setScore(float score) {
    this.score = score;
    }
    @Override
    public String toString() {
    return"id=" + id + "\tname=" + name + "\tmajor=" + birth + "\tscore="
    + score + "\n";
    }
    }


Student.xml

    <?xmlversion="1.0"encoding="UTF-8"?>
    <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
    <sqlMap>
    <!-- Through typeAlias, we do not need to write the package name when using the Student entity class below-->
    <typeAliasalias="Student"type="com.iflytek.entity.Student"/>
    <!-- In this way, if you change the sql in the future, you don't need to change the java code -->
    <!-- id represents the sql statement in the select, and resultClass represents the type of the returned result -->
    <selectid="selectAllStudent"resultClass="Student">
    select * from
    tbl_student
    </select>
    <!-- parameterClass indicates the content of the parameter -->
    <!-- # indicates that this is a parameter that needs to be passed in for an external call, which can be understood as a placeholder -->
    <selectid= "selectStudentById"parameterClass="int"resultClass="Student">
    select * from tbl_student where id=#id#
    </select>
    <! -- Note the resultClass type here, the use of the Student type depends on queryForList or queryForObject -->
    <selectid="selectStudentByName"parameterClass="String"
    resultClass="Student">
    select name,birth,score from tbl_student where name like
    '%$name$%'
    </select>
    <insertid="addStudent"parameterClass="Student" >
    insert into
    tbl_student(name,birth,score) values
    ​​(#name#,#birth#,#score#)
    <selectKeyresultClass="int"keyProperty="id">
    select @@identity as inserted
    <!-- need to be explained here Let's take a look at the generation of primary keys of different databases, which have different methods for their respective databases: -->
    <!-- mysql:SELECT LAST_INSERT_ID() AS VALUE -->
    <!-- mssql:select @@IDENTITY as value -->
    <! -- oracle:SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL -->
    <!-- Another point to note is that different database manufacturers generate primary keys differently, some pre-generate primary keys, such as Oracle and PostgreSQL.
    Some post-generate primary keys, such as MySQL and SQL Server, so if it is an Oracle database, you need to write selectKey before insert -->
    </selectKey>
    </insert>
    <deleteid="deleteStudentById"parameterClass= "int">
    <!-- The id in #id# can be taken at will, but the above insert will have an impact, because the above name will be searched from the attributes in Student -->
    <!-- We can also In this way, if there is a # placeholder, ibatis will call the attributes in parameterClass to assign values ​​-->
    delete from tbl_student where id=#id#
    </delete>
    <updateid="updateStudent"parameterClass="Student">
    update tbl_student set
    name=#name#,birth=#birth#,score=#score# where id=#id#
    </update>





    <?xmlversion="1.0"encoding="UTF-8"?>
    <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
    <sqlMapConfig>
    <!-- 引用JDBC属性的配置文件 -->
    <propertiesresource="com/iflytek/entity/SqlMap.properties"/>
    <!-- 使用JDBC的事务管理 -->
    <transactionManagertype="JDBC">
    <!-- 数据源 -->
    <dataSourcetype="SIMPLE">
    <propertyname="JDBC.Driver"value="${driver}"/>
    <propertyname="JDBC.ConnectionURL"value="${url}"/>
    <propertyname="JDBC.Username"value="${username}"/>
    <propertyname="JDBC.Password"value="${password}"/>
    </dataSource>
    </transactionManager>
    <!-- Multiple entity mapping files can be written here-->
    <sqlMapresource="com/iflytek/entity/Student.xml"/>
    </sqlMapConfig>





















Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327033222&siteId=291194637