用spring的beans配置mysql数据库

今天很兴奋,因为我用spring的beans配置mysql数据库,竟然能用了。下面我用自己写的小项目说一下自己是怎么配置的。
先说下准备工作,需要下个spring包有:spring-framework-3.1.1.RELEASE-with-docs.zip
mysql数据库的驱动包:mysql-connector-java-5.1.18.zip
用mvc模式写本项目:
配置beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/databaseName" />
<property name="username" value="root" />
<property name="password" value="1234"/>
</bean>

<bean id="dbHome" class="com.DBHome">
<property name="dataSource" ref="dataSource"></property>
</bean>

</beans>


用一个servlet初始化这个beans,然后用单例模式控制使整个项目用一个bean工厂:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;


public class InitServlet extends HttpServlet {

public static Resource res = null;
public static BeanFactory factory = null;
public InitServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

@SuppressWarnings("deprecation")
public void init() throws ServletException {
if(res==null)
res = new ClassPathResource("/com/beans.xml");
if(factory==null)
factory = new XmlBeanFactory(res);
}

}


下面就是mvc模式中的m了,这是我随便写的一个测试类:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.jdbc.datasource.DriverManagerDataSource;


public class DBHome {
public DriverManagerDataSource database = null;
public DBHome() {
}
public DBHome(DriverManagerDataSource database) {
System.out.println("++++++++++++");
this.setDataSource(database);
}
public void setDataSource(DriverManagerDataSource database){ //注意这个必须要写这个set方法,这个方法起名是有要求的:set+在beans里面database映射到DBHome里面的名字(首字母要变大写),也就是setDataSource。这样写,beans会自动找到这个方法,然后给DriverManagerDataSource database赋值。
this.database = database;
}
public void test(){
Connection conn=null;
try {
conn =database.getConnection();
Statement s =conn.createStatement();
ResultSet res =s.executeQuery("select * from userTable");
while(res.next()){
System.out.println(res.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


然后是mvc中的c了,他要用到DBHome类,应该从beans里面取,该怎么取呢?看下面:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class UserServlet extends HttpServlet {

public DBHome home = (DBHome)InitServlet.factory.getBean("dbHome");//从beans里面取生成的对象。
public UserServlet() {
super();
}

public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
home.test();
}

}


输出结果:xiaoming
xiaohong
当然没写v的部分,在这简单的写了一下spring怎么通过beans配置数据库,通过beans怎么实例化配置在beans里面的类的对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值