Mybatis (1: Elementary)

MyBatis
is a semi-ORM database persistence framework, the bottom layer is still JDBC, so when using Mybatis alone, the transaction needs to be submitted manually

Historical expansion: It was originally an open source project iBatis of Apache, so many jars are named iBatis

ORM : Object Relational Mapping, a technology to solve the mismatch between object-oriented and relational databases;
there are many ways to manipulate database relations, and these two are commonly used:
1. Semi-mapping: Write SQL in the configuration file and complete the conversion between object entities and database relationships in different SQLs, such as Mybatis.
2. Complete mapping: directly use object entities and database relationships for mapping without writing SQL (simple operations). The framework is generated by itself, such as JPA, Hibenate
(PS: it means that you need to write sql is half-mapping, and what you don’t need is full mapping, but the sql for complete mapping is only simple, and you still have to write it yourself if the complexity is high)

================================================= ===========
1. The core configuration file: MyBatis-Config.xml, placed under the Source Folder (the files under this folder will be automatically read after the project is started)

<configuration>
    <!-- 引入配置文件信息,这里不能加classpath:。
		resource:引入类路径下的资源,即classpath,所以不需要写classpath:
		url:引入网络路径或磁盘路径下的资源 
	-->
	<properties resource="db.properties"></properties>
	<!-- 环境们 (很多环境的意思,就是可以配置很多个数据库连接)
		default:默认使用哪一个环境(必须对应一个环境的id)
	 -->
	<environments default="development">
		<!-- 一个环境  id:为这个环境取唯一一个id名称 -->
		<environment id="development">
			<!-- 事务管理   type:JDBC(支持事务)/MANAGED(什么都不做) -->
			<transactionManager type="JDBC" />
			<!-- 数据源, 连接池  type(POOLED):MyBatis自带的连接池 -->
			<dataSource type="POOLED">
                <!-- 连接数据库的参数:使用属性文件的方式 -->
                <property name="driver" value="${db.driver}" />
				<property name="url" value="${db.url}" />
				<property name="username" value="${db.username}" />	<!-- 加前缀,是因为电脑环境中可能会有username,比如电脑的账户名-->
				<property name="password" value="${db.password}" />
			</dataSource>
		</environment>
	</environments>
   <!-- 这个mappers代表的是相应的ORM映射文件 -->
	<mappers> 
		<mapper resource="cn/itsource/domain/ProductMapper.xml" /> 
	</mappers> 
</configuration> 

2. Mapping file: Regarding the configuration files, they are all placed under the resource file, the package and java should be the same, so they are in the same directory on the hard disk

<?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的主要功能就是写sql
	 mapper:根
	 namespace:命令空间 (用来确定唯一)以前这个是可以不加的,现在必需加,xml之间通过这个来执行相互之间的方法,不是去接口里
     namespace的值:接口的完全限定名
 -->
<mapper namespace="cn.itsource.dao.IProductDao">
	<!-- 
		id:用来确定这条sql语句的唯一
			   以后我们确定唯一,也就是找sql语句 : namespace + id
			 例: cn.itsource.mybatis.day1._1_hello.IProductDao.findById
		parameterType : 传入的参数类型,只有一个的时候最好不写,因为不写一定不会错,写了反而可能会错
		resultType : 结果类型(第一条数据返回的对象类型)自己的对象一定是完全限定类名,
					如果查询记录是多条,接口中方法的返回值是集合,但是返回类型还是写对象,而不是集合,因为myBatis会帮我们将多条数据放入集合中
		#{}:底层调get方法,当传入参数只有一个时,里面写啥都可以,并不需要和参数名相同
	 -->
	<select id="findById" parameterType="long" resultType="cn.itsource.domain.Product">
		select * from product where id = #{id}
	</select>
</mapper> 

3. Extraction tool

public class MybatisUtils {
    
    

    private static SqlSessionFactory sqlSessionFactory;

    static {
    
    
        try {
    
    
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

    public static SqlSession opSession() {
    
    
        if (sqlSessionFactory != null) {
    
    
            return sqlSessionFactory.openSession();
        }
        return null;
    }
}

4. Test: execute sql statement in xml
4.1, traditional way:

	@Test
    public void testFindAll() throws Exception{
    
    
        //获取到会话对象
        SqlSession session = MybatisUtils.opSession();
        //通过命名空间+id,调用sql
        Product product = session.findById(“cn.itsource.dao.IProductDao”,1L);
    }

4.2.
Conditions for creating the Mapper Mapper :
(1) The namespace of the Mapper mapping file (.xml) must be consistent with the "fully qualified name" of the interface;
(2) The id of the definition sql tag needs to be the same as the "method name of the interface" Consistent

	@Test
    public void testFindAll() throws Exception{
    
    
        //获取到会话对象
        SqlSession session = MybatisUtils.opSession();
        //拿到映射对象,是通过mybatis自动创建的指定接口代理对象
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        List<Product> products = mapper.findById(1L);
        //如果是增删改操作,事务需要手动提交
        //session.commit()
        //lambda表达式
        products.forEach(System.out::println);
    }

Guess you like

Origin blog.csdn.net/ExceptionCoder/article/details/108682622