• Mybatis中开发Dao的方法


    • 方法一:我们只需要开发一个Dao接口,以及其实现类。

    (1) UserDao.java

    package test.dao;
    
    import test.entity.User;
    
    public interface UserDao {
        //根据用户信息查询用户信息
        public User findUserById(Integer id);
    }

    (2)  UserDaoImpl.java(UserDao接口的实现类)

           该实现类中需要注入SqlSessionFactory工厂,使用SqlSessionFactory生成SqlSession,如果使用Mybatis和Spring,直接让Spring容器管理SqlSessionFactory,SqlSessionFactory创建SqlSession的方法时线程安全的。

    package dao;
    
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    
    import entity.User;
    
    public class UserDaoImpl implements UserDao {
        
        private SqlSessionFactory sqlSessionFactory;
        
        //通过构造方法注入sqlSessionFactory
        public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
            this.sqlSessionFactory = sqlSessionFactory;
        }
    
        @Override
        public User findUserById(Integer id) {
            SqlSession sqlSession = sqlSessionFactory.openSession();
            String statement = "config.userMapper.getUser";
            User user = sqlSession.selectOne(statement, 1);
            return user;
        }
    
    }

    (3) SqlMapConfig.xml核心配置文件

    <?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:///mybatis" />
                    <property name="username" value="root" />
                    <property name="password" value="123" />
                </dataSource>
            </environment>
        </environments>
        
        <!-- 注册userMapper.xml文件  -->
        <mappers>
            <mapper resource="config/userMapper.xml" />
        </mappers>
    </configuration>

    (4) uerMapper.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">
    
    <!-- namespace:SQL映射文件所在的路路径 -->
    <mapper namespace="config.userMapper">
        <!-- parameterType:参数id的值的类型 resultType:查询的结果返回的类型 -->
        <select id="getUser" parameterType="Integer" resultType="entity.User">
            select * from users where id=#{id}
        </select>
    </mapper>

    (5) JUnit测试

    package test;
    
    import java.io.InputStream;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Before;
    import org.junit.Test;
    
    import dao.UserDao;
    import dao.UserDaoImpl;
    import entity.User;
    
    public class MyBatisTest {
        private SqlSessionFactory sqlSessionFactory;
        
        @Before
        public void Before() throws Exception {
            //注入SqlSessionFactory工厂
            String resource = "config/SqlMapConfig.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }
        
        @Test
        public void test() {
            UserDao userDao = new UserDaoImpl(sqlSessionFactory);
            User user = userDao.findUserById(1);
            System.out.println(user.getName());
        }
    }
    • 方法二:我们只需要编写mapper接口动态代理(就是Dao接口),mapper接口是Mybatis的官方叫法,而不需要写其实现类。

    (1) 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">
    
    <!-- namespace是UserMapper.java的全限定名 -->
    <mapper namespace="way_two.dao.UserMapper">
        <!-- parameterType:参数id的值的类型 resultType:查询的结果返回的类型 -->
        <select id="getUser" parameterType="Integer" resultType="entity.User">
            select * from users where id=#{id}
        </select>
        
        <select id="getAllUser" resultType="entity.User">
            select * from users;
        </select>
    </mapper>

    (2) UserMapper.java(相当于UserDao接口)

        注意:a. userMapper.xml的namespace是UserMapper.java的全限定名

             b. userMapper.xml中的statement的id值是UserMapper.java中的方法名,parameterTyep是方法的参数列表,resultType是方法的返回值。

             statement设置:

                  image

                  UserMapper接口中的方法:

                  image

    package way_two.dao;
    
    import java.util.List;
    
    import entity.User;
    
    public interface UserMapper {
        //根据用户信息查询用户信息
        public User getUser(Integer id);
        
        public List<User> getAllUser();
    }

    (3) JUint测试

    @Test
        public void getUserTest() {
            //得到SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //通过SqlSession得到UserMapper的动态代理对象
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            User user = userMapper.getUser(1);
            System.out.println(user.getName());
        }
        
        @Test
        public void getAllUserTest() {
            //得到SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //通过SqlSession得到UserMapper的动态代理对象
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<User> user = userMapper.getAllUser();
            System.out.println(user.size());
        }

    小结:通过上面的动态代理的例子,我们发现在userMapper.xml中定义的resultType的类型应是一条SQL语句的结果集映射的Java对象。是否返回一个List集合还是一个单独的Java对象,是有UserMapper接口中方法的返回值类型决定,当返回一个List集合时,代理对象内部调用

    sqlSession.selectList()方法获取集合。如果返回的是一个Java对象时,代理对象内部调用sqlSession.selectOne()方法。

  • 相关阅读:
    页面元素定位-CSS元素基本定位
    自动化测试理论知识以及相关框架
    flask自学
    关于测试报告的一些理解
    jenkins、k8s、docker学习
    常用装饰器
    正则
    pycharm设置展示运行结果行数以及python一些好用的模块
    python相关知识点3,线程锁、单例模式
    python相关知识点2(各种内置方法)
  • 原文地址:https://www.cnblogs.com/shi-blog/p/4315485.html
Copyright © 2020-2023  润新知