Learn Mybatis step by step-build the simplest development environment-opening part (1)

Source: Internet
Author: User

I recently learned the Mybatis framework and found many articles during my study. I personally feel that there are too many things on the official website, which is not suitable for many friends who wish to get started quickly, of course, I think it is faster or StackOverflow to find the problem on the official website. For this reason, I want to take notes on my learning experience for reference, minimize detours. Because I want to know how to use Mybatis directly, and many learning articles on the Internet are examples of the combination of Mybatis and Spring, or examples created directly under the Web Project, I feel that a friend who just wants to know how to use that item should not need so many additional conditions, so this series uses the simplest Java Project as an example demonstration.

What is Mybatis, formerly known as iBatis (I have never used it, so I have no right to speak). According to the official website's original intention, Mybatis supports common SQL queries and can perform SQL operations more simply and directly, an excellent persistence layer framework for stored procedures and advanced ing. Using It can basically eliminate all manual settings of JDBC barley and parameters and retrieve result sets. In fact, I personally feel like using it, in fact, the main work is to configure XML or annotations, and then map interfaces to Java POJO (common Java objects to database records). I have never used Java Hb, therefore, you do not have the permission to express your opinions on the differences between the two. I believe that the emergence of each thing will always have its own purpose.

After learning this time, the main process for Mybatis cannot be the following steps:

1. Obtain SessionFactory from the XML configuration file, and then SessionFactory generates the corresponding Session.

2. Use the Session object to perform the corresponding CRUD operations (add, delete, modify, and query) and transaction control on the business data.

3. Close the corresponding Session after use to avoid excessive resource occupation.

4. Configure the corresponding Mapper xml file for Map operations between the JavaBean of the business entity and the database table.

  

Preparations before the war:

1. Development Environment Eclipse JavaEE IDE, JDK 1.6, database mysql 5.5

2. Download the corresponding Jar package for later use

Mybatis-3.2.3.zip decompress and take out the mybatis-3.2.3.jar, =>: http://code.google.com/p/mybatis/ (Mybatis core package)

Mybatis-generator-core-1.3.1.jar =>: http://code.google.com/p/mybatis/wiki/Generator (Mybatis automatically generated configuration package)

Mysql-connector-java-5.1.26-bin.jar =>: http://dev.mysql.com/downloads/connector/j/ (Mysql jdbc driver package)

  

Next, you can create a Java Project named MybatisDemo under Eclipse, and create the corresponding Package Structure and folder structure as shown in, where config and mapper are folders, respectively,

The package david. mybatis. demo and the package david. mybatis. model respectively store the corresponding demo running program and Javabean object. The lib folder stores the downloaded third-party jar packages.

            

  

After creating the following directory, we can add the corresponding Jar package, as shown in figure

  

  

After completion, execute the following SQL statement to create the table structure required by the DEMO. Three tables are available: Visitor (Visitor table), Website (Website table), and Channel (Channel table)

  

()()()()()Table creation SQL

After all this is done, we are about to start ~

As mentioned at the beginning, all the configurations of Mybatis are sourced from an XML configuration file. In the config folder, create a configuration file named mybatis_demo_config.xml, this is one of the core operations we need to follow.

Pay attention to the configuration of this fileHierarchical order

1. typeAliases => alias node. You can set the attributes of this node, so that this alias can be used in other places in the configuration file where the object name is required, rather than the fully qualified name.

For example, <typeAlias type = "david. mybatis. model. Visitor" alias = "Visitor"/>

2. environments => environment node, configure data connection information

3. mappers => Configure the SQL ing statement.

The simplest configuration is as follows:

XML configuration

Create a new class named MyBatisUtils under the package david. mybatis. demo, which stores the methods for obtaining SqlSession and disabling SqlSession, and extracts the methods to facilitate reuse multiple times.

String CONFIG_PATH = "config/mybatis_demo_config.xml" = SqlSessionFactory factory = showMessages (CRUD_Enum type, "added" + count + "records. "" Deleted "+ count +" records. "" Updated "+ count +" records. "" Matched "+ count +" records. "" Total "+ count +" records. "MybatisUtils tool class

Create a class named Visitor under the package david. mybatis. model for the corresponding OR Mapping.

CreateTime =. name =. email =. status = 1. createTime =. name =. email = String. format ("{Id: % d, Name: % s, CreateTime: % s}" SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"Visitor class

Create a VisitorMapper. xml file under the package david. mybatis. demo to map the corresponding SQL statement.

Note that namespace => david. mybatis. demo. IVisitorOperation must be the actual file name under the corresponding package. IVisitorOperation, otherwise the corresponding ing file cannot be loaded successfully.

* From visitor where id => 0 </select> </mapper>IVisitorOperation Interface

Next, run the following program.

TestBasicQuery (= (Visitor) session. selectOne ("david. mybatis. demo. IVisitorOperation. basicQuery"Basic Query

The simplest execution result is coming out.

This is the HelloWord of the Mybatis series. Next, we will talk about how to use interfaces.

  

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.