首页 > 代码库 > Spring JDBC Example

Spring JDBC Example

1.准备数据库(sqlserver2008)

打开SQL Server 配置管理器进行网络配置

技术分享

 

2.重启系统服务

技术分享

3.创建数据库及表

技术分享

4.编写模型及业务代码

技术分享
public class Student {
    public int ID;
    public String UserName;
    public String Password;
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public String getUserName() {
        return UserName;
    }
    public void setUserName(String userName) {
        UserName = userName;
    }
    public String getPassword() {
        return Password;
    }
    public void setPassword(String password) {
        Password = password;
    }
}
model
技术分享
public interface StudentDAO {
    public void setDataSource(DataSource ds);
    public Student getStudent(Integer id);
    public List<Student> listStudents();
    void delete(Integer id) ;
    void update(Integer id, String password);
}


public class StudentDaoImp implements StudentDAO {
       private DataSource dataSource;
       private JdbcTemplate jdbcTemplateObject;
       
       public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;
          this.jdbcTemplateObject = new JdbcTemplate(dataSource);
       }

       public Student getStudent(Integer id) {
          String SQL = "select * from Student where id = ?";
          Student student = jdbcTemplateObject.queryForObject(SQL, 
             new Object[]{id}, new StudentMapper());
          
          return student;
       }
       public List<Student> listStudents() {
          String SQL = "select * from Student";
          List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper());
          return students;
       }
       public void delete(Integer id) {
          String SQL = "delete from Student where id = ?";
          jdbcTemplateObject.update(SQL, id);
          System.out.println("Deleted Record with ID = " + id );
          return;
       }
       public void update(Integer id, String password){
          String SQL = "update Student set Password = ? where id = ?";
          jdbcTemplateObject.update(SQL, password, id);
          System.out.println("Updated Record with ID = " + id );
          return;
       }
    }
dao
技术分享
public class StudentMapper implements RowMapper<Student> {
   public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
      Student student = new Student();
      student.setID(rs.getInt("id"));
      student.setUserName(rs.getString("UserName"));
      student.setPassword(rs.getString("Password"));         
      return student;
   }
}
mapper

5.配置spring容器

技术分享
<?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">

<bean id = "dataSource" 
   class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name = "driverClassName" value = "com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
   <property name = "url" value = "jdbc:sqlserver://localhost:1433;DatabaseName=Qxun"/>
   <property name = "username" value = "sa"/>
   <property name = "password" value = "6665508a"/>
</bean>
<bean id = "studentDaoImp" 
      class = "ttyouni.dao.StudentDaoImp">
      <property name = "dataSource" ref = "dataSource" />    
   </bean>
</beans>
View Code

6.调用层

技术分享
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        StudentDaoImp studentJDBCTemplate = 
                 (StudentDaoImp)context.getBean("studentDaoImp");
        List<Student> list=studentJDBCTemplate.listStudents();
View Code

 

Spring JDBC Example