前提:在Spring框架的基础下
1.在pom.xml里导入Springjdbc及mysql、连接池的相关依赖:
根据所需版本改变版本号即可!
<!--jdbc-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<!--连接池-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
2.在resources下面增加三个配置文件db.properties、application-context-jdbc.xml、application-context.xml
(1)新建db.properties中,配置数据库连接文件:
注意:我用的是mysql8.0版本的,和5.0的配置不同
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/【数据库名】?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
db.username=【用户名】
db.password=【密码】
(2)在application-context-jdbc.xml文件中,配置jdbc的相关属性:
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.wjw.*"/>
<!--加载properties文件-->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="initialSize" value="10"/>
<property name="maxActive" value="20"/>
<property name="minIdle" value="5"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--<bean id="usersService" class="com.wjw.service.UsersService" >
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>-->
</beans>
(3)在application-context.xml文件中,配置jdbc的相关属性,导入application-context-jdbc.xml的配置文件:
<?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.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.wjw.*"/>
<bean id="myUser" class="com.wjw.domain.Users"/>
<!--构造方法注入-->
<bean id="myUser2" class="com.wjw.domain.Users">
<constructor-arg name="uname" value="wjw"/>
<constructor-arg name="upwd" value="qaz123"/>
</bean>
<!--DI-->
<bean id="myPtype" class="com.wjw.domain.PType">
<property name="ptid" value="1"/>
<property name="ptname" value="水果"/>
</bean>
<bean id="myProduct" class="com.wjw.domain.Product">
<constructor-arg name="pid" value="1"/>
<constructor-arg name="pcode" value="fd1231231"/>
<constructor-arg name="pname" value="香蕉"/>
<constructor-arg name="ptid" ref="myPtype"/>
<constructor-arg name="pprice" value="5"/>
</bean>
<import resource="application-context-jdbc.xml"/>
</beans>
3.测试是否成功:
public static void main(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
DataSource dataSource = (DataSource)applicationContext.getBean("dataSource");
Connection connection = dataSource.getConnection();
if(connection != null){
System.out.println("OK");
}
}
输出OK即连接成功!
后见Spring持久层(三)
遇到的问题及解决:
问题1:jdbc启动错误(The driver has not received any packets from the server)
解决:启动mysql服务
问题二:MySQL 8.0 Public Key Retrieval is not allowed 错误
解决:在db.properties的链接后填加 & allowPublicKeyRetrieval=true