其他分享
首页 > 其他分享> > mybatis入门环境搭建

mybatis入门环境搭建

作者:互联网

开发环境

1 简介

1.1、什么是myBatis

如何获取MyBatis?

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

 

1.2、持久化

数据持久化:

1.3、持久层

DAO层 、Service层、Controller层

1.4、为什么要使用myBatista

创建数据库

create database `mybatis`;

use `mybatis`;

create data `user`(
`id` int(20) not null primary key,
   `name` varchar(30) not null,
   `pwd` varchar(30) not null
)engine=innodb default charset=utf8;

insert into `user`(`id`,`name`,`pwd`) value (1,'test01','123456');

2、搭建第一个myBatista程序

  1. 创建一个普通的maven项目

  2. 删除src目录,作为一个父工程使用

一、 导入依赖

<!--导入依赖-->
   <dependencies>
       <!--mysql-->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.49</version>
       </dependency>
       <!--mybatis-->
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis</artifactId>
           <version>3.5.4</version>
       </dependency>

       <!--junit-->
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.13</version>
           <scope>test</scope>
       </dependency>
   </dependencies>
<!--由于maven项目约定大于配置,idea不会将java源目录中的资源文件加载到项目中。-->
    <build>
           <resources>
               <resource>
                   <directory>src/main/resources</directory>
                   <includes>
                       <include>**/*.properties</include>
                       <include>**/*.xml</include>
                       <include>**/*.*</include>
                   </includes>
                   <filtering>true</filtering>
               </resource>
               <resource>
                   <directory>src/main/java</directory>
                   <includes>
                       <include>**/*.properties</include>
                       <include>**/*.xml</include>
                       <include>**/*.*</include>
                   </includes>
                   <filtering>true</filtering>
               </resource>
           </resources>
       </build>

 

二、 创建一个新模块

1. 编写mybatis的核心配置文件 mybatis-config.xml

 ```xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="com/zsq/mapper/UserMapper.xml" />
    </mappers>

</configuration>
```

2. 编写myBatis工具MybatisUntil.class

    ```java
  package com.zsq.untils;
   
  import org.apache.ibatis.session.SqlSession;
  import org.apache.ibatis.session.SqlSessionFactory;
  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
   
  import javax.annotation.Resources;
  import java.io.InputStream;
   
  /**
    * 工具类
    */
  public class MybatisUntil {
   
      // 从 SqlSessionFactory 中获取 SqlSession
      private static SqlSessionFactory sqlSessionFactory;
   
      static {
          // 读取配置
          String resource = "/mybatis-config.xml";
          InputStream inputStream = Resources.class.getResourceAsStream(resource);
          sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      }
      // 获取session
      public static SqlSession getSqlSession(){
          return sqlSessionFactory.openSession();
      }
  }
  ```

​ ​ #### 3. 编写实体类 ​ ```java package com.zsq.entity;

    public class User {
   
      private int id;
   
      private String name;
   
      private String pws;
   
      public User() {
      }
   
      public User(int id, String name, String pws) {
          this.id = id;
          this.name = name;
          this.pws = pws;
      }
   
      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 getPws() {
          return pws;
      }
   
      public void setPws(String pws) {
          this.pws = pws;
      }
   
      @Override
      public String toString() {
          return "User{" +
                  "id=" + id +
                  ", name='" + name + '\'' +
                  ", pws='" + pws + '\'' +
                  '}';
      }
  }
   
  ```

4. 定义mapper接口

```java package com.zsq.mapper;

    import com.zsq.entity.User;
   
  import java.util.List;
   
  public interface UserMapper {
      List<User> getAllUser();
   
      User getOneUser();
  }
   
  ```

 

5. 接口实现类

xml <?xml version="1.0" encoding="UTF8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zsq.mapper.UserMapper"> <select id="getAllUser" resultType="com.zsq.entity.User"> select * from `user` </select> </mapper>

6. 核心配置里添加接口绑定

xml <mappers> <mapper resource="com/zsq/mapper/UserMapper.xml" /> </mappers>

7. 测试

    ```java
  import com.zsq.entity.User;
  import com.zsq.mapper.UserMapper;
  import com.zsq.untils.MybatisUntil;
  import org.apache.ibatis.session.SqlSession;
  import org.junit.Test;
   
  import java.util.List;
   
  public class Test01 {
   
      @Test
      public void selectUserListTest(){
          SqlSession sqlSession = MybatisUntil.getSqlSession();
          UserMapper mapper = sqlSession.getMapper(UserMapper.class);
          List<User> allUser = mapper.getAllUser();
          for (User user : allUser) {
              System.out.println(user.toString());
          }
          sqlSession.close();
      }
  }
   
  ```

​ ​

 

标签:入门,id,org,mybatis,import,搭建,public,name
来源: https://www.cnblogs.com/zsqdtz/p/16684938.html