首先,导jar包一定不能导错!
采用4.3.0来建立连接
其次,建立xml,然后选中建立的xml,右键单击选择open with中的spring config editor
若找不到,则在other中选择以下选项
命名为bean.xml,内容如下(这个名字要记好,待会要用到)
(其中的userDao是根据此工程的其他代码进行配置的,其余部分可通用,同时记得要写DbUtil类)
同时还要注意url中的数据库名称
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///test?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dbUtil" class="com.edu.spring.datasource.DbUtil">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao" class="com.edu.spring.datasource.UserDao">
<property name="dbUtil" ref="dbUtil"></property>
</bean>
</beans>
获取数据库连接对象与关闭数据库连接的工具类DbUtil.java
package com.edu.spring.datasource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
public class DbUtil {
private DataSource dataSource;
public DataSource getDataSource(){
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public Connection getConnection(){
Connection conn=null;
try{
conn=dataSource.getConnection();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
public void close(Connection conn,PreparedStatement pstmt,ResultSet rs){
if(rs!=null){try{rs.close();}catch(SQLException e){e.printStackTrace();}}
if(pstmt!=null){try{pstmt.close();}catch(SQLException e){e.printStackTrace();}}
if(conn!=null){try{conn.close();}catch(SQLException e){e.printStackTrace();}}
}
}
Dao接口IUserDao.java
package com.edu.spring.datasource;
public interface IUserDAO {
public void insert(User user);
public User find(String name);
}
编写JavaBean(User.java)
package com.edu.spring.datasource;
public class User {
private Integer id;
private String name;
private int age;
public User(Integer id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public User() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
编写接口IUserDao的实现类UserDao.java
此处给出了插入函数和查询函数
package com.edu.spring.datasource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao implements IUserDAO{
DbUtil dbUtil;
public DbUtil getDbUtil() {return dbUtil;}
public void setDbUtil(DbUtil dbUtil) {this.dbUtil = dbUtil;}
public void insert(User user){
Connection conn=dbUtil.getConnection();
PreparedStatement stmt=null;
String sql="insert into user(name,age) values(?,?)";
try{
stmt=conn.prepareStatement(sql);
stmt.setString(1, user.getName());
stmt.setInt(2, user.getAge());
stmt.executeUpdate();
System.out.println("插入完成!"+user.getName());
}catch(SQLException e){e.printStackTrace();
}finally{dbUtil.close(conn, stmt, null);}
}
public User find(String name){
Connection conn=dbUtil.getConnection();
PreparedStatement stmt=null;
ResultSet rs=null;
User user=new User();
String sql="select * from user where name=?";
try{
stmt=conn.prepareStatement(sql);
stmt.setString(1, name);
rs=stmt.executeQuery();
if(rs.next()){
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
}
}catch(SQLException e){e.printStackTrace();
}finally{dbUtil.close(conn, stmt,rs);}
return user;
}
}
测试类Main.java,此处要用到刚刚写的配置文件,名字一定不能写错
package com.edu.spring.datasource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
String path="bean.xml";
ApplicationContext ctx=new ClassPathXmlApplicationContext(path);
IUserDAO userDao=(UserDao)ctx.getBean("userDao");
User user=new User();
user.setAge(20);
user.setName("张三");
userDao.insert(user);
User user2=userDao.find("张三");
if(user2!=null){
String xx="序号:"+user2.getId()+"姓名:"+user2.getName()+"年龄:"+user2.getAge();
System.out.println("查询到的信息为:"+xx);
}else{
System.out.println("没有查询到!");
}
}
}
jar包截图(应该是多了好多,但起码多了比少了好)