当前位置: 首页> 科技> 名企 > 电脑网页设计教程_成都百度网站制作_今天实时热搜榜排名_百度推广管家登录

电脑网页设计教程_成都百度网站制作_今天实时热搜榜排名_百度推广管家登录

时间:2025/7/14 5:29:06来源:https://blog.csdn.net/w776341482/article/details/144348242 浏览次数:1次
电脑网页设计教程_成都百度网站制作_今天实时热搜榜排名_百度推广管家登录

点一下关注吧!!!非常感谢!!持续更新!!!

大数据篇正在更新!https://blog.csdn.net/w776341482/category_12713819.html

在这里插入图片描述

目前已经更新到了:

  • MyBatis(正在更新)

框架实现

在当前的项目中,在 resources 下新建:

  • sqlMapConfig.xml
  • mapper.xml

sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration><property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://172.16.1.130:3306/wzk-mybatis?characterEncoding=utf-8"></property><property name="user" value="hive"></property><property name="password" value="hive@wzk.icu"></property><mapper resource="mapper.xml"></mapper>
</configuration>

对应的截图如下所示:
在这里插入图片描述

这段代码是一个 MyBatis 的配置文件,通常命名为 mybatis-config.xml 或类似的名字。它主要完成以下任务:

  • 配置数据库连接属性,例如驱动类、JDBC URL、用户名和密码。
  • 引入 MyBatis 的映射文件,用于定义 SQL 语句和映射关系。

mapper.xml

实现一个单查询和列表的查询

<mapper namespace="icu.wzk.dao.UserInfoMapper"><select id="selectOne" parameterType="icu.wzk.model.UserInfo" resultType="icu.wzk.model.UserInfo">SELECT*FROMuser_infoWHEREusername=#{username}</select><select id="selectList" parameterType="icu.wzk.model.UserInfo" resultType="icu.wzk.model.UserInfo">SELECT*FROMuser_info</select>
</mapper>

对应的截图如下所示:
在这里插入图片描述
命名空间 (namespace):

  • mapper namespace=“icu.wzk.dao.UserInfoMapper”:定义了映射文件的命名空间,用于标识当前的 Mapper 文件。这里的命名空间与 Java 接口类 UserInfoMapper 对应。

单条记录查询 (selectOne):

  • select id=“selectOne” parameterType=“icu.wzk.model.UserInfo” resultType=“icu.wzk.model.UserInfo”:这是一个 select 标签,用于查询单条记录。id=“selectOne” 定义了方法名称,与 UserInfoMapper 接口中的方法名对应。parameterType=“icu.wzk.model.UserInfo” 指定了方法的参数类型,这里是 UserInfo 对象。resultType=“icu.wzk.model.UserInfo” 指定了返回结果的类型,同样是 UserInfo 对象。

实体相关

model

新建一个实体类,UserInfo

package icu.wzk.model;import lombok.Data;@Data
public class UserInfo {private Long id;private String username;private String password;private Integer age;}

资源相关

Configuration

package icu.wzk.bean;import lombok.Data;import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;@Data
public class Configuration {private DataSource dataSource;private Map<String, MappedStatement> mappedStatementMap = new HashMap<>();}

MappedStatment

package icu.wzk.bean;import lombok.Data;@Data
public class MappedStatement {private String id;private String sql;private String parameterType;private String resultType;}

Resources

package icu.wzk.bean;import java.io.InputStream;public class Resources {public static InputStream getResourceAsStream(String path) {return Resources.class.getClassLoader().getResourceAsStream(path);}}

解析相关

XMLConfigerBuilder

package icu.wzk.bean;import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;public class XMLConfigerBuilder {private Configuration configuration;public XMLConfigerBuilder(Configuration configuration) {this.configuration = configuration;}public Configuration parseConfiguration(InputStream inputStream) throws DocumentException, PropertyVetoException, ClassNotFoundException {Document document = new SAXReader().read(inputStream);Element rootElement = document.getRootElement();List<Element> propertyElements = rootElement.selectNodes("//property");Properties properties = new Properties();for (Element element : propertyElements) {String name = element.attributeValue("name");String value = element.attributeValue("value");properties.setProperty(name, value);}ComboPooledDataSource comboSource = new ComboPooledDataSource();comboSource.setDriverClass(properties.getProperty("driverClass"));comboSource.setJdbcUrl(properties.getProperty("jdbcUrl"));comboSource.setUser(properties.getProperty("user"));comboSource.setPassword(properties.getProperty("password"));configuration.setDataSource(comboSource);List<Element> mappedElements = rootElement.selectNodes("//mapper");XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(configuration);for (Element element : mappedElements) {String mapperPath = element.attributeValue("resource");InputStream resourceAsStream = Resources.getResourceAsStream(mapperPath);xmlMapperBuilder.parse(resourceAsStream);}return configuration;}}

这段代码是一个用于解析 XML 配置文件的 Java 类,主要作用是读取 XML 配置文件内容,初始化应用程序所需的配置信息(例如数据库连接池配置、Mapper 文件解析等)。

类的定义

类名 XMLConfigerBuilder:表示这是一个 XML 配置解析器。
成员变量 configuration:保存应用的配置信息,类型为 Configuration。

构造方法

XMLConfigerBuilder(Configuration configuration):通过构造方法将 Configuration 对象传入并保存在成员变量中。

parseConfiguration 方法

该方法负责解析 XML 文件并初始化 Configuration 对象,主要包括以下几个步骤:
解析 XML 文档:

  • 使用 SAXReader 读取输入流(InputStream),生成 XML 文档对象。
  • 获取 XML 的根元素(rootElement)。

读取数据库配置:

  • 查找 XML 中的 节点,解析出 name 和 value 属性,将其存入 Properties 对象。
  • 创建 ComboPooledDataSource 对象,用于配置数据库连接池,设置驱动类、URL、用户名和密码等信息。
  • 将初始化好的数据源对象设置到 configuration 中。

解析 Mapper 文件

  • 查找 XML 中的 节点,提取每个节点的 resource 属性值(Mapper 文件路径)。
  • 逐个读取 Mapper 文件,通过 XMLMapperBuilder 的 parse 方法解析每个文件,将其结果更新到 configuration 中。

返回最终的 Configuration 对象

  • 方法执行完成后,返回已经完整解析并初始化的 Configuration 对象。

XMLMapperBuilder

package icu.wzk.bean;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;import java.io.InputStream;
import java.util.List;public class XMLMapperBuilder {private Configuration configuration;public XMLMapperBuilder(Configuration configuration) {this.configuration = configuration;}public void parse(InputStream inputStream) throws DocumentException, ClassNotFoundException {Document document = new SAXReader().read(inputStream);Element rootElement = document.getRootElement();String namespace = rootElement.attributeValue("namespace");List<Element> selectList = rootElement.selectNodes("select");for (Element element : selectList) {String id = element.attributeValue("id");String parameterType = element.attributeValue("parameterType");String resultType = element.attributeValue("resultType");String key = namespace + "." + id;String textTrim = element.getTextTrim();MappedStatement mappedStatement = new MappedStatement();mappedStatement.setId(id);mappedStatement.setParameterType(parameterType);mappedStatement.setResultType(resultType);mappedStatement.setSql(textTrim);configuration.getMappedStatementMap().put(key, mappedStatement);}}}
关键字:电脑网页设计教程_成都百度网站制作_今天实时热搜榜排名_百度推广管家登录

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: