Getting started with java--MyBatis

Getting Started with java – MyBatis

The real power of MyBatis lies in its mapping statement, which is also its magic. Because of how powerful it is, the mapper's XML file is relatively simple. If you compare this with the JDBC code that has the same functionality, you'll immediately see that you've saved almost 95% of the code. MyBatis is built for SQL and does it better than normal.



The SQL mapping file has a few top-level elements (in the order they should be defined):

  • cache– Cache configuration for a given namespace.
  • cache-ref– References to other namespace cache configurations.
  • resultMap– is the most complex and powerful element, used to describe how to load objects from the database result set.
  • sql– A reusable block of statements that can be referenced by other statements.
  • insert– Map insert statements.
  • update– Mapping update statements.
  • delete– Map delete statements.
  • select– Map query statement.


If you use Mavento build the project, you need to put the following dependency code in pom.xmlthe file.



Simple example (need to import shelf packages: database driver shelf package and mybatis shelf package):

Entity classes are still used UserInfo.

mapperkind:

package cn.zbw.mapper;

import cn.zbw.entity.UserInfo;

import java.util.List;

public interface UserInfoMapper {
    
    
    public List<UserInfo> findUserAll();
}

mapper.xmlFile configuration:

<?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="cn.zbw.mapper.UserInfoMapper">
    <select id="findUserAll" resultType="cn.zbw.entity.UserInfo">
        select * from user
    </select>
</mapper>

Tools:

package cn.zbw.utils;

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;
import java.io.InputStream;

public class MybatisUtil {
    
    
    private static SqlSessionFactory sqlSessionFactory;
    static {
    
    
        String resource = "mybatis.xml";
        try {
    
    
            InputStream is = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
    public static SqlSession getSession(){
    
    
        return sqlSessionFactory.openSession();
    }
    public static void closeSession(SqlSession sqlSession){
    
    
        if (sqlSession!=null){
    
    
            sqlSession.close();
        }
    }
}

mybatis.xmlFile configuration:

<?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="zbw">
        <environment id="zbw">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/zh_demo?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC"></property>
                <property name="username" value="root"></property>
                <property name="password" value="123456"></property>
            </dataSource>
        </environment>
    </environments>

    <!--配置接口所对应的xml文件-->
    <mappers>
        <mapper resource="cn/zbw/mapper/UserInfoMapper.xml"></mapper>
    </mappers>
</configuration>

Test class:

package cn.zbw.test;

import cn.zbw.entity.UserInfo;
import cn.zbw.mapper.UserInfoMapper;
import cn.zbw.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class Test01 {
    
    
    public static void main(String[] args) {
    
    
        test01();
    }

    private static void test01() {
    
    
        SqlSession session= MybatisUtil.getSession();
        List<UserInfo> lists=session.getMapper(UserInfoMapper.class).findUserAll();
        for (UserInfo u:lists){
    
    
            System.out.println(u.getUsername());
        }
    }
}

turn out:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45686583/article/details/114669105