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> </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>