MyBatis series (a) introductory MyBatis

Foreword

MyBatis official document: https://mybatis.org/mybatis-3/zh/index.html

MyBatis apache this is an open source project iBatis, 2010 Nian migration project by the apache software foundation to google code, and changed its name to MyBatis. Support is a common SQL query , excellent persistence framework stored procedures and advanced mappings. MyBatis eliminates almost all of the code and to manually set parameters JDBC package and retrieve the result set. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJO (Plain Old Java Objects, ordinary Java Objects) to database records in.

First, prepare the environment

MyBatis Jar package need to reference the database in Java Jar drive package, you can download at the link below, which only MySQL database-driven Jar package with Oracle, if you need to use your own search other databases download.

https://files.cnblogs.com/files/yogouo/MyBatisJar%E5%8C%85.zip

Two, SqlSessionFactory and SqlSession

SqlSessionFactory

SqlSessionFactory is a key target of MyBatis, it is a single database mapping of compiled memory mirroring.
Examples can be obtained by a SqlSessionFactory SqlSessionFactoryBuilder object class.
Once SqlSessionFactory is created, there is during execution.
SqlSessionFactoryBuilder SqlSessionFactory an example may be constructed from a profile pre-customized XML instance or the Configuration.

SqlSession

SqlSession MyBatis is the key target is to perform persistence operations exclusive, similar to the JDBC Connection.
The SqlSession contains all the methods to execute SQL commands against the database, which encapsulates the underlying JDBC connection, can be used to execute the SqlSession instance SQL statements are mapped directly.
Examples SqlSession can not be shared, while also SqlSession thread safe.
SqlSeesion need to be closed after use.

Third, the implementation code

structure

 

 music source

Such fields in the query field and the need to maintain a consistent, if not consistent, you need to add the fields in the query SQL alias name.

package com.mybatis.bean;

public class music {
    private int id;
    private String name;
    private String music;
    private String musicurl;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMusic() {
        return music;
    }
    public void setMusic(String music) {
        this.music = music;
    }
    public String getMusicurl() {
        return musicurl;
    }
    public void setMusicurl(String musicurl) {
        this.musicurl = musicurl;
    }
    
    

}

mybatis-config.xml

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&amp;useSSL=false" />
                <property name="username" value="root" />
                <Property name = "password" value = "Yuhao @ 520" />
            </dataSource>
        </ Environment> 
    </ Environments> 
    <-! our written sql mapping file (musicMapper.xml) must be registered with the global configuration file (mybatis-config.xml) in -> 
    <mappers> 
        <Resource Mapper = "musicMapper.xml" /> 
    </ by mappers> 
</ Configuration>

musicMapper.xml

<?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">
<mapper namespace="com.mybatis.dao.musicMapper">
    
    <select id="getmusicall" resultType="com.mybatis.bean.music">
        select id,name,music,musicurl from test where name = #{name};
    </select>
    
</mapper>

mybatistest

package com.mybatis.test;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

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 org.testng.annotations.Test;

import com.mybatis.bean.music;

public class mybatistest {

    @Test
    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            List<music> GetmusicList = openSession.selectList("com.mybatis.dao.musicMapper.getmusicall", "许嵩");
            for (int i = 0; i < GetmusicList.size(); i++) {
                System.out.println("歌手:" + GetmusicList.get(i).getName());
                System.out.println("歌名:" + GetmusicList.get(i).getMusic() + "---"
                                 + "歌曲网址:" + GetmusicList.get(i).getMusicurl());
            }
            
        } finally {
            openSession.close();
        }
        
    }

}

 

Guess you like

Origin www.cnblogs.com/yogouo/p/11980913.html