Mybatis quick learning (1) and Mybatis quick learning (
I have studied mybatis over the past two days. I want to write the main knowledge points and procedures. I am too lazy to classify them. I will put them in the ssh class for the time being. First, it is an ORM framework with performance between native JDBC and hibernate. You need to write SQL statements by yourself. The development efficiency is slower than hibernate and faster than jdbc, however, the running efficiency is faster than that of hibernate and slower than that of jdbc.
Mybatis Quick Start steps:
First, introduce the package: mybatis package and mysql package.
Second, write the configuration file: as long as it is about the jdbc Configuration:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE configuration PUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <! -- Configure the development environment, such as the development environment and work environment --> <environments default = "development"> <environment id = "development"> <! -- Event Management: Select jdbc --> <transactionManager type = "JDBC"> </transactionManager> <! -- Configure the data 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> <! -- Ing file --> <mapper resource = "jsky/dao/Student. xml"/> </mappers> </configuration>
Step 3: Compile the entity class:
Simple, no code posted
Step 4: Write the corresponding er file, that is, the second ing file:
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- Mark the file --> <mapper namespace = "jksy. dao. student "> <select id =" select "parameterType =" Integer "resultType =" jsky. model. student "> select * from student where id =#{ id} </select> </mapper>
Step 5: Test
// Retrieve the configuration file as the ingress inbound InputStream is = Resources. getResourceAsStream ("mybatis. xml "); // retrieve factorySqlSessionFactory = new SqlSessionFactoryBuilder () through the configuration file handler (). build (is); // get sessionSqlSession session = factory through factory sessions. openSession (); // The first response is reflected to Student. in xml, the second parameter is the related parameter number Student stu = session. selectOne ("jksy. dao. student. select ", 1); System. out. println (stu); session. close ();
This example is a simple one. It is the most basic mybatis operation.