和我一起迎接明天的太阳吧

klaus08

焦虑源于行动的匮乏

MyBatis的使用

一、环境搭建

CREATE DATABASE mybatis;

USE mybatis;

CREATE TABLE USER(
id INT(20) NOT NULL PRIMARY KEY,
`name` VARCHAR(30) DEFAULT NULL,
pwd VARCHAR(30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO USER(id, NAME, pwd) VALUES
(1,'klaus', '123456'),
(2,'klaus01', '123457'),
(3,'klaus08', '123458');

SELECT * FROM USER;

二、创建一个Maven项目

创建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?userSSl=true&amp;userUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="com/klaus/dao/UserMapper.xml"/>
    </mappers>
    
</configuration>

创建MyBatis工具类

SessionFactoryBuilder一旦创建了SessionFactory就被丢弃;

创建的SessionFactory会在运行期间一直存在;

SqlSession是线程不安全的。

package com.klaus.utils;

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 {
        // 获取sqlSessionFactory对象
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }

}

编写代码

  1. 实体类

    package com.klaus.pojo;
    
    public class User {
        private int id;
        private String name;
        private String pwd;
    
        public User() {
        }
    
        public User(int id, String name, String pwd) {
            this.id = id;
            this.name = name;
            this.pwd = pwd;
        }
    
        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 getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    }
    
  2. Dao接口

    package com.klaus.dao;
    
    import com.klaus.pojo.User;
    
    import java.util.List;
    
    public interface UserDao {
        List<User> getUserList();
    }
    
  3. 接口实现类变成mapper.xml

    <?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">
    
    <!-- 绑定一个Dao\Mapper接口 -->
    <mapper namespace="com.klaus.dao.UserDao">
        <!-- 查询语句 -->
        <select id="getUserList" resultType="com.klaus.pojo.User">
            select * from mybatis.user;
        </select>
    </mapper>
    

测试

package com.klaua.dao;

import com.klaus.dao.UserDao;
import com.klaus.pojo.User;
import com.klaus.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserDaoTest {
    @Test
    public void test(){
        //获取sqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //指向SQL
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> userList = mapper.getUserList();
        for (User user :userList)
            System.out.println(user);

        sqlSession.close();
    }
}
posted @ 2021-07-25 10:48  klaus08  阅读(30)  评论(0编辑  收藏  举报