Spring的简单入门(二)之jdbc

这篇博客介绍了如何在Spring框架中使用JDBC连接数据库,包括在pom.xml中添加必要的依赖库,配置spring.xml文件以及控制器层的设置。
摘要由CSDN通过智能技术生成

一.使用jdbc连接数据库

1.在pom.xml文件中添加jdbc的架包和数据库的架包

	<!-- 添加jdbc的架包 -->
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-jdbc</artifactId>
  		<version>4.3.12.RELEASE</version>
  	</dependency>
  		<!-- 加载数据库的架包 -->
  	<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<version>5.1.26</version>
  	</dependency>
 2.创建jdbc.properties的文件写入四要素 

url=jdbc:mysql://localhost:3306/news
driverClassName=com.mysql.jdbc.Driver
userName1=root
password=123456
3.在spring.xml中配置bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd">
     
     <!-- 使用bean来定义读取资源文件
    	优先读取bean定义的
    	context命名空间 优先读取系统资源
     -->
        <context:property-placeholder location="classpath:/less02/jdbc/jdbc.properties"/> 
	<!--数据源只是为了获取链接 
	${username}是个关键字 默认是系统的用户名 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="url" value="${url}"></property>
		<property name="username" value="${userName1}"></property>
		<property name="password" value="${password}"></property>
		<property name="driverClassName" value="${driverClassName}"></property>
	</bean>
	<!-- 封装一些操作的方法 -->
	<bean id="jdbcTemp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值