1.Mybatis基础配置

<?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>
<!--别名-->
<!-- 使用别名代替全限定名 -->
<typeAliases>
<typeAlias type="model.User" alias="user"/>
</typeAliases>
<!-- 数据库环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!--在xml中 &=&-->
<property name="url" value="jdbc:mysql://localhost:3306/office?useSSL=false&serverTimezone=Hongkong"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<!-- 映射文件存放的位置 -->
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>

2.得到SqlSessionFactory

import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SqlSessionFactorytest {
//定义参数
public static SqlSessionFactory sqlSessionFactory=null;
//定义配置文件
public static String resource="mybatis-config.xml";
public static InputStream inputStream;
//构建SesseionFactory
public static SqlSessionFactory getSqlSessionFactoryBuider() {
try {
//读取配置文件
inputStream=Resources.getResourceAsStream(resource);
System.out.println("配置文件加载成功!!");
//SqlSessionFactoryBuilder通过输入流的相关配置得到sqlSessionFactory
}catch(IOException e) {
e.printStackTrace();
}
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
System.out.println("sqlSessionFactory创造成功!!");
return sqlSessionFactory; }
}

3.数据层开发标准

public interface UserDao {
public List<User> getAllUser();//得到所有的用户
}

4.用XML实现映射器

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.UserDao">
<select id="getAllUser" resultType="User">
select * from user
</select>
</mapper>

5.得到SqlSession

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import dao.UserDao;
import model.User;
import utils.SqlSessionFactorytest; public class test {
public static void main(String[] args) {
SqlSessionFactory sqlSessionFactory=SqlSessionFactorytest.getSqlSessionFactoryBuider();
SqlSession sqlSession=sqlSessionFactory.openSession();
System.out.println("SqlSession创造成功!!");
//用SqlSession得到Mapper接口
UserDao userDao=sqlSession.getMapper(UserDao.class);
List<User> user=userDao.getAllUser();
if(user!=null) {
System.out.println("数据读取成功!!");
}
for(int x=0;x<user.size();x++) {
User u=user.get(x);
System.out.println(u.getId());
System.out.println(u.getUsername());
System.out.println(u.getPassword());
}
}
}

