Excellent persistence framework Mybatis, connect to the database quick step

Then we learned before and JDBC connection pool, the efficiency of programming siege lion who is still not very satisfactory. Ever! With more excellent content today, and that is Mybatis framework. It appears to solve some of the problems in the jdbc improve the robustness of the code. We look at it ~

Mybatis Introduction

After Mybatis in 2010 migrated from an open source project under the Apache ibatis to Google codde, change mybatis.

This is a very useful framework for persistence layer, we had achieved in this framework encapsulates database jdbc operations, we just need to care about our SQL database itself can, and do not bother to register the drive, connected databases, etc. Complex process.

Here, it is primarily used to execute our xml configuration of a sleep, and the statement by java object mapping in sql we eventually want to generate the SQL statement, and finally, we executed sql statement by the mybatis frame and map the results into java objects can be returned.

Mybatis Architecture (keep in mind)

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

1, mybatis profile

In the figure above, we can see the top SqlMapConfig.xml, this is our profile global configuration file, which we can configure mybatis operating environment, etc., and mapper.xml file is what we call sql mapping file, we in this file to configure the sql statement, when the execution of this document also need to load the global configuration file.

2, the information by some configurations, etc., we can construct a SqlSessionFactory (session factory)

3. Create sqlSession (session) through the session factory, sql our operation is performed by sqlSession.

4, the bottom mybatis custom database interface operation actuator Executor

5, Mapped Statement, which encapsulates some configuration information and mapping information mybatis sql like. mapper.xml each of our sql file corresponds to a Mapped Statement object.

Mybatis entry procedures

Give everyone a look at the layout of all the code files. The following steps, we went to completion in accordance with these.

Learn together big data | good persistence framework Mybatis, connect to the database quick step

1. Download jar package

下载地址 https://github.com/mybatis/mybatis-3/releases

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

在这个包里面我们可以看到,第一个是其依赖包,第三个是其核心包。

2.环境搭建

首先,我们先新建一个项目工程,新建lib,将我们上面的核心包和依赖包全部加入到项目中,除此之外,不要忘了还有我们的数据库的驱动包也要添加进来(参考开篇文章)。具体的效果如下图。

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

3.加入配置文件

我们新建一个资源文件包,并在其中加入3个配置文件。

3.1配置文件log4j.properties(日志信息)

对于日文件我们直接拷贝即可。

# Global logging configurationlog4j.rootLogger=DEBUG, stdout# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

3.2配置文件 db.properties

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

我们通过key,value的形式来配置加载数据库的连接。

3.3配置文件SqlMapConfig.xml

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

上面图片就是一个.xml的配置文件,我们通过<properties>将数据库连接的文件加载进来,通过typeAlias修一下我们过长的类型,再往下就是加载我们的数据库信息了。

其中主要的配置信息大概是这个样子的,

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

3.封装pojo

我们的数据库是这么定义的。

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

像以前一样我们将我们使用的字段进行封装,获取get,set方法,tostring方法。

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

4.Mapper.xml(映射文件)

接下来,我们需要定义一个mapper映射文件UserMapper.xml,具体的效果图如下所示。这个文件其实就是存我们各种sql语句的。

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

Specific content, where we have defined three SQL statements, which are user queries and store user information sql statement.

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

5.UserMapper (interface file)

Follows, we define three interfaces, respectively, after the way we achieve.

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

6. Load files UserMapper.xml

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

7. Test

When the test is that we want to follow a flow chart of the top, where you need to write content where only the red box, we only demonstrate the id query. FIG code is as follows.

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

Once complete, we will finish the code. We will select the class name, right click run as to run the code to pass the test.

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

After the printing is complete, you will see a lot of lines operating results, this is not an error, the log file is set before us, we look carefully, we will be able to find the results.

Learn together big data | good persistence framework Mybatis, connect to the database quick step

 

The above is an excellent open source persistence framework Mybatis us, we jar package after addition, we add three profiles to load the database, the data complete package.

We only need to add a sql statement UserMapper.xml file, you can execute the sql statement by mybatis framework and mapping the results into a java object can be returned. Where we need to be modified, in fact, these two, is not more quick step.

Guess you like

Origin www.cnblogs.com/dashjunih/p/10990370.html