IDEA如何构建mybatis

任何一个软件都要和数据库关联,软件需要的数据都存储在数据库中。

对于经常使用的数据库相关的代码就出现了很多冗余的代码,持久层框架也随之出现。

目前使用比较流程的持久层框架有hibernate和mybatis等。这次介绍下如何搭建使用mybatis框架。

工具介绍

  • IDEA开发工具
  • maven开发工具
  • MariaDB数据库

首先创建Maven项目配置pom.xml添加Mybatis依赖包

作者在此使用的数据库是MariaDB,所有也需要添加依赖包

 mybatis数据库配置。对于mybatis框架来说,首先需要配置的就是数据库的数据源配置以及

采用何种开发模式的配置,对于mavne项目来说这些资源信息都需要配置在src/main/resources下面,

对于普通的java项目则只需要配置在src下即可。

mybatis-config.xml的配置。在src/main/resources/下创建一个名为mybatis-config的文件。

在文件中需要配置resources数据库资源文件,

数据库链接需要的driver驱动、url连接串、username数据库用户名、password数据库密码等。

 数据库信息的配置。根据mybatis-config配置中的resources文件引入,

需要创建一个jdbc.properties文件的配置。

同样是在src/main/resources资源文件夹下新建properties文件。

配置url/driver/username/password。

接下来就是实体类了

创建java对象。根据数据库表或者某些表的字段配置一个java对象。

mybatis可以根据框架将表中的数据自动转成对象。

创建实体类实现get、set方法

编写对应的接口需要操作的功能

在此演示一个Customer类

编写好实体类以及接口然后就配置对应的映射文件了

配置xml映射文件。在resources包下创建mapper文件夹然后创建与接口对象同名的.xml文件,配置java与数据库之间交互的方法。主要配置的有namespace、

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

<mapper namespace="com.nf147.dao.CustomerDAO">

    <select id="queryAll" resultType="Customer">
        select * from Customer
    </select>

    <select id="queryById" resultType="Customer">
        select * from Customer where CustomerId = #{CustomerId}
    </select>

</mapper>

配置好xml之后,需要在mybatis-config中将这个xml添加到mappers中,这样项目启动的时候才能加载到这些sql脚本。 

创建加载mybatis配置文件的类。可以通过mybatis框架自带的一些类加载xml配置文件,

根据factory获取一个session,通过session执行对应脚本的sql语句。

对于执行insert或者update语句需要在最后执行session.commit进行提交操作。

创建方法

public Object queryAll(){

        String resource = "/mybatis-config.xml";
        InputStream inputStream = Resources.class.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();
        Object o = sqlSession.selectList("com.nf147.dao.CustomerDAO.queryById",1);
        sqlSession.commit();
        sqlSession.close();
        return o;
    }

然后编写测试类

 

针对于一些结果集渲染方法

使用resultMap
 <!--映射 Invoice表与BillingInfo-->
    <resultMap id="invoiceInfo" type="Invoice" autoMapping="true">
        <association property="billingInfo" javaType="BillingInfo" autoMapping="true">
            <id property="BillingId" column="InvoiceId"></id>
        </association>
    </resultMap>
    <!--映射Customer-->
    <resultMap id="customer" type="Customer" autoMapping="true">
        <collection property="invoices" ofType="Invoice" resultMap="invoiceInfo"></collection>
    </resultMap>
<select id="getInvoiceInfo" resultMap="invoiceInfo">
        SELECT
          c.*,
          i.InvoiceId,
          i.InvoiceDate,
          i.BillingAddress,
          i.BillingCity,
          i.BillingState,
          i.BillingCountry,
          i.BillingPostalCode,
          i.Total
        FROM Customer c LEFT JOIN Invoice I ON c.CustomerId = I.CustomerId
        where i.InvoiceId = #{InvoiceId}
    </select>

    <select id="queryById" resultMap="customer">
          SELECT
          c.*,
          i.InvoiceId,
          i.InvoiceDate,
          i.BillingAddress,
          i.BillingCity,
          i.BillingState,
          i.BillingCountry,
          i.BillingPostalCode,
          i.Total
        FROM Customer c LEFT JOIN Invoice I ON c.CustomerId = I.CustomerId
        where C.CustomerId = #{CustomerId}
    </select>
原文地址:https://www.cnblogs.com/dzcici/p/9794600.html