Mybatis读取数据实战的更多相关文章

  1. 自动化测试-19.selenium定位之通过JS修改html写入日期数据以及从文本读取数据实战

    # -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.support.select import ...

  2. springBoot第二种配置文件yaml书写方式及读取数据、整合myBatis和整合junit

    一.yaml文件格式:key-value形式:可以表示对象 集合 1.语法:key:value 冒号后面必须跟一个空格再写value值 key1: key2: key3:value 2.属性取值:a. ...

  3. mybatis完美的实战教程

    文件夹(? )[-] (读者注:事实上这个应该叫做非常基础的入门一下下,假设你看过Hibernate了那这个就非常的简单) 文章来源:http://blog.csdn.net/techbirds_ba ...

  4. 《OD大数据实战》驴妈妈旅游网大型离线数据电商分析平台

    一.环境搭建 1. <OD大数据实战>Hadoop伪分布式环境搭建 2. <OD大数据实战>Hive环境搭建 3. <OD大数据实战>Sqoop入门实例 4. &l ...

  5. SparkSQL大数据实战:揭开Join的神秘面纱

    本文来自 网易云社区 . Join操作是数据库和大数据计算中的高级特性,大多数场景都需要进行复杂的Join操作,本文从原理层面介绍了SparkSQL支持的常见Join算法及其适用场景. Join背景介 ...

  6. 一个I/O线程可以并发处理N个客户端连接和读写操作 I/O复用模型 基于Buf操作NIO可以读取任意位置的数据 Channel中读取数据到Buffer中或将数据 Buffer 中写入到 Channel 事件驱动消息通知观察者模式

    Tomcat那些事儿 https://mp.weixin.qq.com/s?__biz=MzI3MTEwODc5Ng==&mid=2650860016&idx=2&sn=549 ...

  7. MyBatis插入数据之后返回插入记录的id

    MyBatis插入数据的时候,返回该记录的id<insert id="insert" keyProperty="id" useGeneratedKeys= ...

  8. 归纳从文件中读取数据的六种方法-JAVA IO基础总结第2篇

    在上一篇文章中,我为大家介绍了<5种创建文件并写入文件数据的方法>,本节我们为大家来介绍6种从文件中读取数据的方法. 另外为了方便大家理解,我为这一篇文章录制了对应的视频:总结java从文 ...

  9. # spring boot + mybatis 读取数据库

    spring boot + mybatis 读取数据库 创建数据库 use testdb; drop table if exists t_city; create table t_city( id i ...

随机推荐

  1. Spark面试题(五)——数据倾斜调优

    1.数据倾斜 数据倾斜指的是,并行处理的数据集中,某一部分(如Spark或Kafka的一个Partition)的数据显著多于其它部分,从而使得该部分的处理速度成为整个数据集处理的瓶颈. 数据倾斜俩大直 ...

  2. Databend 社区积极拥抱 Rust 生态(202111回顾)

    Databend 旨在成为一个 开源.弹性.可靠 的无服务器数仓,查询快如闪电,与 弹性.简单.低成本 的云服务有机结合.数据云的构建,从未如此简单! Databend 对 Rust 社区的意义 Da ...

  3. Python如何格式化输出

    目录 Python中的格式化输出 1.旧格式化 2.新格式format( ) 函数 Python中的格式化输出 格式化输出就是将字符串中的某些内容替换掉再输出就是格式化输出 旧格式化输出常用的有%d( ...

  4. 【Microsoft Azure 的1024种玩法】五、基于Azure Cloud Shell 一站式创建Linux VM

    [文章简介] Azure Cloud Shell 是一个用于管理 Azure 资源的.可通过浏览器访问的交互式经验证 shell. 它使用户能够灵活选择最适合自己工作方式的 shell 体验,无论是 ...

  5. [gym102832J]Abstract Painting

    考虑每一个圆即对应于区间$[x_{i}-r_{i},x_{i}+r_{i}]$,可以看作对于每一个区间,要求所有右端点严格比其小的区间不严格包含左端点 用$f_{i}$表示仅考虑右端点不超过$i$的区 ...

  6. springboot和springcloud版本上的选择

    现在的springboot项目和cloud版本都是更新很快,但我们开发不是版本越新越好,我们要把版本对应起来,那么我们怎么去关联呢? springboot和springcloud不是越新越好,clou ...

  7. AI剪辑和自定义UI,打造更智能的剪辑体验

    为满足开发者构建高效的应用内视频编辑能力,7月的HMS Core 6.0 推出了视频编辑服务(Video Editor Kit),一站式的视频处理能力获得了积极反响.同时,我们也关注到开发者需要集成丰 ...

  8. Codeforces 1461F - Mathematical Expression(分类讨论+找性质+dp)

    现场 1 小时 44 分钟过掉此题,祭之 大力分类讨论. 如果 \(|s|=1\),那么显然所有位置都只能填上这个字符,因为你只能这么填. scanf("%d",&n);m ...

  9. 对 SAM 和 PAM 的一点理解

    感觉自己学 SAM 的时候总有一种似懂非懂.云里雾里.囫囵吞枣.不求甚解的感觉,是时候来加深一下对确定性有限状态自动机的理解了. 从 SAM 的定义上理解:SAM 可以看作一种加强版的 Trie,它可 ...

  10. AnnotationHub, clusterProfiler 进行GO,KEGG注释

    ️ AnnotationHub 目前最新的工具包叫做AnnotationHub,顾名思义,就是注释信息的中装站.通过它,能找到了几乎所有的注释资源.如果没有,你还可以根据已有的数据用它提供的函数进行构 ...