JavaEE MyBatis核心配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/83381498

一、MyBatis的核心对象

1.SqlSessionFactory

SqlSessionFactory是MyBatis框架中十分重要的对象,主要作用是用来创建SqlSession。

2.SqlSession

SqlSession是MyBatis框架另一个十分重要的对象,它包含了数据库中的所有SQL操作方法,由于其底层封装了JDBC连接,所以可以直接使用其实例来执行已映射的SQL语句

二、MyBatis的配置文件

1.作用

其首要作用是承载MyBatis的配置信息,例如MyBatis的约束,数据库环境等等

2.主要元素

MyBatis配置文件的主要元素
元素名称 描述
configuration 是配置文件的根元素,其余元素均是其子元素
properties 通常用于将内部的配置外在化,及通过外部的配置来动态的替换内部定义的属性,例如数据库的连接等属性
setting 用于改变MyBatis运行时的行为,例如:开启二级缓存,开启延迟加载
typeAliases 用于为Java类型设置一个简短的名字,即起别名
typeHandler 将预处理语句的参数从Java类型转化为jdbc类型
plugins 用于配置用户开发的插件
environments 对数据源的配置,可以配置多种数据库
mappers 指定MyBatis映射文件的位置

三、MyBatis的映射文件

1.主要作用

是用来映射操作语句的,例如select查询语句,insert插入语句,等等

2.主要元素

MyBatis的映射文件的主要元素
元素名称 元素描述
mapper 是映射文件的根元素,其他元素都是它的子元素
select 映射查询语句,可自定义参数
insert 映射插入语句,执行后返回一个整数
delete 映射删除语句,执行后返回一个整数
update 映射更新语句,执行后返回一个整数
sql 用于定义一部分sql,然后可以被其他与句所引用,
cache 给定命名空间的缓存配置
resultMap 用于描述如何从数据库结果集中加载对象

四、操作演示

(本篇用到的主要代码来源于之前的一篇博客(点这里))

首先select,insert,update,delete等元素在上一篇博客中已经学习过(点这里),所以现在主要学习resultMap元素的用法,

1.在数据库mybatis中创建t_user表

create table t_user(
t_id int primary key auto_increment,
t_name varchar(50),
t_age int);

insert into t_user(t_name,t_age)
values('lucy',25),
('lili',20),
('jim',20);

2.在com.itheima.po包中,创建持久化类User,并在类中定义id,name,age等属性

package com.itheima.po;

public class User {

	private Integer id;
	private String name;
	private Integer age;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String toString() {
		return "User [id=" + id + ",name=" + name + ",age=" + age + "]";
	}
}

3.在com.itheima.mapper包下创建映射文件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.itheima.mapper.UserMapper">
    <resultMap type="com.itheima.po.user" id="resultMap">
        <id property="id" column="t_id"/>
        <result property="name" column="t_name"/>
        <result property="age" column="t_age"/>
    </resultMap>
    <select id="findAllUser" resultMap="resultMap">
        select * from t_user
    </select>
</mapper>

4.在配置文件中引入UserMapper.xml

    <mappers>
        <mapper resource="com/itheima/mapper/UserMapper.xml"/>
    </mappers>

5.编写测试方法

@Test
	public void findAllUserTest() throws Exception {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		 
		//2.根据配置文件构建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		//3.通过SqlSessionFactory创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		
		List<User> list = sqlSession.selectList("com.itheima.mapper.UserMapper.findAllUser");
		for(User user : list) {
			System.out.println(user);
		}
		//5.提交事务
		sqlSession.commit();
		//6.关闭SqlSession
	    sqlSession.close();
	}

猜你喜欢

转载自blog.csdn.net/qq_40788630/article/details/83381498
今日推荐