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)