摘要:一個(gè)使用了DataSource注入的完整AOP例子。
?
●????
IUserDAO.java
?
完成一個(gè)插入和查詢方法
package com.kela.spring.jdbc;
?
public interface IUserDAO {
???
??? public void insert(User user);
??? public User find(Integer id);
}
?
●????
User.java
同上一節(jié)
●????
UserDAO.java
?
package com.kela.spring.jdbc;
?
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
?
import javax.sql.DataSource;
?
import com.kela.spring.util.Util;
?
public class UserDAO implements IUserDAO {
???
??? private DataSource dataSource;
???
??? public DataSource getDataSource() {
??????? return dataSource;
??? }
?
??? public void setDataSource(DataSource dataSource) {
??????? this.dataSource = dataSource;
??? }
?
??? public void insert(User user) {
??????? String name = user.getName();
??????? int age = user.getAge().intValue();
?
??????? String sql = "INSERT INTO user (name, age) VALUES ('" + name + "', " + age + ")";
??????? //
為解決Mysql5.0的中文問題,轉(zhuǎn)碼
??????? sql = Util.GBKToISO(sql);
???????
??????? Connection conn = null;
??????? Statement stmt = null;
???????
??????? try {
??????????? conn = dataSource.getConnection();
??????????? stmt = conn.createStatement();
??????????? stmt.execute(sql);
???????????
??????? } catch (SQLException e) {
??????????? e.printStackTrace();
??????? } finally {
??????????? if(stmt != null) {
??????????????? try {
??????????????????? stmt.close();
??????????????? } catch (SQLException e) {
??????????????????? e.printStackTrace();
??????????????? }
??????????? }
??????????? if(conn != null) {
??????????????? try {
??????????????????? conn.close();
??????????????? } catch (SQLException e) {
??????????????????? e.printStackTrace();
??????????????? }
??????????? }
??????? }
???????
???
??? }
?
??? public User find(Integer id) {
???????
??????? String sql = "SELECT * FROM user WHERE id = " + id.intValue();
???????
??????? Connection conn = null;
??????? Statement stmt = null;
???????
??????? try {
??????????? conn = dataSource.getConnection();
??????????? stmt = conn.createStatement();
???????????
??????????? ResultSet rs = stmt.executeQuery(sql);
??????????? if(rs.next()) {
??????????????? Integer i = new Integer(rs.getInt(1));
??????????????? String name = Util.getStr(rs.getString(2));//
轉(zhuǎn)碼
??????????????? Integer age? = new Integer(rs.getInt(3));
???????????????
??????????????? User user = new User();
??????????????? user.setId(i);
??????????????? user.setAge(age);
??????????????? user.setName(name);
???????????????
??????????????? return user;
??????????? }
??????? } catch(SQLException e) {
??????????? e.printStackTrace();
??????? } finally {
??????????? if(stmt != null) {
??????????????? try {
??????????????????? stmt.close();
??????????????? } catch (SQLException e) {
??????????????????? e.printStackTrace();
??????????????? }
??????????? }
??????????? if(conn != null) {
??????????????? try {
??????????????????? conn.close();
??????????????? } catch (SQLException e) {
??????????????????? e.printStackTrace();
??????????????? }
??????????? }
??????? }
???????
??????? return null;
??? }
?
}
?
?
●????
Beans-config.xml
?
注入了DataSource的實(shí)例,這里使用了DBCP來獲得連接池的功能,如果需要其他連接設(shè)置,直接在修改該配置文件即可。另外需要,DBCP所需要的.jar文件(commons-dbcp.jar, commons-pool.jar, commons-collections.jar)。
?
<?xml
version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE
beans
PUBLIC
"-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd"
>
?
<beans>
???
<bean
id=
"dataSource"
class=
"org.apache.commons.dbcp.BasicDataSource"
destroy-method=
"close"
>
??????
<property
name=
"driverClassName"
>
??????????
<value>
com.mysql.jdbc.Driver
</value>
??????
</property>
??????
??????
<property
name=
"url"
>
??????????
<value>
jdbc:mysql://127.0.0.1:3306/demo
</value>
??????
</property>
??????
??????
<property
name=
"username"
>
??????????
<value>
root
</value>
??????
</property>
??????
??????
<property
name=
"password"
>
??????????
<value></value>
??????
</property>
???
</bean>
???
???
<bean
id=
"userDAO"
class=
"com.kela.spring.jdbc.UserDAO"
>
??????
<property
name=
"dataSource"
>
??????????
<ref
bean=
"dataSource"
/>
??????
</property>
???
</bean>
</beans>
?
?
●????
SpringDAODemo.java
?
最后是測試類
?
package com.kela.spring.jdbc;
?
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
?
public class SpringDAODemo {
???
/**
*
測試insert()
*/
??? public void method_1() {
??????? try {
??????????? ApplicationContext context = new FileSystemXmlApplicationContext(
??????????????????? "bin\\beans-config.xml");
??????????? User user = new User();
??????????? user.setName("kela001");
??????????? user.setAge(new Integer(27));
?
??????????? IUserDAO userDao = (IUserDAO) context.getBean("userDAO");
??????????? userDao.insert(user);
??????????? System.out.println("** OK **");
??????? } catch (Exception e) {
??????????? System.out.println("[ERROR]" + e.getMessage());
??????? }
??? }
???
??? public void method_2() {
??????? try {
??????????? ApplicationContext context = new FileSystemXmlApplicationContext(
??????????????????? "bin\\beans-config.xml");
??????????? User user = new User();
??????????? IUserDAO userDao = (IUserDAO) context.getBean("userDAO");
??????????? user = userDao.find(new Integer(1));
??????????? System.out.println("name:" + user.getName());
??????????? System.out.println("age:" + user.getAge());
??????????? System.out.println("** OK **");
??????? } catch (Exception e) {
??????????? System.out.println("[ERROR]" + e.getMessage());
??????? }
??? }
??? public static void main(String[] args) {
??????? SpringDAODemo springDAODemo = new SpringDAODemo();
??????? springDAODemo.method_1();
springDAODemo.method_2();
??? }
}
?
●????
demo.sql
?
create table user (
id int(11) not null auto_increment ERIMARY KEY,
name varchar(20),
age int(3));
?
●????
Util.java
?
測試中用的轉(zhuǎn)碼工具類
?
package com.kela.spring.util;
?
import java.io.UnsupportedEncodingException;
?
public class Util {
???
??? public static String getStr(String str) {
??????? try {
??????????? String temp_p = str;
??????????? byte[] temp_t = temp_p.getBytes("ISO8859-1");
??????????? //
使用ISO8859-1字符集解碼字節(jié)的指定數(shù)組
??????????? String temp = new String(temp_t);
???
??????? return temp;
??????? } catch(UnsupportedEncodingException ex) {
??????????? ex.printStackTrace();
??????????? return "";
??????? }
??? }
???
??? /**
???
?*
方法是使用指定的字符集(ISO8859-1)解碼指定的字節(jié)數(shù)組(GBK)
???
?* @param str
???
?* @return
???
?*/
??? public static String GBKToISO(String str) {
??????? if(str == null) {
??????????? str = "";
??????? } else {
??????????? try {
??????????????? str = new String(str.getBytes("GBK"), "ISO8859-1");
??????????? } catch (Exception e) {
??????????????? e.printStackTrace();
??????????? }
???????????
??????? }
???????
??????? return str;
??? }
}
?