MyBatis的原始DAO开发!!!

2023-12-20 16:07:59

引用:MyBatis的删除、修改、插入操作!!!-CSDN博客的准备工作!!!(准备工作都一样只不过文件名称有所不同)

1.利用原始DAO开发,查询所有的信息。

UserDao:

package com.by.dao;

import com.by.pojo.User;

import java.util.List;

public interface UserDao {
    List<User> findAll();

}

UserMapper.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">
<mapper namespace="com.by.dao.UserDao">
    <select id="findAll" resultType="com.by.pojo.User">
        select  * from user
    </select>
</mapper>

与Mapper开发有所不同的是,原始Dao开发需要有接口的实现类:

UserDaoImpl:?接口的实现类的写法你需要注意调用的方法以及传递的参数。(参数为全类名+方法名

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package com.by.dao;

import com.by.pojo.User;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserDaoImpl implements UserDao{
    private SqlSession sqlSession;

    public UserDaoImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public List<User> findAll() {
        return sqlSession.selectList("com.by.dao.UserDao.findAll");
    }

}

测试类:

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package com.by;

import com.by.dao.UserDao;
import com.by.dao.UserDaoImpl;
import com.by.pojo.User;
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 org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * <p>Project: mybatis - MyBatisTest</p>
 * <p>Powered by scl On 2023-12-18 11:44:53</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
public class MyBatisTest {

    @Test
    public void testFindAll() throws IOException {
        // 加载配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 创建sqlSessionFActory
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获得数据的绘画实例
        SqlSession sqlSession = sessionFactory.openSession();

        UserDao userDao = new UserDaoImpl(sqlSession);
        List<User> userList = userDao.findAll();
        for (User str : userList) {
            System.out.println(str);
        }


        inputStream.close();
        sqlSession.close();
    }

}

2.根据id查询信息

UserDao:

package com.by.dao;

import com.by.pojo.User;
import java.util.List;

public interface UserDao {
   
    User getUserById(Integer id);
}

UserDao.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">
<mapper namespace="com.by.dao.UserDao">
    <select id="getUserById" resultType="com.by.pojo.User">
        select *
        from user where id=#{id};
    </select>
</mapper>

UserDaoImpl:?接口的实现类的写法你需要注意调用的方法以及传递的参数。(参数为全类名+方法名

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package com.by.dao;

import com.by.pojo.User;
import org.apache.ibatis.session.SqlSession;
import java.util.List;


public class UserDaoImpl implements UserDao{
    private SqlSession sqlSession;

    public UserDaoImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public User getUserById(Integer id) {
        return sqlSession.selectOne("com.by.dao.UserDao.getUserById",id);
    }
}

测试类:

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package com.by;

import com.by.dao.UserDao;
import com.by.dao.UserDaoImpl;
import com.by.pojo.User;
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 org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyBatisTest {

    @Test
    public void getUserById() throws IOException {
        // 加载配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        // 创建sqlSessionFActory
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获得数据的绘画实例
        SqlSession sqlSession = sessionFactory.openSession();

        UserDao userDao = new UserDaoImpl(sqlSession);
        User user = userDao.getUserById(42);
        System.out.println(user);


        inputStream.close();
        sqlSession.close();
    }

}

文章来源:https://blog.csdn.net/qq_64847107/article/details/135104606
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。