Getting Started with Mybits

Source: Internet
Author: User
First, MyBatis introduction

  

MyBatis is an excellent persistence layer framework that supports common SQL queries , stored Procedures , and advanced mappings . MyBatis eliminates the manual setting of almost all JDBC code and parameters and the retrieval encapsulation of the result set. MyBatis can use simple XML or annotations for configuration and raw mappings, mapping interfaces and Java Pojo (Plain old Java Objects, normal Java objects) to records in the database. Second, MyBatis Quick Start 2.1. Prepare development environment

1. Create a test project, a normal Java project or a Javaweb project, as shown in the following illustration:

  

2, add the appropriate jar package

"MyBatis"

Mybatis-3.1.1.jar

"MySQL driver package"
Mysql-connector-java-5.1.7-bin.jar

   

3, CREATE database and table, for MySQL database

The SQL script is as follows:

1 CREATE database MyBatis;
2 use MyBatis;
3 CREATE TABLE users (id int PRIMARY KEY auto_increment, NAME VARCHAR,age INT);
);
);

Execute the SQL script in the MySQL database, complete the creation of the database and the table, as follows:

  

To this end, the early development of the environmental preparation of all completed. 2.2, the use of MyBatis query table data

  1, add the MyBatis profile Conf.xml

Create a conf.xml file in the SRC directory, as shown in the following illustration:

  

The contents of the Conf.xml file are as follows:

1 <?xml version= "1.0" encoding= "UTF-8"?>
 2 <! DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" >
 3 <configuration>
 4     <environments default= "Development" >
 5         <environment id= " Development ">
 6             <transactionmanager type=" JDBC "/>
 7             <!--metabase connection information-->
 8             <datasource type= "Pooled" >
 9                 <property name= "Driver" value= "Com.mysql.jdbc.Driver"/>                 <property name= "url" value= "Jdbc:mysql://localhost:3306/mybatis"/> One                 <property Name= "username" value= "root"/>                 <property name= "password" value= "XDP"/> </             datasource>         </environment>     </environments>     
</ Configuration>

  2. Define the entity classes that the table corresponds to, as shown in the following illustration:

  

The code for the user class is as follows:

1 package me.gacl.domain;
 2 
 3/**
 4  * @author gacl
 5  * The user table corresponds to entity Class 6 * * 7 public
 class User {
 8 
 9     //Entity class properties and table field name one by one corresponding to the     private int id;
Individually     private String name;
A     private int age;
public     int getId () {return         ID;
The     public     void setId (int id) {         this.id = ID;
Public     String getName () {n ' return         name;
The     public     void SetName (String name) {         this.name = name;
The     public     int getage () {
to return to age; The     public     void Setage (int.) {         this.age = age;
id=
@Override public     String toString () {A return         "User [" + ID + ", name=" + name + ", age=" + Age + "]";
42}

3. Define SQL mapping files that manipulate the users table Usermapper.xml

Create a me.gacl.mapping package that is designed to hold the SQL mapping file and create a usermapper.xml file in the package as shown in the following illustration:

  

The contents of the Usermapper.xml file are as follows:

 1 <?xml version=" 1.0 "encoding=" UTF-8 "?> 2 <! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <!-- Specifying a unique Namespace,namespace value for this mapper is customarily set to the package name +sql mapping file name so that the namespace value is the only 4 for example namespace= " Me.gacl.mapping.userMapper "is me.gacl.mapping (package name) +usermapper (usermapper.xml file removal suffix) 5--> 6 <mapper namespace=" Me.gacl.mapping.userMapper "> 7 <!--to write the query's SQL statement in the Select tab, set the Select Label ID property to Getuser,id property value must be unique and not be able to repeat 8 use PA The Rametertype property indicates the type of parameter used in the query, Resulttype property indicates that the result set type returned by the query resulttype= "Me.gacl.domain.User" means that the object that encapsulates the query result into a User class returns the U The Ser class is the corresponding entity class for the Users table--> <!--13 Gets a user object--> <select id= by ID query GetUser "Parametertype= int" resulttype= "Me.gacl.domain.User" > 

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.