一.使用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