java mybatis 使用

1. 读取sql基础配置文件

2. 创建 SqlSessionFactory 工厂

3. 使用工厂类创建 SqlSesson 对象

4. 使用 sqlSession 对象创建 Dao 层的代理对象

5. 关闭资源

测试代码:

1. sql基础配置文件

<?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">
<!--mybatis 的主配置文件-->
<configuration>
    <!--设置配置-->
    <settings>
        <!--懒加载配置-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>

    </settings>
    
    <typeAliases>
        <package name="cn.monkey.domain"></package>
    </typeAliases>

    <environments default="mysql">
        <!--mysql配置-->
        <environment id="mysql">
            <!--配置事务类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置数据源(连接池)-->
            <dataSource type="POOLED">
                <!--配置连接数据库的4要素-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <!--<mapper resource="cn/monkey/dao/UserDao.xml"></mapper>-->
        <package name="cn.monkey.dao"></package>
    </mappers>

</configuration>

2. dao 层 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="cn.monkey.dao.UserDao">

    <resultMap id="userMap" type="User">
        <id property="id" column="id"></id>
        <result property="username" column="username"></result>
        <result property="birthday" column="birthday"></result>
        <result property="sex" column="sex"></result>
        <result property="address" column="address"></result>

        <!--一对多懒加载配置-->
        <collection property="accounts" ofType="Account" select="cn.monkey.dao.AccountDao.finAccountByUid" column="id"></collection>
        <!--<collection property="accounts" ofType="Account">-->
            <!--<id property="id" column="aid"></id>-->
            <!--<result property="uid" column="uid"></result>-->
            <!--<result property="money" column="money"></result>-->
        <!--</collection>-->
    </resultMap>

    <!--一对多实现懒加载-->
    <select id="findAll" resultMap="userMap">
        SELECT * FROM USER
    </select>

    <!--<select id="findAll" resultMap="userMap">-->
        <!--SELECT * FROM USER u LEFT OUTER JOIN account a ON u.id = a.UID;-->
    <!--</select>-->

    <select id="findById" parameterType="Integer" resultType="User">
        SELECT * from USER where id = #{id}
    </select>

</mapper>

3. 测试代码

package cn.monkey.test;

import cn.monkey.dao.AccountDao;
import cn.monkey.dao.UserDao;
import cn.monkey.domain.Account;
import cn.monkey.domain.AccountUser;
import cn.monkey.domain.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.After;
import org.junit.Before;
import org.junit.Test;

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

public class TestUser {
    private InputStream is;
    private SqlSessionFactory factory;
    private SqlSession session;
    private UserDao userDao;

    @Before
    public void init()  throws IOException {
        is = Resources.getResourceAsStream("sqlMapConfig.xml");
        // 2. 创建 SqlSessionFactory 工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        factory = builder.build(is);
        // 3. 使用工厂类创建 SqlSesson 对象
        session = factory.openSession();
        // 4. 使用 sqlSession 对象创建 Dao 层的代理对象
        userDao = session.getMapper(UserDao.class);
    }

    @After
    public void destroy() throws IOException {
        // 提交事务
        session.commit();
        // 6. 释放资源
        session.close();
        is.close();
    }

    /**
     * 测试一对多
     */
    @Test
    public void testFindAll () {
        List<User> users = userDao.findAll();
//        for (User user : users) {
//            System.out.println(user);
//        }
    }

}

 

posted @ 2020-05-26 22:05  monkey-K  阅读(196)  评论(0编辑  收藏  举报