MyBatis Quick Learning (i)

Source: Internet
Author: User

These two days to learn MyBatis, write about the main points of knowledge and process it, too lazy to classify, for the moment put it in the SSH class inside it. First, it's an ORM framework, where performance is between native JDBC and hibernate, and you need to write your own SQL statements, which are slower to develop than hibernate, faster than JDBC, but faster than hibernate for operational efficiency, slower than JDBC, It's the same anyway.

MyBatis Quick Start steps:

First: Introduction Package: MyBatis Package and MySQL package.

Second: Write the configuration file: As long as the configuration is about JDBC:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE configuration Public          "-//mybatis.org//dtd Config 3.0//en"        "http://mybatis.org/dtd/ Mybatis-3-config.dtd "><configuration>    <!--configuration environment, can be developed environment and work environment, such as--><environments default=" Development "><environment id=" development "><!--Management, select jdbc--><transactionmanager type=" JDBC " ></transactionManager><!--Configuration Source--><datasource type= "pooled" ><property name= "Driver" value = "Com.mysql.jdbc.Driver"/><property name= "url" value= "jdbc:mysql://127.0.0.1:3306/jsky?useunicode=true &characterencoding=utf8 "/><property name=" username "value=" root "/><property name=" password "value= "123456"/></datasource></environment></environments><mappers><!--mapping file-< Mapper resource= "Jsky/dao/student.xml"/></mappers></configuration>
Step three: Write the entity class:

It's a little simple, no code.

Fourth step: Write the corresponding mapper file, which is the second mapped file:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en"        "HTTP://MYBATIS.ORG/DTD/MYBATIS-3-MAPPER.DTD" >        <!--The file's label-        <mapper namespace= "jksy.dao.student" >        <select id= "select" ParameterType = "Integer" resulttype= "jsky.model.Student" >        select * from Student where id=#{id}        </select>        < /mapper>
Fifth Step: Test


                Read the configuration file as the input InputStream is=resources.getresourceasstream ("Mybatis.xml");//pass through the configuration file for Factorysqlsessionfactory Factory=new Sqlsessionfactorybuilder (). Build (IS);//Access Sessionsqlsession session=factory.opensession () through factory;// Here the first parameters are reflected on the Student.xml method, the second is the relative student Stu=session.selectone ("Jksy.dao.student.select", 1); System.out.println (Stu); Session.close ();
This example is slightly simpler and is the most basic mybatis operation.

MyBatis Quick Learning (i)

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.