概述
1.简介
1.1 什么是MyBatis?
持久层框架。支持定制化SQL、存储过程以及高级映射。避免了几乎所有JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用XML或注解来配置和映射原生类型、接口和Java的POJO为数据库中的记录。
1.2 获取MyBatis
Maven仓库:
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
Github:https://github.com/mybatis/mybatis-3/releases/tag/mybatis-3.5.2
中文文档:https://mybatis.org/mybatis-3/zh/index.html
1.3 持久化、持久层
-
持久化就是将数据在持久状态和瞬时状态转化的过程。
-
数据库(jdbc),io文件持久化
-
为什么需要持久化?-有些对象,不能让他丢掉。
-
持久层:完成持久化工作的代码块(Dao层)
-
MyBatis就是为了简化传统JDBC代码,自动化,帮助程序员将数据存入数据库中。
-
MyBatis优点:
- 提供映射标签,支持对象与数据库的orm字段关系映射
- 提供对象关系映射标签,支持对象关系组建维护
- 提供xml标签,支持编写动态sql。
2、第一个Mybatis程序
2.1 搭建环境
1.数据库
create database MyBatiss;
use MyBatiss;
create table user (
id int(20) not null primary key,
name varchar(30) default null,
pwd varchar(30) default null
)engine=innodb default charset=utf8;
INSERT INTO user (id,name,pwd) VALUES
(1,'郑可可','123'),
(2,'甲可可','123'),
(3,'甲芋头','123')
2.新建maven项目
导入依赖:mybatis,mysql,Junit ;在build中配置resources
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2.2 创建一个模块
-
目录结构
-
编写mybatis的核心配置文件: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/MyBatiss?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册!--> <mappers> <mapper resource="dao/UserMapper.xml"/> </mappers> </configuration>
-
编写mybatis工具类:MybatisUtils
//sqlSessionFactory --> sqlSession public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { //获取sqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //获取SqlSession实例 //SqlSession完全包含了面向数据库执行SQL命令所需的所有方法 public static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); } }
2.3 编写代码
-
pojo
package pojo; 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; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + ''' + ", pwd='" + pwd + ''' + '}'; } }
-
Dao接口
package dao; public interface UserDao { List<User> getUserList(); }
-
接口实现类由原来的UserDaoImp.java转变为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"> <!--namespace绑定一个对应的Dao接口--> <mapper namespace="dao.UserDao"> <!--id对应绑定接口的方法,resultType对应返回类型中的泛型--> <select id="getUserList" resultType="pojo.User"> select * from mybatiss.user </select> </mapper>
2.4 测试
UserDaoTest
package dao;
public class UserDaoTest {
@Test
public void test() {
//获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//getMapper
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
}
2.5 常见问题
-
org.apache.ibatis.binding.BindingException: Type interface dao.UserDao is not known to the MapperRegistry.
原因:未在mybatis配置文件中注册用到的mapper.xml
-
java.lang.ExceptionInInitializerError
The error may exist in org/mybatis/example/UserMapper.xml
Caused by: java.io.IOException: Could not find resource org/mybatis/example/UserMapper.xml
原因1:src/java目录下的xml文件不会被编译进classes。在maven中用build配置resources,可以防止资源导出失败的问题
<!--在build中配置resources,来防止我们资源导出失败的问题--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
原因2:Mybatis配置文件中的mapper.xml路径出错,写的是java目录下的相对路径
<mappers> <mapper resource="dao/UserMapper.xml"/> </mappers>
最后
以上就是威武皮带为你收集整理的【Mybatis】(一)第一个mybatis程序-实现查功能的全部内容,希望文章能够帮你解决【Mybatis】(一)第一个mybatis程序-实现查功能所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复