什么是 MyBatis?
- MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。
- MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
- MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
- MyBatis本是apache的一个开源项目iBatis,2010年这个项目由apache software foundation迁移到了[google code](https://baike.baidu.com/item/google code/2346604),并且改名为MyBatis。2013年11月迁移到Github。
持久层
- 持久化就是将程序的数据在持久状态转化的过程
- 内存,断电即逝
为什么需要mybatis
- 帮助程序员将数据存到数据库中。
- 方便
- 传统的jdbc代码太复杂了,简化,框架,自动化
- 简单易学:本身就很小且简单。没有任何第三方依赖,最简单安装只要两个jar文件+配置几个sql映射文件。
- 灵活:mybatis不会对应用程序或者数据库的现有设计强加任何影响。 sql写在xml里,便于统一管理和优化。通过sql语句可以满足操作数据库的所有需求。
- 解除sql与程序代码的耦合:通过提供DAO层,将业务逻辑和数据访问逻辑分离,使系统的设计更清晰。sql和代码的分离,提高了可维护性。
- 提供映射标签,支持对象与数据库的orm字段关系映射。
- 提供对象关系映射标签,支持对象关系组建维护。
- 提供xml标签,支持编写动态sql。
一个mybatis程序
1.创建一个数据库
2.在maven项目的pom.xml文件中导入依赖文件-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.29</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.13.1</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.5.9</version>
- </dependency>
复制代码 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">
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers><transactionManager type="JDBC"/>
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers><dataSource type="POOLED">
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers> <property name="driver" value="com.mysql.jdbc.Driver"/>
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers> <property name="url" value="jdbc:mysql://localhost:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=utf-8"/>
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers> <property name="username" value="root"/>
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers> <property name="password" value="123123"/>
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers></dataSource>
- </environment>
- </environments>
-
- </configuration>
复制代码 4.编写操作数据库要用的SQLSession对象并且封装成一个工具类- 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 panglili
- * @create 2022-07-21-14:49
- */
- public class MybatisUtil {
- static SqlSessionFactory build;
- static {
- String resource="mybatis-config.xml";
- InputStream inputStream = null;
- try {
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers>inputStream = Resources.getResourceAsStream(resource);
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers>build = new SqlSessionFactoryBuilder().build(inputStream);
- } catch (IOException e) {
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers>e.printStackTrace();
- }
- }
- //通过sqlSessionFactoryBuild创建拿到了工厂,由这个工厂就可以拿到sqlSession了
- //sqlSession中封装了所有操作数据库的代码
- public static SqlSession getSqlSession(){
- SqlSession sqlSession = build.openSession();
- return sqlSession;
- }
- }
复制代码 5.与数据库对应的实体类- package pojo;
- import java.util.Date;
- /**
- * @author panglili
- * @create 2022-07-21-15:01
- */
- public class perple {
- private int id;
- private String name;
- private int age;
- private Date birth;
- 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 int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Date getBirth() {
- return birth;
- }
- public void setBirth(Date birth) {
- this.birth = birth;
- }
- @Override
- public String toString() {
- return "perple{" +
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers> "id=" + id +
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers> ", name='" + name + '\'' +
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers> ", age=" + age +
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers> ", birth=" + birth +
- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers> '}';
- }
- }
复制代码 6.接口与接口的配置操作数据库文件,尽量命名为相同- package mapper;
- import pojo.perple;
- import java.util.List;
- /**
- * @author panglili
- * @create 2022-07-21-15:07
- */
- public interface perpleMapper {
- //查询所有信息
- List<perple> perples();
- //通过id查询某个用户
- perple queryById();
- }
复制代码- <?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="mapper.perpleMapper">
- <select id="perples" resultType="pojo.perples">
- select * from perple;
- </select>
- </mapper>
复制代码 7.注册到mybatis配置文件- <mappers>
- <mapper resource="mapper/perpleMapper.xml"></mapper>
- </mappers>
复制代码 8.测试
[code]package mapper;import org.apache.ibatis.session.SqlSession;import pojo.perple;import util.MybatisUtil;import java.util.List;/** * @author panglili * @create 2022-07-21-15:16 */public class perpleTest { public void test(){ SqlSession sqlSession = MybatisUtil.getSqlSession(); perpleMapper mapper = sqlSession.getMapper(perpleMapper.class); List perples = mapper.(); for(int i=0;i |