美文网首页
Mybatis入门

Mybatis入门

作者: zzqsmile | 来源:发表于2021-10-14 11:07 被阅读0次

    1. 获取Mybatis

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.7</version>
    </dependency>
    

    2. 第一个Mybatis程序

    2.1 搭建数据库环境

    CREATE DATABASE `mybatis`;
    
    USE `mybatis`;
    
    CREATE TABLE `user`(
            `id` INT NOT NULL PRIMARY KEY,
            `name` VARCHAR(30) DEFAULT NULL,
            `pwd` VARCHAR(30) DEFAULT NULL
    )ENGINE=INNODB CHARSET=utf8;
    
    INSERT INTO `user`(`id`,`name`,`pwd`) VALUES
    (1,'zzq','123456'),
    (2,'zzq22','123456'),
    (3,'admin','123456');
    

    2.2 新建项目

    • 新建一个普通的maven项目
    • 删除src目录
    • 导入依赖
      pom.xml
    <!--导入依赖-->
        <dependencies>
            <!--mysql驱动-->
            <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.26</version>
            </dependency>
    
            <!--Mybatis-->
            <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.7</version>
            </dependency>
    
            <!--junit-->
            <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
            </dependency>
    
        </dependencies>
    
    • 创建一个模块
      • 编写Mybatis的核心配置文件mybatis-config.xml
        resources目录下新建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.cj.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false"/>
                    <property name="username" value="root"/>
                    <property name="password" value="root"/>
                </dataSource>
            </environment>
        </environments>
    </configuration>
    
    • 编写Mybatis的工具类
    package com.zzqsmile.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;
    
    //sqlSessionFactory  ---> sqlSession
    public class MybatisUtils {
        private static SqlSessionFactory sqlSessionFactory;
        static{
            try {
                //使用Mybatis第一步 获取sqlSessionFactory
                String resource = "mybatis-config.xml";
                InputStream inputStream = Resources.getResourceAsStream(resource);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        //既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
        //SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。
        public static SqlSession sqlSessionFactory(){
            return sqlSessionFactory.openSession();
        }
    }
    
    
    

    2.3 编写代码

    • 编写实体类
    package com.zzqsmile.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 com.zzqsmile.dao;
    
    import com.zzqsmile.pojo.User;
    
    import java.util.List;
    
    public interface UserDao {
        List<User> getUserList();
    }
    
    • 接口实现类由原来的UserDaoimpl转变为Mapper配置文件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">
    <mapper namespace="org.mybatis.example.BlogMapper">
        <select id="getUserList" resultType="com.zzqsmile.pojo.User">
            select * from Blog where id = #{id}
        </select>
    </mapper>
    

    2.4测试

    org.apache.ibatis.binding.BindingException: Type interface com.zzqsmile.dao.UserDao is not known to the MapperRegistry.
    mybatis-config.xml:

    <!--每一个Mapper.XML都需要在Mybatis核心配置文件中注册-->
    
        <mappers>
            <mapper resource="com/zzqsmile/dao/UserMapper.xml"/>
        </mappers>
    

    resource必须要是/

    相关文章

      网友评论

          本文标题:Mybatis入门

          本文链接:https://www.haomeiwen.com/subject/fgnsoltx.html