1、先放上mybatis官网地址:

  https://mybatis.org/mybatis-3/zh/index.html

2、mybatis源码和有关包下载地址(GitHub):

  https://github.com/mybatis/mybatis-3

源码项目结构:

介绍:

  MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAOs)。
 

3、第一个mybatis项目的搭建

  创建普通Maven项目在pom.xml文件下进行相关依赖引入

1)Maven项目构建下通过pom.xml文件,Mybatis的引入

此处引入版本为3.5.3:
        <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>

  2)其他包的依赖

    <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>

  3)数据库配置文件的配置(官网即可找到)

  命名: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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--数据库连接驱动-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--数据库连接路径以及连接的有关条件,-->
<property name="url" value="jdbc:mysql://localhost:3306/填自己的数据库名?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
<!--数据库安全连接用户名-->
<property name="username" value="填自己的,用户名"/>
<!--数据库安全连接密码-->
<property name="password" value="填自己的,密码"/>
</dataSource>
</environment>
</environments>
  <mappers>
<!--填自己的xml配置文件路径-->
<mapper resource="com/demo/dao/UserMapper.xml"/>
</mappers> </configuration>

  4)代码编写:

  创建java包相关目录结构:

  • com.demo.dao  
  • com.demo.utils
  • com.demo.pojo

  utils目录下:

  文件名:MybatisUtils.java

package com.demo.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; /**
* @author June
* @date 2022/1/6 17:35
*/
//SqlSessionFactory-->SqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用mybatis第一步,获取SqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}

  pojo目录下:

  User.java

package com.demo.pojo;

/**
* @author June
* @date 2022/1/6 17:43
*/
public class User {
private int id;
private String name;
private String pwd; public User() {
} public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
}
}

  dao目录下:

  UserDao.java

package com.demo.dao;

import com.demo.pojo.User;

import java.util.List;

/**
* @author June
* @date 2022/1/6 17:44
*/
public interface Userdao {
List<User> getUser();
}

  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">
<!--命名空间,绑定一个Dao-->
<mapper namespace="com.demo.dao.Userdao">
<!--查询语句-->
<select id="getUser" resultType="com.demo.pojo.User">
select * from calss.qiye
</select>
</mapper>

最新文章

  1. geotrellis使用(二十五)将Geotrellis移植到spark2.0
  2. 面向对象的JS(一)
  3. Spring Framework------&gt;version4.3.5.RELAESE-----&gt;Reference Documentation学习心得-----&gt;使用spring framework的IoC容器功能-----&gt;方法一:使用XML文件定义beans之间的依赖注入关系
  4. 关于fast cgi和php-fpm的关系
  5. 第51课 C++对象模型分析(下)
  6. ###《Machine Learning in Action》 - KNN
  7. linux学习(二)-目录的操作命令
  8. js——DOM操作(一)
  9. mysql5.1 有什么新特性
  10. React实践
  11. 在Xbox和Hololens 上部署、调试UWP App
  12. 这些年常用的WEB开发工具和技术, 学会一半你找工作没问题
  13. C++中,用类和重载运算符写高精模板
  14. centos安装jenkins
  15. 道路运输车辆卫星定位系统JT/T808服务实现和压测
  16. vue从入门到进阶:组件Component详解(六)
  17. Django视图层、虚拟环境
  18. java八大数据类型
  19. iview表格高度自适应只需要三步即可
  20. MPD软件工作坊上海站本周末在上海举行

热门文章

  1. CF1036A Function Height 题解
  2. Shell之Sed常用用法
  3. 查找局域网中未知设备的IP
  4. 给初学者的STM32(Cortex-M3)中断原理及编程方法介绍 [原创www.cnblogs.com/helesheng]
  5. cmake之引入外部项目(引用其他项目)、FetchContent管理子模块(fetchcontent用法)
  6. 【LeetCode】1432. 改变一个整数能得到的最大差值 Max Difference You Can Get From Changing an Integer
  7. Cornfields(poj2019)
  8. ORA-14450: 试图访问已经在使用的事务处理临时表
  9. LeetCode—剑指 Offer学习计划
  10. ios离线打包报错Showing Recent Messages :-1: HBuilder has conflicting provisioning settings. HBuilder is automatically signed for development, but a conflicting code signing identity iPhone Distribution has