Mybatis------Mybatis basic operation

Getting Started_MyBatis Chinese Website

The first step is to write dependencies and import jar packages. provide technical support.

Create a module, import coordinates, and add dependent coordinates to the pom.xml configuration file in the created module

<dependencies>
        <!--mybatis 依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>

        <!--mysql 驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>

        <!--junit 单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>

        <!-- 添加slf4j日志api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.20</version>
        </dependency>
        <!-- 添加logback-classic依赖 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- 添加logback-core依赖 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>

The second part writes the configuration file of Mybatis

Under the resource folder, create a new mybatis-config.xml file

Then write the xml file, first of all, let's write the constraints

<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

 Then write that in the configuration.

<?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="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

Take a look at the official documentation:

  <environments default="development">        //配置数据源,里面可以有很多environment
    <environment id="development">            //其中一个environment
      <transactionManager type="JDBC"/>       //
      <dataSource type="POOLED">              //      
        //数据库信息 <!--数据库连接信息-->
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
<!--    <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
        <property name="username" value="root"/>
        <property name="password" value="1234"/>        -->
      </dataSource>
    </environment>
  </environments>
    <mappers>
       <!--加载sql映射文件-->
       <mapper resource="UserMapper.xml"/>
    </mappers>

What is it here? This is the third step, create the sql file mapper under the resource folder, and write the sql file specially. For high cohesion, low coupling

The filename must match the filename here! ! ! ! ! ! !

    <mappers>
       <!--加载sql映射文件-->
       <mapper resource="Mapper/UserMapper.xml"/>
    </mappers>

Then we write the xml file

The first is to add constraints:

<?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="test">
    <select id="selectAll" resultType="com.itheima.User">
        select * from tb_user;
    </select>
</mapper>

id is a name for this query, and resultType is the returned result, which should be assigned to the entity class.

resultType="com.iheima.User"> So here we need to create an entity class

 The private fields here should be the same as yours in the database.

com.itheima Write the MybatisDemo test class under the  package 

public class MyBatisDemo {

    public static void main(String[] args) throws IOException {
        //1. 加载mybatis的核心配置文件
        String resource = "mybatis-config.xml";
        //读取配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //通过配置文件生产sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象,用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        List<User> users = sqlSession.selectList("test.selectAll"); //参数是一个字符串,该字符串必须是映射配置文件的namespace.id
        System.out.println(users);
        //4. 释放资源
        sqlSession.close();
    }
}

Finally, add this sentence in the configuration

    <!-- 扫描实体类-->
    <typeAliases>
        <package name="com.itheima.Domain">
    </typeAliases>

Guess you like

Origin blog.csdn.net/makabaka12138/article/details/126178236
Recommended