MyBatis was originally an open source project of Apache. In 2010, this project was relocated from Apache Software Foundation to Google Code and renamed mybatis. Migrate to GitHub in November 2013.
The term iBatis comes from the combination of "Internet" and "ABATIS". It is a Java -based long -lasting framework. The long -lasting framework provided by iBatis includes SQL MAPS and Data Access Objects (DAO)
First of all, introduce the meaning of mybatis
MyBatis is an excellent and long -lasting framework that supports ordinary SQL queries, storage procedures and advanced mapping. MyBatis eliminates the hand -settings of almost all JDBC code and parameters and the retrieval packaging of the result set. MyBatis can use simple XML or annotations for configuration and original mapping, and map the interface and Java's Pojo (PLAIN OLD JAVA OBJECTS, ordinary Java objects) into records in the database.
2. MYBATIS quickly get started
2.1. Prepare the development environment
1. Create a test item. Ordinary Java projects or Javaweb projects can be available, as shown in the figure below:
2. Add the corresponding jar package
【MyBatis】
mybatis-3.1.1.jar
【MySQL Driver Pack】
MySQL-Connector-Java-5.1.7-bin.jar
3. Create a database and table, for the MySQL database
SQL script is as follows:
Create database mybatis; use mybatis; create table users (id int Primary Key Auto_increment, name varchar (), Age Into); 'Lonely Wolf',); Insert Into Users (name, Age) Values ('White Tiger Emperor',);
Perform the SQL script in mysql database to complete the operation of creating databases and tables, as follows:
At this point, all the preparations for the early development environment were completed.
2.2. Use the data in the MyBatis query table
1. Add mybatis configuration file conf.xml
Create a conf.xml file in the src directory, as shown in the figure below:
The content in the conf.xml file is as follows:
<? xml version = "." encoding = "UTF-"?> <! <! Doctype configuration public "-// mybatis.org/dtd config .//en" http://mybatis.org/mybatis- -CONFIG.DTD "> <Configuration> <ENVIRONMENTS Default =" Development "> <ENVIRONMENT Id =" Development "> <tractionManager Type =" JDBC " /<!-Configure database connection information-- > <datasource type = " POOOLED "> <Property name =" Driver "value =" com.mysql.jdbc.driver "/> <property name =" url "value =" jdbc: mysql: // localhost:/mybatis "/> <proper ty name = "username" value = "root"/> <property name = "password" value = "xdp"/> </dataSource> </Environment> </ENVIRONMENTS> </Configuration>
2. Define the physical class corresponding to the table, as shown in the figure below:
The code of the user class is as follows:
Package me.gacl.DOMain; /*** @AutHor Gacl* Users table corresponding to the physical class* /Public Class user {// The attribute of the physical class and the field name of the table correspond to the private string name; Private int Age; Public int Getid () {Return ID;} Public void setId (int id) {this.id = id;} public string getName () {Return name;} publi. c void setname (string name) {this.Name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "U ser [id=" + id + ", name =" + name + ", age=" + age + "]"; } }
3. Define the SQL mapping file of the users table usermapper.xml
Create a me.gcl.mapping package, specifically used to store SQL mapping files, create a userMapper.xml file in the package, as shown in the figure below:
The content of the usermapper.xml file is as follows:
<? xml version = "." encoding = "uTF-"?>
<! Doctype mapper public "-// mybatis.org/dtd mapper .///en" http://mybatis.org/dtd/mybatis-mapper.dtddd ">
<!- Specify a unique namespace for this mapper, and the value of namespace is set as a package name+SQL mapping file name, so as to ensure that the value of namespace is the only one.
For example, namespace = "me.gacl.mapping.userMapper" is me.gacl.mapping (package name)+userMapper (usermapper.xml file removal of the suffix))
->
<mapper namespace = "me.gacl.mapping.userMapper">
<!- Writing the query SQL statement in the Select tag, set the ID attribute of the SELECT tag to getuser, the ID attribute value must be unique, and the parameterType attribute can be used to indicate the parameter type used during the query. The result set type
Resulttype = "me.gacl.Domain.user" means that the query result is encapsulated into an object of a User class
The user class is the physical class corresponding to the users table
->
<!--
Get a user object based on the ID query
->
<select id="getUser" parameterType="int" resultType="me.gacl.domain.User"> select * from users where id=#{id} </select> </mapper>
4. Register userMapper.xml file in conf.xml file
<?xml version="." encoding="UTF-"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config .//EN" "http://mybatis.org/dtd/mybatis- -CONFIG.DTD "> <Configuration> <ENVIRONMENTS Default =" Development "> <ENVIRONMENT Id =" Development "> <tractionManager Type =" JDBC " /<!-Configure database connection information-- > <dataSource type=" POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:/mybatis" /> <proper ty name= "username" value = "root"/> <property name = "password" value = "xdp"/> </dataSource> </environment> </environments> <Mappers>- Register UserMapper. xml file, userMapper .xml is located under the package of me.gacl.mapping, so Resource is written as Me/GACL/MAPPING/UserMapper.xml-> <Mapper Resource = "Me/GACL/MAPPPIPER.XML"/> </MAPPERS>. </ configuration>
5. Write test code: Select statement defined
Create a TEST1 class and write the following test code:
package me.gacl.test; Import Java.io.ioException; Import Java.Io.InputStream; Import Java.Reader; Import me.gacl.duser; Import e.ibatis.io.Resources; Import org .apache.ibatis.Session.sqlSession; Import org.apache.ibatis.SQLSESSIONFACTORY; Import ILDER; Public Class Test {Public Static Void Main (String [] ARGS) Throws IOEXception {// MyBatis's configuration file string resource = "conf.xml"; // Use a class loader to load the configuration file of mybatis (it also loads related mapping files) inputStream is = test.getClassloader (). am (resource); / /The factory of SQLSESSION SQLSESSIONFACTORY SessionFactory = New SQLSessionFactoryBuilder (). BUILD (IS); // Use the Resources provided by the Resources provided by MyBatis Map file) // Reader Reader = Resources.getResourceAsReader (Resource ); // Construct a factory of sqlSession. signion = sessionFactory.Ovensession (); /*** Map the SQL of SQL The identifier string, * me.gacl.mapping.userMapper is the value of the namespace property of the mapper tag in usermapper.xml file. * GetUser is the ID attribute value of the SELECT tag. */String statement = "me.gacl.mapping.userMapper.getUser"; // The identifier string of the sql // execute the query to return a unique user user = session.selectoctone (statement,). ; System.out .println (user);}}
The execution results are as follows:
As can be seen above, the records in the database have been successfully queried.
The above is all the contents of this article for mybatis entry learning tutorials (1) -Mybatis to get started quickly. I hope it will be helpful to everyone.