发表于: 2017-05-20 21:14:18

1 1022


今日完成:

编写了Spirng 结合JDBC


实体类

package com.JDBC;

/**
* Created by yangshun on 2017/5/20.
*/
public class Student {
private String name;
   private Integer id;
   private Integer age;

   public String getName() {
return name;
   }

public void setName(String name) {
this.name = name;
   }

public Integer getId() {
return id;
   }

public void setId(Integer id) {
this.id = id;
   }

public Integer getAge() {
return age;
   }

public void setAge(Integer age) {
this.age = age;
   }
}

接口

package com.JDBC;

import javax.sql.DataSource;
import java.util.List;

/**
* Created by yangshun on 2017/5/20.
*/
public interface StudentDAO {

public void setDataSource(DataSource ds);
   /**
    * This is the method to be used to create
    * a record in the Student table.
    */
   public void create(String name, Integer age);
   /**
    * This is the method to be used to list down
    * a record from the Student table corresponding
    * to a passed student id.
    */
   public Student getStudent(Integer id);
   /**
    * This is the method to be used to list down
    * all the records from the Student table.
    */
   public List<Student> listStudents();
   /**
    * This is the method to be used to delete
    * a record from the Student table corresponding
    * to a passed student id.
    */
   public void delete(Integer id);
   /**
    * This is the method to be used to update
    * a record into the Student table.
    */
   public void update(Integer id, Integer age);
}

实现类

package com.JDBC;

import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.List;

/**
* Created by yangshun on 2017/5/20.
*/
public class StudentDAOImpl implements StudentDAO {
private DataSource dataSource;
   private JdbcTemplate jdbcTemplateObject;
   public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
       this.jdbcTemplateObject = new JdbcTemplate(dataSource);
   }
public void create(String name, Integer age) {
String SQL = "insert into Student (name, age) values (?, ?)";
       jdbcTemplateObject.update( SQL, name, age);
       System.out.println("Created Record Name = " + name + " Age = " + age);
       return;
   }
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, Integer age){
String SQL = "update Student set age = ? where id = ?";
       jdbcTemplateObject.update(SQL, age, id);
       System.out.println("Updated Record with ID = " + id );
       return;
   }
}


package com.JDBC;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
* Created by yangshun on 2017/5/20.
*/
public class StudentMapper implements RowMapper<Student> {


public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
       Student student =new Student();
       student.setAge(rs.getInt("age"));
       student.setId(rs.getInt("id"));
       student.setName(rs.getString("name"));
       return student;
   }

}




package com.JDBC;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
* Created by yangshun on 2017/5/20.
*/
public class MainApp {
public static void main(String[] args) {
       ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
       StudentDAOImpl studentJDBCTemplate =
               (StudentDAOImpl)context.getBean("studentDAOImpl");
       System.out.println("------Records Creation--------" );
       studentJDBCTemplate.create("Zara", 11);
       studentJDBCTemplate.create("Nuha", 2);
       studentJDBCTemplate.create("Ayan", 15);
       System.out.println("------Listing Multiple Records--------" );
       List<Student> students = studentJDBCTemplate.listStudents();
       for (Student record : students) {
           System.out.print("ID : " + record.getId() );
           System.out.print(", Name : " + record.getName() );
           System.out.println(", Age : " + record.getAge());
       }
       System.out.println("----Updating Record with ID = 2 -----" );
       studentJDBCTemplate.update(2, 20);
       System.out.println("----Listing Record with ID = 2 -----" );
       Student student = studentJDBCTemplate.getStudent(2);
       System.out.print("ID : " + student.getId() );
       System.out.print(", Name : " + student.getName() );
       System.out.println(", Age : " + student.getAge());
   }

}

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 ">

   <!-- Initialization for data source -->
   <bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost:3306/demo?useSSL=true"/>
       <property name="username" value="root"/>
       <property name="password" value="ysmh2008"/>
   </bean>

   <!-- Definition for studentJDBCTemplate bean -->
   <bean id="studentDAOImpl"
         class="com.JDBC.StudentDAOImpl">
       <property name="dataSource"  ref="dataSource" />
   </bean>

</beans>

运行结果

明日计划:

编写Spring MVC




收获:

了解Spring JDBC的模板


返回列表 返回列表
评论

    分享到