Tutorial on building java + mybatis framework + MySQL development environment under Windows

Mybatis is a Java persistence framework that associates objects with stored procedures or SQL statements through XML descriptors or annotations.

Mybatis is free software distributed under Apache license 2.0. It is a branch version of ibatis 3.0. Its maintenance team also includes startup members of ibatis. Unlike other object relational mapping frameworks, mybatis does not associate Java objects with database tables, but Java methods with SQL statements. Mybatis allows users to make full use of various functions of the database, such as stored procedures, views, various complex queries, and the proprietary features of a database. If you want to operate legacy databases, non-standard databases, or fully control the execution of SQL, mybatis is a good choice.

Compared with JDBC, mybatis simplifies the related code: SQL statements can be executed in one line of code. Mybatis provides a mapping engine that declaratively maps the execution results of SQL statements to the object tree. SQL statements can be generated dynamically by using a built-in XML like expression language or Apache velocity integrated plug-in.

Mybatis integrates with the spring framework and Google Guice, which frees developers from dependency issues.

Mybatis supports declarative data caching. When an SQL statement is marked as "cacheable" After that, all the data obtained from the database when it is executed for the first time will be stored in a cache. In the future, when this statement is executed, the results will be read from the cache instead of hitting the database again. Mybatis provides a cache implementation based on Java HashMap by default and a default connector for connecting to OSCache, ehcache, hazelcast and memcached. Mybatis also provides APIs for other caching implementations.  

Key points after learning this period of time, in fact, the main process for mybatis cannot be the following steps

  1. Obtain the sessionfactory from the XML configuration file, and then generate the corresponding session from the sessionfactory.

  2. The session object is used to complete the corresponding CRUD operations (addition, deletion, modification and query) and corresponding transaction control on business data.

  3. Close the corresponding session after use to avoid excessive resource consumption

  4. Use the corresponding mapper XML file to configure the corresponding map operation between the JavaBean of the business entity and the database table

Pre war preparation:

  1. Development environment eclipse JavaEE IDE, JDK 1.6, database mysql 5.5

  2. Download the corresponding jar package for future use

    mybatis-3.2. 3. Unzip and take out mybatis-3.2 3. Jar, = > download address: http://code.google.com/p/mybatis/ (mybatis core package)

    mybatis-generator-core-1.3. 1. Jar = > download address: http://code.google.com/p/mybatis/wiki/Generator (mybatis automatically generates configuration file packages)

    mysql-connector-java-5.1. 26-bin. Jar = > download address: http://dev.MysqL.com/downloads/connector/j/ (jdbc driver package for MySQL)

Construction steps

Next, you can build a java project named mybatisdemo under eclipse, and create the corresponding package structure and folder structure as shown in the figure below, where config and mapper are folders respectively,

Package David mybatis. Demo and package David mybatis. The corresponding demo running program and Java Bean object are stored in the model, and the third-party jar packages just downloaded are stored in the Lib folder.

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

After completion, execute the following SQL to establish the table structure required by demo, including three tables, visitor table, website table and channel table

When all this is done, we'll start~

As mentioned at the beginning, all configurations of mybatis are derived from an XML configuration file. We need to create a new file named mybatis in the config folder_ demo_ config. XML configuration file, which is one of the core operations we need later.

When configuring this file, be sure to note that the elements in the < configuration > node have hierarchical order requirements, and the order cannot be changed at will. Otherwise, exceptions will occur when loading the XML configuration file, resulting in unsuccessful subsequent operations.

Specific node descriptions can be viewed http://mybatis.github.io/mybatis-3/zh/configuration.html# , only the commonly used nodes, typealiases, environments and mappers, are mentioned here.

1. Typealiases = > alias node. You can set the properties of this node so that other places in the configuration file that need entity names can use this alias instead of fully qualified names

For example, < typealias type = "David. Mybatis. Model. Visitor" alias = "visitor" / >

2. Environments = > environment node, configure information related to data connection

3. Mappers = > configure SQL mapping statements.

The simplest configuration is as follows:

In package David mybatis. A new class named mybatisutils is created under the demo, which stores the methods to obtain sqlsession and close sqlsession. It is refined to facilitate multiple reuse.

In package David mybatis. A new class named visitor is created under model for corresponding or mapping.

In package David mybatis. Under demo, create a new visitormapper XML to map corresponding SQL statements.

Notice that namespace = > David mybatis. demo. Ivisotoroperation must correspond to the actual file name under the package, otherwise the corresponding mapping file cannot be loaded successfully

Next, run the following program

The simplest execution result comes out

This is the helloword of the mybatis series~

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>