1.创建空的Java工程,安装MyBatis依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
<artifactId>mybatis03</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build> <dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

2.创建JavaBean类

在src/main/java下新建com.ttpfx.domain包,并创建User类:

package com.ttpfx.domain;

import java.util.Date;

public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}

3.创建Dao接口

在src/main/java新建com.ttpfx.dao包,并创建UserDao接口:

package com.ttpfx.dao;

import com.ttpfx.domain.User;

import java.util.List;

public interface UserDao {
List<User> getAll();
}

4.创建XML映射文件

在src/main/resources下创建com/ttpfx/dao/UserDao.xml

注意事项:

  1. xml文件的路径和名称与com.ttpfx.dao.UserDao接口对应
  2. mapper标签的namespace属性为UserDao的全限定类名
  3. select标签的id与UserDao接口的方法对应
  4. 指定select标签的resultType为User类的全限定类名
<?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.ttpfx.dao.UserDao">
<select id="getAll" resultType="com.ttpfx.domain.User">
select id, username, birthday, sex, address from user;
</select>
</mapper>

5.创建MyBatis主配置文件

在src/main/resources下创建SqlMapConfig.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://localhost:3306/t_mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/ttpfx/dao/UserDao.xml"/>
</mappers>
</configuration>

6.测试使用

在src/test/java新建com.ttpfx.test包,并创建MybatisTest类:

package com.ttpfx.test;

import com.ttpfx.dao.UserDao;
import com.ttpfx.domain.User;
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.InputStream;
import java.util.List; public class MybatisTest {
public static void main(String[] args) throws Exception {
// 1.读取配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
// 2.创建SqlSession工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = builder.build(in);
// 3.创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4.创建UserDao动态代理
UserDao userDao = sqlSession.getMapper(UserDao.class);
// 5.使用userDao
List<User> list = userDao.getAll();
list.forEach(System.out::println);
// 6.销毁资源
sqlSession.close();
in.close();
}
}

使用MyBatis的步骤的更多相关文章

  1. MyBatis --- 配置步骤

    本文并非具体的细节,而是主要的配置步骤 概述 MyBatis 是半自动的ORM 框架,在MyBatis 整合 Spring Boot 的时候步骤比较繁琐,所以写下此篇纪录一下步骤. 使用 MyBati ...

  2. MyBatis使用步骤及原理

    主要讲解MyBatis-基本使用步骤      回顾:     MyBatis是一个数据持久层(ORM)框架.把实体 类和SQL语句之间建立了映射关系,是一种半自 动化的ORM实现.MyBATIS需要 ...

  3. mybatis使用步骤

    1.创建config.xml文件.设置环境.数据源等: 2.设置mapper.xml文件.写sql:下面图中的resultType属性经常会替换为resultMap,不过需要加入<resultM ...

  4. [Java面试七]Mybatis总结以及在面试中的一些问题.

    1.JDBC编程有哪些不足之处,MyBatis是如何解决这些问题的? ① 数据库链接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题. 解决:在SqlMapConfig ...

  5. MyBatis之CRUD

    1 mybatis框架介绍 1.1回顾jdbc操作数据库的过程 1.2 mybatis开发步骤 A.提供一个SqlMapperConfig.xml(src目录下),该文件主要配置数据库连接,事务,二级 ...

  6. Mybatis部分

    Mybatis部分 1.JDBC编程有哪些不足之处,MyBatis是如何解决这些问题的? ① 数据库链接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题. 解决:在Sq ...

  7. 浅谈Mybatis(一)

    一.MyBatis引言 1.基本概念 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google co ...

  8. Mybatis面试题

    面试题示例 1.JDBC编程有哪些不足之处,MyBatis是如何解决这些问题的? 1)数据库链接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库链接池可解决此问题. 解决:在SqlMap ...

  9. 使用mybatis-generator工具自动生成mybatis代码

    使用mybatis-generator工具自动生成mybatis代码 步骤如下: 1.引入maven  依赖,在项目pom.xml文件中添加 <plugin> <groupId> ...

随机推荐

  1. 网站备案查询/ICP备案查询网

    网站备案查询/ICP备案查询网 互联网站备案信息全国公安机关互联网站安全服务平台http://www.beian.gov.cn/portal/index 1 http://www.miitbeian. ...

  2. npm 安装 electron 失败的解决方案

    npm 安装 electron 失败的解决方案 shit GFW npm 安装 electron 失败 解决方案 https://www.npmjs.com/package/nrm $ nrm ls ...

  3. I ❤️ W3C : Secure Contexts

    I ️ W3C : Secure Contexts Secure Contexts W3C Candidate Recommendation, 15 September 2016 https://ww ...

  4. 电信悦 me 智能网关

    电信悦 me 智能网关 悦 me 智能网关 Q1:什么是电信悦 me 智能网关? 悦me网关是智慧家庭的核心终端,作为"光猫+智能路由器"的集合体, 采用了全新的硬件.外观及智能操 ...

  5. js online playground & web editor

    js online playground & web editor -javascript playgrounds 2019 https://scotch.io/tutorials/7-jav ...

  6. webpack defineConstants

    webpack defineConstants PAGES 全局常量/全局变量 https://webpack.js.org/plugins/define-plugin/ taro https://n ...

  7. puppeteer render local HTML template bug

    puppeteer render local HTML template bug ➜ url-to-poster git:(master) ✗ dev ^-v-^ app is running in ...

  8. clisp的一些function

    ;; example: (write (A-Z :start (+ 65 1) :end 87)) (defmacro A-Z (&key (start 65) (end 90)) (let* ...

  9. element-ui的树型结构图,带有复选框的,没有子项的,横排展示

    // 修改树形图样式,如果不含有下箭头的块,要变成行内样式 treeChildInline(){ let hasCaretRight = $("#permission_panel" ...

  10. java线程池趣味事:这不是线程池

    要想写出高性能高并发的应用,自然有许多关键,如io,算法,异步,语言特性,操作系统特性,队列,内存,cpu,分布式,网络,数据结构,高性能组件. 胡说一通先. 回到主题,线程池.如果说多线程是提高系统 ...