The use of the Spring series 3--ibatis

Source: Internet
Author: User
Tags cdata

We have docked the database, currently lacks ORM, we use Ibatis to implement ORM.

First, directory structure

Talk is cheap, first look at the directory structure of our project:


Two, ORM class

We first set up three files:

1.Person class: holds the corresponding relation with the table field in the database, and the set, get method

2.PersonDAO interface: Operational functions for database tables

3.PersonDAOImpl class: Implement Persondao interface

public class person implements Serializable {private static final long Serialversionuid =-51741316

    5963030507L;
    private int id;
    private String name;

    private int sex;
     /** * CREATE TABLE person (ID int primary key, name varchar (), sex int);
        * * Public person () {} public person (int id,string name,int sex) {this.id = ID;
        THIS.name = name;
    This.sex = sex;
    public int getId () {return id;
    The public void setId (int id) {this.id = ID;
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    public int Getsex () {return sex;
    The public void setsex (int sex) {this.sex = sex;
    @Override public String toString () {return "id=" + ID + "\tname=" + name + "\tsex=" + sex; }

}
Public interface Persondao {public
    boolean insertperson (person);   Add Public
    boolean deletebyid (int id);            Delete public
    list<person> Queryallperson ();         Query All
}
public class Persondaoimpl extends Sqlmapclientdaosupport implements Persondao {

    @Override public
    Boolean InsertPerson {
        getsqlmapclienttemplate (). Insert ("InsertPerson", person);
        return false;
    }

    @Override Public
    boolean deletebyid (int id) {
        getsqlmapclienttemplate (). Delete ("Deletebyid", id);
        return false;
    }

    @Override public
    list<person> Queryallperson () {
        list<person> persons = Getsqlmapclienttemplate (). queryForList ("Queryallperson");
        return persons
    }
}
Third, the configuration file
1. Introduction of the Ibatis Sqlmap package, added in Pom.xml:
    <dependency>
      <groupId>org.apache.ibatis</groupId>
      <artifactid>ibatis-sqlmap </artifactId>
      <version>2.3.4.726</version>
    </dependency>
2.spring-core.xml file to add Ibatis SQL map definition:
    <!--iBatis SQL map definition--> <bean id= "sqlmapclient" class= "Org.springframework.orm.ibatis.SqlMapClientFacto" Rybean "> <property name=" dataSource "ref=" DataSource "/>" <property name= "configlocation" value = "Classpath:/sql-map-config.xml"/> </bean> <bean id= "Persondaoimpl" class= "Com.alibaba.druid.model.Pe" Rsondaoimpl "> <!--dataSource not required--> <property name=" DataSource "> <ref loca L= "DataSource"/> </property> <!--sqlmapclient required--> <property name= "sqlmapcli Ent "> <ref local=" sqlmapclient "/> </property> </bean> <!--transactio Nmanager is not required--> <bean id= "TransactionManager" class= "
            Org.springframework.jdbc.datasource.DataSourceTransactionManager "> <property name=" DataSource ">
 <ref local= "DataSource"/> </property> </bean>
3. The 2nd step involves sql-map-config.xml files, creating a new document and adding:
<?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>
    <!--here must not have <settings/> label-->
    <!--< Settings   cachemodelsenabled= "true"-->
                 <!--enhancementenabled= "true"-->
                 <!-- Lazyloadingenabled= "true"-->
                 <!--errortracingenabled= "true"-->
                 <!--maxrequests= "-->"
                 <!--maxsessions= "ten"-->
                 <!--maxtransactions= "5"-->
                 <!--usestatementnamespaces= " False "/>-->

    <sqlmap resource=" person.xml "/>

</sqlMapConfig>
4. The 3rd step involves person.xml files, creating a new document and adding:
 
<?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" > & lt;sqlmap> <typealias alias= "person" type= "Com.alibaba.druid.model.Person"/> <insert id= "Insertperso" N "parameterclass=" Person > <! [cdata[INSERT into the person values (#id #, #name #, #sex #)]]> </insert> <delet E id= "Deletebyid" parameterclass= "int" > <! [cdata[Delete from person where id= #id #]]> </delete> <update id= ' update Person "parameterclass=" > <!

    [cdata[Update person set name= #name #,sex= #sex # where id= #id #]]> </update> <select id= "Querybyid" parameterclass= "int" resultclass= "person" > <! [cdata[select * from person where id= #id #]]> </select>

    <select id= "Queryallperson" cachemodel= "Personcache" resultclass= "person" > <! [cdata[SELECT * from person]]> </select> </sqlMap>
Four, test
Create a new test class to test the connectivity of the database:
public class Persondaoimpltest {
    private static applicationcontext ApplicationContext;   Provide static ApplicationContext
    private static Persondaoimpl Persondaoimpl;

    static{
        applicationcontext = new Classpathxmlapplicationcontext ("Spring-core.xml");
        Persondaoimpl = (Persondaoimpl) applicationcontext.getbean ("Persondaoimpl");
    }
    Add Operation
    @Test public
    void Testinsertperson () {
        Persondaoimpl.insertperson (new person (1, Zhangsan, 2));

    //delete operation
    @Test public
    void Testdeleteperson () {
        Persondaoimpl.deletebyid (1);
    }

    Query all
    @Test public
    void Testqueryallperson () {
        list<person> persons = Persondaoimpl.queryallperson ();
        System.out.println (Persons.size ());
        iterator<person> ite = Persons.iterator ();
        while (Ite.hasnext ()) {person person
            = Ite.next ();
            SYSTEM.OUT.PRINTLN (person);}}







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.