Mybatis quick learning (1) and Mybatis quick learning (

Source: Internet
Author: User

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.

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.