初识Mybatis

Posted 唐宋丶元明清

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初识Mybatis相关的知识,希望对你有一定的参考价值。

文档地址:https://mybatis.org/mybatis-3/zh/getting-started.html

步骤:

         第一步导入包,

         第二步获得SqlSessionFactory,得到SqlSession

         第三步,配置Mybatis核心配置文件(名字可以随便取),推荐使用mybatis-config.xml

         第四步,配置数据库配置文件

         第五步,编写Mapper接口层(Dao接口层)

         第六步,配置Mapper层映射xml文件,最好放在mapper层同一包下

         第七步,测试

使用mybatis步骤:第一步导入包

<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
 </dependency>

第二步获得SqlSessionFactory,得到SqlSession

package com.czg.mapper;

import com.czg.pojo.User;
import com.czg.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

/**
 * @program: tx_mybatis
 * @description:
 * @author: czg
 * @create: 2019-10-04 12:55
 */
public class UserMapperTest {


    @Test
    public  void getUsers(){
        SqlSession sqlSession=MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = mapper.getUsers();

        for (User user : users) {
            System.out.println(user.toString());
        }
        sqlSession.close();
    }
}

第三步,配置Mybatis核心配置文件(名字可以随便取),推荐使用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>
    
    <properties resource="db.properties"></properties>

    <typeAliases>
        <typeAlias type="com.czg.pojo.User" alias="user"></typeAlias>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>


    <mappers>
        <mapper resource="com/czg/mapper/mapperXml/UserMapper.xml"/>
    </mappers>
</configuration>

第四步,配置数据库配置文件

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
username=root
password=123456

第五步,编写Mapper接口层(Dao接口层)

public interface UserMapper {

    List<User> getUsers();
}

第六步,配置Mapper层映射xml文件,最好放在mapper层同一包下

<?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.czg.mapper.UserMapper">
    <select id="getUsers" resultType="user">
    select * from user
  </select>
</mapper>

 第七步,测试

public class UserMapperTest {


    @Test
    public  void getUsers(){
        SqlSession sqlSession=MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = mapper.getUsers();

        for (User user : users) {
            System.out.println(user.toString());
        }
        sqlSession.close();
    }
}

 

以上是关于初识Mybatis的主要内容,如果未能解决你的问题,请参考以下文章

初识MyBatis

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

初识MyBatis

初识MyBatis

初识MyBatis