在项目中使用Spring
下载Spring ,由于官网的改版,下载Spring不太方便,网上查找后,推荐一个Spring下载网址http://repo.spring.io/libs-release-local/org/springframework/spring/
注意要添加commons-logging-1.1.1的包
P1:HelloSpring
新建一个Java Project项目,新建包com.tutorialspoint
编写两个类HelloWorld和MainApp
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
MainApp
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
编写Beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- test wiring -->
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
注意:依赖SpringContext注入的属性需要写get/set方法
P2:配置数据源(datasource)采用JDBC直连数据库
在Beans.xml文件中配置DataSource bean
<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/midatabase" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
编写DAO类,直接持有datasource对象,用来访问数据库
package com.tutorialspoint;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
public class DirectJdbc {
private static final String SelectAll= "select * from mitable";
private DataSource datasource;
public void showAllItems(){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = this.datasource.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(SelectAll);
while(rs.next()){
System.out.println(rs.getString("id")+":"+rs.getString("name"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt!=null){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public DataSource getDatasource() {
return datasource;
}
public void setDatasource(DataSource datasource) {
this.datasource = datasource;
}
}
从上面到代码可以看出,访问数据库的这个过程依然比较繁琐,当然,这仅仅是用来一步步熟悉Springframework。
配置Beans.xml
<bean id="directjdbc" class="com.tutorialspoint.DirectJdbc">
<property name="datasource" ref="datasource"></property>
</bean>
在MainApp的main()方法中,添加如下代码
DirectJdbc djdbc = (DirectJdbc) context.getBean("directjdbc");
djdbc.showAllItems();
P3:使用SimpleJDBCTemplate
首先在Beans.xml中配置jdbctemplage bean对象
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="datasource" />
</bean>
编写一个DAO类,持有SimpleJdbcTemplate对象
package com.tutorialspoint;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
public class TemplateJdbc {
private static final String INSET = "insert into mitable (id,name) values(?,?)";
<span style="color:#ff0000;">@SuppressWarnings("deprecation")</span>
private SimpleJdbcTemplate simplejdbctemplate;
public void addItem(Item item){
this.simplejdbctemplate.update(this.INSET,item.getId(),item.getName());
}
public SimpleJdbcTemplate getSimplejdbctemplate() {
return simplejdbctemplate;
}
public void setSimplejdbctemplate(SimpleJdbcTemplate simplejdbctemplate) {
this.simplejdbctemplate = simplejdbctemplate;
}
}
在main函数中,添加代码
</pre><pre name="code" class="java">TemplateJdbc tjdbc = (TemplateJdbc) context.getBean("template");
Item it1 = new Item(6,"mi@eil");
tjdbc.addItem(it1);