MyBatis详细使用步骤

MyBatis详细使用步骤

1、创建数据库和表,在表里添加

建库建表省略

2、创建模块,导入依赖

  • 官网下载mybatis。jar

  • 导入依赖

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>x.x.x</version>
    </dependency>
    

3、编写Mybatis核心配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 别名,使Mapper映射返回值类型可以变的简便-->
    <typeAliases>
        <package name="com.itheima.pojo"/>
       <!-- <typeAlias type="user" alias="com.itheima.pojo.User"/>-->
    </typeAliases>

    <!--
       environments:配置数据库连接环境信息,可以配置多个environment,通过对应的default属性切换不同的environment
   -->
    <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?useSSl=false"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>

        <!-- 加载SQL的映射文件-->
        <!--<mapper resource="com/itheima/mapper/UserMapper.xml"/>-->
        <package name="com.itheima.mapper"/>
    </mappers>
</configuration>

4、编写SQL映射文件

<?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 :名称空间 -->
<-- 作用于BrandMapper这个借口 --/>
<mapper namespace="com.itheima.mapper.BrandMapper">

    <select id="selectAll" resultType="com.itheima.pojo.Brand">
        select *
        from tb_brand;
    </select>

</mapper>

5、编码

1、定义pojo类

package com.itheima.pojo;

/**
 * 品牌
 *
 * alt + 鼠标左键:整列编辑
 *
 * 在实体类中,基本数据类型建议使用其对应的包装类型
 */

public class Brand {
    // id 主键
    private Integer id;
    // 品牌名称
    private String brandName;
    // 企业名称
    private String companyName;
    // 排序字段
    private Integer ordered;
    // 描述信息
    private String description;
    // 状态:0:禁用  1:启用
    private Integer status;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public Integer getOrdered() {
        return ordered;
    }

    public void setOrdered(Integer ordered) {
        this.ordered = ordered;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "id=" + id +
                ", brandName='" + brandName + '\'' +
                ", companyName='" + companyName + '\'' +
                ", ordered=" + ordered +
                ", description='" + description + '\'' +
                ", status=" + status +
                '}';
    }
}

2、正式测试

/**
 * @author 戒爱学Java
 * @date 2023/4/2 8:50
 */
public class MyBatisTest {

    @Test
    public void selectAllTest() throws IOException {
        
        //1、获取sqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2、获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3、获取Mapper接口代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //4、执行方法
        List<Brand> brands = brandMapper.selectAll();

        for (Brand brand : brands) {
            System.out.println(brand);
        }

        //5、关闭连接
        sqlSession.close();
    }
}

注意事项:
1、核心配置文件的名称myBatis-config.xml
2、映射文件的名称为XXXMapper.xml
3、核心配置文件分为两部分,数据库核心信息,映射信息
4、映射文件中的命名空间一般是dao接口的全限定接口名com.xszx.dao.UserDao,每一个sql语句对应的id必须是接口中的方法名

posted @ 2023-04-02 10:29  戒爱学Java  阅读(103)  评论(0编辑  收藏  举报