Java Learning Note--mybatis Three Musketeers (MyBatis)

Source: Internet
Author: User

Using the MyBatis starter program in a Java project

Wanna attention

2017.03.23 14:33* Words 270 read 1243 Comments 0 Likes 5

MyBatis is an excellent persistence layer framework that supports the customization of SQL, stored procedures, and advanced mapping.

Welcome to visit my blog: Http://wangnan.tech

What is MyBatis?

MyBatis is an excellent persistence layer framework that supports the customization of SQL, stored procedures, and advanced mapping.

MyBatis GitHub

Https://github.com/mybatis/mybatis-3

MyBatis Documentation

Http://mybatis.github.io/mybatis-3/zh/index.html

Starter Program

To use MyBatis, simply place the Mybatis-x.x.x.jar file in Classpath.
If you are building a project using Maven, you need to place the following dependency in Pom.xml:

    <dependency>
        <groupId> org.mybatis </groupId>
        <artifactId> mybatis </artifactId>
        <version> 3.8.2 </version>
    </dependency>

I chose the second one to create a new MAVEN project in Eclipse

Project structure:

01.jpg

Each document introduces:
    • Pom file: Due to the use of MySQL database, a MySQL driver package is also relied upon
    • Entity class User:
    • Usermapper Interface:
    • Implementation of the Usermapper interface, Usermapper.xml:
    • Data Source Configuration Configuration.xml
    • Test class Mybatistest:
     <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
      <modelVersion> 4.0.0 </modelVersion>
      <groupId> Mybatis </groupId>
      <artifactId> Mybatis </artifactId>
      <version> 0.0.1-SNAPSHOT </version>
      <dependencies>
        <dependency>
          <groupId> org.mybatis </groupId>
          <artifactId> mybatis </artifactId>
          <version> 3.2.8 </version>
        </dependency>
        <dependency>
          <groupId> mysql </groupId>
          <artifactId> mysql-connector-java </artifactId>
          <version> 5.1.34 </version>
        </dependency>
      </dependencies>
     </project>
  Package com.mybatis.domain;
 
 
    Public{
         Private String name;
         Private Integer age;
         {
            Return name;
         }
         {
         This.name = name;
         }
         {
         Return age;
         }
         {
            This.age = age;
         }
    }
Package
Import
 
Public
    Public User FindByID (String Id)
}
    <?xml version= "1.0" encoding= "UTF-8"?> <!  DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  <!--equivalent to the implementation of the Usermapper interface namespace must be a Usermapper class path--
    <mapper namespace= "Com.mybatis.mapper.UserMapper" >
        <!--FindByID must return a user--> as the method name in the interface
        <select id= "FindByID" parametertype= "String"
Resulttype= "Com.mybatis.domain.User" >
        </select>
    </mapper>
    <?xml version= "1.0" encoding= "UTF-8"?>
    <! DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" >
    <configuration>
        <!--data Source configuration--
        <environments default= "Development" >
            <environment id= "Development" >
                <transactionmanager type= "jdbc"/>
                <datasource type= "Pooled" >
                    <property name= "Driver" value= "Com.mysql.jdbc.Driver"/>
                    <property name= "url" value= "Jdbc:mysql://localhost:3306/test"/>
                    <property name= "username" value= "root"/>
                    <property name= "Password" value= "123456"/>
                </dataSource>
            </environment>
        </environments>  <mappers>
            <!--usermapper.xml loading in--
            <mapper resource= "Usermapper.xml"/>
        </mappers>
    </configuration>
Package com.mybatis.test;
 
Import com.mybatis.domain.User;
 
Import com.mybatis.mapper.UserMapper;
 
Import org.apache.ibatis.io.Resources;
Import org.apache.ibatis.session.SqlSession;
Import org.apache.ibatis.session.SqlSessionFactory;
Import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
Import java.io.IOException;
 
 
Public{
    /** * MyBatis sqlsessionfactory * sqlsessionfactory???? Sqlsession???????????? Sqlsession?????????? Commit?rollback?close???? * @return * *{
Null;
"Configuration.xml";
        Try {
New SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(
                        resource));
Catch (IOException e) {
            e.printStackTrace();
        }
        Return sessionFactory;
    }
 
    {
        SqlSession sqlSession = getSessionFactory().openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.findById("1");
        System.out.println(user.getName());
    }
}
 

Insert bar data in the database:
Id= "1" name= "WN" age= "23"

Java Learning Note--mybatis Three Musketeers (MyBatis)

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.