- 新建一个文件,选择为JAVA EE web选项
- 建好项目后,在web文件下建立lib包,并导入所需要的包(注意每个人的mysql版本不同,所需的jdbc包版本也不同,以自己的为主)
- 在src下建立所需的文件夹
- 将spring.xml文件拷贝到src下
- 编辑spring.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 http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描注解“@Service”-->
<context:component-scan base-package="com.zhongruan"></context:component-scan>
<!-- <bean id="userService" class="com.zhongruan.service.impl.UserServiceImpl"></bean>-->
<!-- <bean id="user" class="com.zhongruan.bean.User">-->
<!-- <property name="id" value="1"></property>-->
<!-- <property name="username" value="zfx"></property>-->
<!-- <property name="password" value="123"></property>-->
<!-- </bean>-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost/zjgm?serverTimezone=CTT"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
</beans>
6.User.java
package com.zhongruan.bean;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this