XML
以mybatis.xml:获取所有Environment中的数据库并连接session为例
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.*;public static Map<String, SqlSession> openSessionOther(String resource){Map<String, SqlSession> sessionMap = new HashMap<>();try {InputStream inputStream = Resources.getResourceAsStream(resource);File configFile = new File(resource);DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();Document doc = dBuilder.parse(inputStream);doc.getDocumentElement().normalize();NodeList nList = doc.getElementsByTagName("environment");for (int temp = 1; temp < nList.getLength(); temp++) {Node nNode = nList.item(temp);if (nNode.getNodeType() == Node.ELEMENT_NODE) {Element eElement = (Element) nNode;SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource), eElement.getAttribute("id"));sessionMap.put(eElement.getAttribute("id"), factory.openSession());log.debug("Environment id : " + eElement.getAttribute("id"));}}} catch (IOException| ParserConfigurationException | SAXException e) {e.printStackTrace();log.error("其他数据库连接异常!!!");throw new ExceptionInInitializerError(e);}return sessionMap;}
properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
1.properties文件mybatis数据库配置
<?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><!--配置--><properties resource="jdbc.properties"><!--properties属性--><property name="database.driver" value="com.mysql.jdbc.Driver"/> <property name="database.url" value="jdbc:mysql://localhost:3306/chapter4"/><property name="database.username" value="root"/> <property name="database.password" value="123456"/> </properties><typeAliases><!-- 别名 --><typeAlias alias="Student" type="domain.Student"/> </typeAliases><!-- 数据库环境 --><environments default="development"><!--配置环境--><environment id="development"><!--环境变量--><transactionManager type="JDBC" /><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><package name="com.learn.ssm.chapter4.mapper" /></mappers>
</configuration>
2.java读取
方式很多,获取文件流
public void readProperties1() throws IOException {//不加/会从当前包进行寻找,加上/会从src开始找InputStream inputStream = this.getClass().getResourceAsStream("jdbc.properties");Properties properties=new Properties();properties.load(inputStream);System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}// import org.apache.ibatis.io.Resources;
public static void main(String[] args) {try {Properties properties = new Properties(Resources.getResourceAsProperties(""));} catch (IOException e) {e.printStackTrace();}}
YML解析
SnakeYAML
-
优点:功能强大、灵活的API、广泛使用。
- 缺点:相对复杂,需要处理更多的细节。
Jackson Dataformat YAML
-
优点:与JSON处理库相同的API、广泛使用、与Jackson生态系统集成。
-
缺点:相对复杂,需要在Jackson库的基础上进行配置和使用。
YAMLBeans
-
优点:轻量级、简单易用。
-
缺点:相对功能较少,适用于简单的YAML数据处理。