Mybatis 学习记录
2024-10-07 02:57:20
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&useUnicode=true&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>
最新文章
- geotrellis使用(二十五)将Geotrellis移植到spark2.0
- 面向对象的JS(一)
- Spring Framework------>;version4.3.5.RELAESE----->;Reference Documentation学习心得----->;使用spring framework的IoC容器功能----->;方法一:使用XML文件定义beans之间的依赖注入关系
- 关于fast cgi和php-fpm的关系
- 第51课 C++对象模型分析(下)
- ###《Machine Learning in Action》 - KNN
- linux学习(二)-目录的操作命令
- js——DOM操作(一)
- mysql5.1 有什么新特性
- React实践
- 在Xbox和Hololens 上部署、调试UWP App
- 这些年常用的WEB开发工具和技术, 学会一半你找工作没问题
- C++中,用类和重载运算符写高精模板
- centos安装jenkins
- 道路运输车辆卫星定位系统JT/T808服务实现和压测
- vue从入门到进阶:组件Component详解(六)
- Django视图层、虚拟环境
- java八大数据类型
- iview表格高度自适应只需要三步即可
- MPD软件工作坊上海站本周末在上海举行
热门文章
- CF1036A Function Height 题解
- Shell之Sed常用用法
- 查找局域网中未知设备的IP
- 给初学者的STM32(Cortex-M3)中断原理及编程方法介绍 [原创www.cnblogs.com/helesheng]
- cmake之引入外部项目(引用其他项目)、FetchContent管理子模块(fetchcontent用法)
- 【LeetCode】1432. 改变一个整数能得到的最大差值 Max Difference You Can Get From Changing an Integer
- Cornfields(poj2019)
- ORA-14450: 试图访问已经在使用的事务处理临时表
- LeetCode—剑指 Offer学习计划
- 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