spring DAO 实例

通过这个例子,简单介绍了使用Spring操作数据库流程。
使用JDBC 方式的Spring DAO 主要由以下几步:
1. 数据库中数据表myspringuser这个数据表的设计
2. 与数据库数据表myspringuser对应的数据类User的设计,
3. 设计用户表myspringuser的操作DAO类,UserDao,其中实现接口IUserDao
4. 配置文件bean.xml
5. 测试类SpringDaoTest

在开始使用Spring DAO前需要加入相关包到工程,主要有spring.jar,数据库驱动mysql-connector-java-5.1.0-bin.jar,log4j.jar,org.apache.commons.logging.jar。
下面开始我们的Spring DAO之旅。
第一步:在MySQL中设计数据表myspringuser;
使用SQLyog可以对MySQL进行图形化操作,在数据库myproject下建立数据表myspringuser,如下:
create table myspringuser( 
uid int not null, 
username varchar(36), 
age int, 
primary key(uid))

然后使用SQLyog设置uid为自动增长
第二步:设计与数据表myspringuser对应的数据类User
package com.peng.springdao;
public class User {
private Integer id;
private String name;
private Integer age;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
第三步:设计用户表myspringuser的操作DAO类,UserDao,其中实现接口IUserDao
首先设计接口IUserDao

package com.peng.springdao;
public interface IUserDao {
public void insert(User user);
public User find(Integer id);
}


然后设计操作类UserDao:

package com.peng.springdao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;


//DAO的实现,必须要有一个setDataSource()的方法,这样才能出入DataSource。

public class UserDao implements IUserDao {
private DataSource dataSource;
public User find(Integer id) {
   Connection conn = null;
   Statement stmt = null;
   ResultSet rs = null;
   try {
    conn = dataSource.getConnection();
    stmt = conn.createStatement();
    String sql = "select * from myspringuser where uid='" + id + "'";
    rs = stmt.executeQuery(sql);
   } catch (Exception e) {
    e.printStackTrace();
    return null;
   }
   User user = new User();
   user.setId(id);
   try {
    while (rs.next()) {
     String username = rs.getString("username");
     int age = rs.getInt("userage");
     user.setAge(age);
     user.setName(username);
    }
   } catch (SQLException e) {
    e.printStackTrace();
    return null;
   } finally {
    if (stmt != null) {
     try {
      stmt.close();
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   }
   return user;
}
public void insert(User user) {
   String name = user.getName();
   int age = user.getAge().intValue();
   Connection conn = null;
   Statement stmt = null;
   try {
    conn = dataSource.getConnection();
    stmt = conn.createStatement();
    String sql = "insert into myspringuser (username, userage)"
      + "values('" + name + "'," + age + ")";
    stmt.execute(sql);
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    if (stmt != null) {
     try {
      stmt.close();
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
    if (conn != null) {
     try {
      conn.close();
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   }
}
public DataSource getDataSource() {
   return dataSource;
}

public void setDataSource(DataSource dataSource) {
   this.dataSource = dataSource;
}
}


第四步:配置文件bean.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "../resources/spring-beans-2.0.dtd" >
<beans>
<bean id="dataSource"   class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName">
    <value>com.mysql.jdbc.Driver</value>
   </property>
   <property name="url">
    <value>jdbc:mysql://localhost:3306/myproject</value>
   </property>
   <property name="username">
    <value>root</value>
   </property>
   <property name="password">
    <value>root</value>
   </property>
</bean>
<bean id="userDao" class="com.peng.springdao.UserDao">
   <property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
第五步:测试类

package com.peng.springdao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDaoTest {
public static void main(String[] args) {
   User user = new User();
   user.setAge(23);
   user.setName("jim");
   ApplicationContext context = new ClassPathXmlApplicationContext(
     "bean.xml");
   IUserDao userDao = (IUserDao) context.getBean("userDao");
   userDao.insert(user);
   User user2 = userDao.find(2);//在此之前已经插入id为2的数据
   if (user2 != null)
    System.out.println("uid:2" + " ,name:" + user2.getName());

}
}


你可能感兴趣的:(DAO,spring,sql,mysql,bean)