Java中使用SQL标签库<sql:setDataSource />设置数据源

setDataSource标签用于生成一个数据源,需要制定JDBC驱动、数据库连接URL、数据库用户名密码等,例如:
setDataSource.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<sql:setDataSource driver="com.mysql.jdbc.Driver" user="root"
    password="123"
    url="jdbc:mysql://localhost:3306/jstl?charachterEncoding=UTF-8"
    var="dataSource" scope="page" />

<sql:setDataSource dataSource="jdbc/jstl" var="serverDataSource" />

数据源:${ dataSource.class.name }

${ serverDataSource }


</body>
</html>

var用户指明数据源的名称,以供其他的sql标签使用。程序中输出了JSTL创建的数据源的类名:org.apache.taglibs.standard.tag.common.sql.DataSourceWrapper
。创建了数据源就可以进行数据库读写操作了。
setDataSource标签还支持JNDI数据源。可以通过dataSource属性制定JNDI名称,数据库信息都写在JNDI配置里,例如

<sql:setDataSource dataSource="jdbc/jstl" var="serverDataSource"/>

JNDI数据源需要在服务器中配置。Tomcat6中配置数据源的方法为:在Web应用的/WEB-INF/下新建context.xml文件(与web.xml同一个位置),配置代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/jstl1" type="javax.sql.DataSource"
    driverClassName="com.mysql.jdbc.Driver" 
    maxIdle="5"
    maxWait="5000"
    username="root"
    password="123"
    url="jdbc:mysql://localhost:3306/jstl1?characterEncoding=UTF-8"
    maxActive="10"
    />
</Context>

程序运行结果如下:
这里写图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是代码: 首先,在 pom.xml 文件添加以下依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> ``` 接下来,配置数据源。在 application.properties 文件添加以下配置: ```properties # 主数据源配置 spring.datasource.primary.url=jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 spring.datasource.primary.username=root spring.datasource.primary.password=root spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver # 从数据源配置 spring.datasource.secondary.url=jdbc:mysql://localhost:3306/db2?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 spring.datasource.secondary.username=root spring.datasource.secondary.password=root spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver ``` 然后,创建两个数据源的配置类。分别为 PrimaryDataSourceConfig 和 SecondaryDataSourceConfig。代码如下: ```java @Configuration @MapperScan(basePackages = "com.example.demo.mapper.primary", sqlSessionTemplateRef = "primarySqlSessionTemplate") public class PrimaryDataSourceConfig { @Bean(name = "primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "primarySqlSessionFactory") public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/primary/*.xml")); return bean.getObject(); } @Bean(name = "primaryTransactionManager") public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "primarySqlSessionTemplate") public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } ``` ```java @Configuration @MapperScan(basePackages = "com.example.demo.mapper.secondary", sqlSessionTemplateRef = "secondarySqlSessionTemplate") public class SecondaryDataSourceConfig { @Bean(name = "secondaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondarySqlSessionFactory") public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/secondary/*.xml")); return bean.getObject(); } @Bean(name = "secondaryTransactionManager") public DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "secondarySqlSessionTemplate") public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 最后,创建两个 Mapper 接口。分别为 PrimaryMapper 和 SecondaryMapper。代码如下: ```java @Mapper public interface PrimaryMapper { @Select("SELECT * FROM table1") List<Table1> selectFromPrimary(); } @Mapper public interface SecondaryMapper { @Select("SELECT * FROM table2") List<Table2> selectFromSecondary(); } ``` 这样,就完成了整合 MyBatis 多数据源的配置。在需要使用的地方,可以通过 @Autowired 注入对应的 Mapper 接口,然后调用其的方法即可。 注意:以上代码仅供参考,具体实现需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值