编程语言
首页 > 编程语言> > MyBatis01:第一个程序

MyBatis01:第一个程序

作者:互联网

MyBatis简介

什么是MyBatis

如何获取MyBatis?

持久化

持久化是将程序数据在持久状态和瞬时状态间转换的机制。

为什么需要持久化服务呢?那是由于内存本身的缺陷引起的

持久层

什么是持久层?

为什么需要MyBatis

MyBatis第一个程序

思路流程:搭建环境---》导入MyBatis---》编写代码---》测试

代码演示MyBatis-01

  1. 搭建实验数据库

    CREATE DATABASE mybatis;
    
    USE mybatis;
    
    DROP TABLE IF EXISTS user;
    
    CREATE TABLE user(
    	id int(20) NOT NULL,
        name VARCHAR(30) DEFAULT NULL,
        pwd VARCHAR(30) DEFAULT NULL,
    	PRIMARY KEY(id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    insert  into user(id,name,pwd) values 
    (1,'张三','123456'),(2,'李四','abcdef'),(3,'王五','987654');
    
  2. 导入MyBatis相关jar包

    		<!--mysql连接驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.19</version>
            </dependency>
            <!--mybatis-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.5</version>
            </dependency>
            <!--junit-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13</version>
                <scope>test</scope>
            </dependency>
    
  3. 编写MyBatis核心配置文件

    • 查看帮助文档
    <?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.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                    <property name="username" value="root"/>
                    <property name="password" value="12345678"/>
                </dataSource>
            </environment>
        </environments>
    <!--每一个mapper.xml都需要在mybatis-config核心配置文件中注册!-->
        <mappers>
            <package name="com.sofwin.mapper"/>
        </mappers>
    </configuration>
    
  4. 编写MyBatist工具类

    • 查看帮助文档
    package com.sofwin.util;
    
    
    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;
    
    public class MybatisUtils {
    
        private static SqlSessionFactory sqlSessionFactory;
    
        static {
            try {
                InputStream inputStream = Resources.getResourceAsStream(resource);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        //获取SqlSession连接
        public static SqlSession getSqlSession(){
            return sqlSessionFactory.openSession();
        }
    }
    
    
  5. 创建实体类

    package com.sofwin.pojo;
    
    public class User {
       
       private int id;  //id
       private String name;   //姓名
       private String pwd;   //密码
       
       //构造,有参,无参
       //set/get
       //toString()
       
    }
    
  6. 编写Mapper接口类

    package com.sofwin.mapper;
    
    import com.sofwin.pojo.User;
    import java.util.List;
    
    public interface UserMapper {
        //查询所有用户
        List<User> getUserList();
    }
    
    
  7. 编写Mapper.xml配置文件

    • namespace非常重要,不能写错!
    <?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=绑定一个对应的dao/mapper接口-->
    <mapper namespace="com.sofwin.mapper.UserMapper">
        <!--查询所有用户-->
        <select id="getUserList" resultType="com.sofwin.pojo.User">
            select * from user;
        </select>
    </mapper>
    
  8. 编写测试类

    • JUnit包测试
    public class UserMapperTest {
        @Test
        public void test(){
    
            //第一步:获取SQLSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            //执行SQL
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> userList = mapper.getUserList();
    
            for (User user: userList) {
                System.out.println(user);
            }
    
            sqlSession.close();
        }
        
    }
    
    
  9. 运行测试,成功地查询出来我们的数据,OK!

问题说明

  1. Maven静态资源过滤问题

    在build标签下加入:

    <resources>
       <resource>
           <directory>src/main/java</directory>
           <includes>
               <include>**/*.properties</include>
               <include>**/*.xml</include>
           </includes>
           <filtering>false</filtering>
       </resource>
       <resource>
           <directory>src/main/resources</directory>
           <includes>
               <include>**/*.properties</include>
               <include>**/*.xml</include>
           </includes>
           <filtering>false</filtering>
       </resource>
    </resources>
    

标签:持久,第一个,程序,mybatis,内存,org,MyBatis,import,MyBatis01
来源: https://www.cnblogs.com/xxyang/p/13758150.html