首页 > 编程语言 >mybatis源码环境搭建

mybatis源码环境搭建

时间:2022-11-27 10:25:14浏览次数:39  
标签:name age id String 源码 mybatis Integer public 搭建

MyTest.java

  @Test
  public void test() {
    String resource = "mybatis-config.xml";
    InputStream inputStream = null;
    try {
      inputStream = Resources.getResourceAsStream(resource);
    } catch (IOException e) {
      e.printStackTrace();
    }
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    SqlSession sqlSession = sqlSessionFactory.openSession();

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

//    List<User> users = userMapper.selectUsers();
//
//    System.out.println(users);

    User user = userMapper.selectUser(1);
    System.out.println(user);
  }

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>
    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    <property name="username" value="root"/>
    <property name="password" value="1226abcd"/>
  </properties>

  <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="mappers/userMapper.xml"/>
  </mappers>
</configuration>

userMapper.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="org.apache.ibatis.myTest.mapper.UserMapper">

  <select id="selectUsers" resultType="org.apache.ibatis.myTest.entity.User">
    select * from user
  </select>


  <select id="selectUser" resultType="org.apache.ibatis.myTest.entity.User">
    select * from user
    <where>
      <if test="id != null ">
        and id = #{id}
      </if>
    </where>
  </select>
</mapper>

UserMapper.java

public interface UserMapper {
  List<User> selectUsers();

  User selectUser(@Param(value = "id") Integer id);
}

User.java

public class User {
  private Integer id;

  private String name;

  private Integer age;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  @Override
  public String toString() {
    return "User{" +
      "id=" + id +
      ", name='" + name + '\'' +
      ", age=" + age +
      '}';
  }
}

标签:name,age,id,String,源码,mybatis,Integer,public,搭建
From: https://www.cnblogs.com/shigongp/p/16929069.html

相关文章

  • Mybatis基础
    1.Mybatis概述1.1Mybatis概念MyBatis是一款优秀的持久层框架,用于简化JDBC开发MyBatis本是Apache的一个开源项目iBatis,2010年这个项目由apachesoftware......
  • LevelDB源码剖析(1) Arena内存管理
    1.背景对于数据库来说,内存的分配非常重要,当我们使用C++默认的内存分配方式malloc/free或者new/delete的时候,如果遇到很小的键值对时,每次调用的平均开销就会比较大,同时会......
  • LevelDB源码剖析(2) 编码与字符串
    1.背景编码指的是内存里的整数和字符串放到磁盘上的方式,其主要目的有两个对不定长整数以及字符串能够在读取的时候感知到已经读取完了整个值最大程度的节省在磁盘上占......
  • 不背锅运维:搭不起来我赔钱给你:分享Ubuntu20和Centos7中使用kubeadm搭建k8s集群。
    一、Ubunt环境1.测试环境机器规划角色主机名IP地址mastertest-b-k8s-master192.168.11.13nodetest-b-k8s-node01192.168.11.14nodetest-b-k8s-nod......
  • mybatis
    一、创建mybatis配置文件和mapper映射文件的模板mabatis模板<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig......
  • easylogging++的那些事(四)源码分析(二)日志记录宏(一)CLOG宏(五)其他相关类
    目录el::base::Writer类el::base::NullWriter类el::LogMessage类el::Logger类isValidId接口flush接口initUnflushedCount接口el::base::MessageBuilder类成员变量成员函数......
  • Mybatis笔记
    由于Mybatis是面向接口进行编程的,需要注意以下两点:​1、映射文件中的namespace标签中的值需要和mapper中的全类名相同​2、映射文件中的id属性值,需要和mapper接口中的......
  • mybatisPlus笔记
    MybatisPlus:一些容易不知道是什么含义的方法:MybaitsPlus中我们没有进行设置表名,MybaitsPlus是如何进行确定表名的?​MybatisPlus中默认是通过实体类(Ma......
  • Failed to load ApplicationContext-myBatis注解与整合
    严重:CaughtexceptionwhileallowingTestExecutionListener[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@21213b92]topre......
  • 谷粒商城 第二章 开发环境搭建
    1、安装linux虚拟机1下载&安装VirtualBoxhttps://www.virtualbox.org/,要开启CPU虚拟化1下载&安装Vagrant2https://app.vagrantup.com/boxes/searchVag......