MyBatis getting started

Source: Internet
Author: User
MyBatis is an excellent persistent layer framework that supports common SQL queries, stored procedures, and advanced mappings. MyBatis eliminates the manual setup and completion of almost all JDBC code and parameters.

MyBatis is an excellent persistent layer framework that supports common SQL queries, stored procedures, and advanced mappings. MyBatis eliminates the manual setup and completion of almost all JDBC code and parameters.

1. Introduction to Mybatis

  

MyBatis is an excellent persistent layer framework that supports common SQL queries, stored procedures, and advanced mappings. MyBatis eliminates the manual setup of almost all JDBC code and parameters and retrieval encapsulation of result sets. MyBatis can use simple XML or annotations for configuration and original ing. It maps interfaces and Java POJO (Plain Old Java Objects, common Java Objects) into records in the database.

Ii. mybatis Quick Start 2.1. Prepare the development environment

1. Create a test project, common java project or JavaWeb project, as shown in:

  

2. Add the corresponding jar package

[Mybatis]

Mybatis-3.1.1.jar

MYSQL driver package]
Mysql-connector-java-5.1.7-bin.jar

   

3. create databases and tables for MySQL Databases

The SQL script is as follows:

Mybatis; 2 use mybatis; users (id AUTO_INCREMENT, name varchar (20), age INT); users (NAME, age), 27); users (NAME, age ), 27 );

Run the SQL script in the MySQL database to create the database and Table, as follows:

  

By now, all preparations for the development environment have been completed.

2.2 Use MyBatis to query table data

1. Add the configuration file conf. xml of Mybatis.

Create a conf. xml file in the src directory, as shown in:

  

The content in the conf. xml file is as follows:

2. Define the object class corresponding to the table, as shown in:

  

The User class code is as follows:

1 package me. gacl. domain; gacl 5 * users table object class User {id; 11 private String name; age; getId () {15 return id; 16} setId (int id) {19this. id = id; 20} String getName () {23 return name; 24} setName (String name) {27this. name = name; 28} getAge () {31 return age; 32} setAge (int age) {35this. age = age; 36} 37 38 @ Override39public String toString () {40 return "User [id =" + id + ", age =" + age + "]"; 41} 42}

3. Define the SQL ing file userMapper. xml for operating the users table

Create a me. gacl. mapping package to store the SQL ing file. Create a userMapper. xml file in the package, as shown in:

  

The content of the userMapper. xml file is as follows:

Specify a unique namespace for this mapper. Set the namespace value to the package name + SQL ing file name. This ensures that the namespace value is unique. For example, namespace = "me. gacl. mapping. userMapper "is me. gacl. mapping (package name) + userMapper (userMapper. remove the Suffix from the xml file) write the query SQL statement in the select tag and set the id attribute of the select tag to getUser. The id attribute value must be unique, you cannot use the parameterType attribute to specify the parameter type used for the query. The resultType attribute indicates the type of the returned result set 9 resultType = "me. gacl. domain. "User" indicates that the query results are encapsulated into an object of the User class and 10 objects of the User class are returned. The object class corresponding to the users table obtains a user object select * from users where id = according to the id query. # {id}

4. register the userMapper. xml file in the conf. xml file.

Register the userMapper. xml file,

5. Compile the test code: Execute the defined select statement.

Create a Test1 class and write the following test code:

Package me. gacl. test; import java. io. IOException; import java. io. inputStream; import java. io. reader; import me. gacl. domain. user; import org. apache. ibatis. io. resources; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import org. apache. ibatis. session. sqlSessionFactoryBuilder; public class Test1 {main (String [] args) throws IOException {// String resource = "conf. xml "; // use the class loader to load the configuration file of mybatis (It also loads the associated ing file) InputStream is = Test1.class. getClassLoader (). getResourceAsStream (resource); // The SqlSessionFactory sessionFactory that constructs the sqlSession = new SqlSessionFactoryBuilder (). build (is); // use the Resources class provided by MyBatis to load the configuration file of mybatis (It also loads the associated ing file) // Reader reader = Resources. getResourceAsReader (resource); // construct the sqlSession factory // SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder (). build (reader); // create sqlSessionSqlSession session = sessionFactory that can execute SQL IN THE ing file. openSession ();/*** ing the SQL identifier string, * me. gacl. mapping. userMapper is userMapper. the namespace attribute value of the ER er tag in the xml file. * getUser is the id attribute value of the select tag, you can find the SQL statement to be executed */String statement = "me. gacl. mapping. userMapper. getUser "; // ing the SQL identity string // execute the query and return the sqlUser user = session of a unique user object. selectOne (statement, 1); System. out. println (user );}}

The execution result is as follows:

  

We can see that records in the database have been successfully queried.

Practical application of Java: Mybatis implements addition, deletion, and modification of a single table

[Java] [Mybatis] physical paging implementation

Mybatis quick start tutorial

Test batis's batch data operations

In MybatisBatch insert of Object List

For more details, please continue to read the highlights on the next page:

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.