MyBatis学习笔记

1 最佳范围

  • SqlSessionFactoryBuilder最佳范围是方法范围。
  • SqlSessionFactory最佳范围是应用范围。
  • SqlSession最佳范围是请求范围或方法范围,SqlSession的实例不能被共享,也是线程不安全的。

2 属性加载顺序

properties元素体内指定的元素–>类路径资源或properties元素的url属性中加载的属性–>作为方法参数传递的属性
(后读取的属性,会覆盖任一已经存在的完全一样的属性)

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">
<mapper namespace="club.chuxing.tech.learn.mybatis.StudentMapper">
    <select id="selectStudent" parameterType="int" resultType="club.chuxing.tech.learn.mybatis.Student">
        select * from student where id = #{id}
    </select>
</mapper>

接口文件

package club.chuxing.tech.learn.mybatis;

import org.apache.ibatis.annotations.Select;
public interface StudentMapper {
    @Select("select * from student where id = #{id}")
    Student selectStudent(int id);
}

3.1 使用方法的区别:

package club.chuxing.tech.learn.mybatis;

import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Main {
    public static void main(String[] args) {
        SqlSession session = null;
        try {
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();
            // 1st
            Student student = session.selectOne("club.chuxing.tech.learn.mybatis.StudentMapper.selectStudent", 8);

            // 2nd
            // 若mybatis-config.xml未配置mapper
            // sqlSessionFactory.getConfiguration().addMapper(StudentMapper2.class);  
            // StudentMapper2 mapper = session.getMapper(StudentMapper2.class);
            // Student student = mapper.selectStudent(8);

            System.out.println(student.getName() + ", " + student.getAge());
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            session.close();
        }
    }
}

3.2 Mappers配置的区别

使用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>
    <!-- Continue going here -->
    <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/test" />
                <property name="username" value="root" />
                <property name="password" value="meituan" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="StudentMapper.xml" />
    </mappers>
</configuration>

使用接口文件的方式:

<mappers>
    <mapper class=“club.chuxing.tech.learn.mybatis.StudentMapper” />
</mappers>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值