【MyBatis】Mybatis使用SqlSessionFactory加载xml文件
程序员文章站
2022-04-08 22:42:17
1.概述MyBatis框架主要是围绕着SqlSessionFactory这个类进行的,这个的创建过程如下:定义一个Configuration对象,其中包含数据源、事务、mapper文件资源以及影响数据库行为属性设置settings通过配置对象,则可以创建一个SqlSessionFactoryBuilder对象通过 SqlSessionFactoryBuilder 获得SqlSessionFactory 的实例。SqlSessionFactory 的实例可以获得操作数据的SqlSession实例....
1.概述
MyBatis框架主要是围绕着SqlSessionFactory这个类进行的,这个的创建过程如下:
- 定义一个Configuration对象,其中包含数据源、事务、mapper文件资源以及影响数据库行为属性设置settings
- 通过配置对象,则可以创建一个SqlSessionFactoryBuilder对象
- 通过 SqlSessionFactoryBuilder 获得SqlSessionFactory 的实例。
- SqlSessionFactory 的实例可以获得操作数据的SqlSession实例,通过这个实例对数据库进行操作
2.案例
案例结构如下
只需要看红色框框的就可以了。
2.1 数据源配置
位置:/Users/lcc/IdeaProjects/lcc_work/test-flink/flink-v110-myibatis-demo/src/main/resources/mybatis-config-person.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>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/><!-- 单独使用时配置成MANAGED没有事务 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mybatis/PersonMapper.xml"/>
</mappers>
</configuration>
然后我们要写一个mapppr类主要用来查询的。
package com.flink.myibatis.mapper;
import com.flink.myibatis.entity.Person;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import java.util.Map;
@Mapper
public interface PersonMapper {
List<Map<String,Object>> listAll();
}
最后配置mapper
<?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.flink.myibatis.mapper.PersonMapper">
<select id="listAll" resultType="java.util.Map">
SELECT * from db
</select>
</mapper>
这里很简单,但是要注意namespace不要写错了,然后我们写测试类
package com.flink.myibatis.utils;
import com.flink.myibatis.mapper.PersonMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import static org.junit.Assert.*;
public class MybatisSessionFactoryTest {
@Test
public void mainTest() throws FileNotFoundException {
Properties properties = new Properties();
properties.setProperty("jdbc.driver","com.mysql.jdbc.Driver");
// properties.setProperty("jdbc.url","jdbc:mysql://localhost:3306/mysql_monitor?useUnicode=true&characterEncoding=utf-8&useSSL=true");
properties.setProperty("jdbc.url","jdbc:mysql://localhost:3306/mysql_monitor");
properties.setProperty("jdbc.username","root");
properties.setProperty("jdbc.password","12345678");
File file = new File("/Users/lcc/IdeaProjects/lcc_work/test-flink/flink-v110-myibatis-demo/src/main/resources/mybatis-config-person.xml");
InputStream mybatis = new FileInputStream(file);
SqlSessionFactory session = new SqlSessionFactoryBuilder().build(mybatis,properties);
SqlSession sqlSession = session.openSession();
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
List<Map<String, Object>> persons = personMapper.listAll();
System.out.println(persons);
}
}
运行结果如下,可以看到查询到了数据
Opening JDBC Connection
Created connection 1470344997.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@57a3af25]
==> Preparing: SELECT * from db
==> Parameters:
<== Columns: id, name, ip, username, password, database, port, status
<== Row: 26, lcc, localhost, root, 12345678, house, 3306, null
<== Total: 1
[{password=12345678, database=house, port=3306, ip=localhost, name=lcc, id=26, username=root}]
但是这里有个坑
// properties.setProperty("jdbc.url","jdbc:mysql://localhost:3306/mysql_monitor?useUnicode=true&characterEncoding=utf-8&useSSL=true");
properties.setProperty("jdbc.url","jdbc:mysql://localhost:3306/mysql_monitor");
url只能写到库信息,再写多了就会报错mysql连接不上
报错参考:MySQL : mysql连接报 Communications link failure
参考:MyBatis执行SQL的两种方式:SqlSession和Mapper接口
参考:MyBatis的通俗理解:SqlSession.getMapper()源码分析
参考:MyBatis SqlSessionFactory的几种常见创建方式
参考:MyBatis常用对象SqlSessionFactory和SqlSession介绍和运用
本文地址:https://blog.csdn.net/qq_21383435/article/details/107424258
上一篇: 全局唯一ID
下一篇: 学习web前端开发从事小程序可以吗