本文共 6994 字,大约阅读时间需要 23 分钟。
以下内容引用自:
实现前提:
1、在MySql上新建数据库并导入相关数据,脚本如下:
---- 数据库: `test`---- ------------------------------------------------------------ 表的结构 `student`--CREATE TABLE IF NOT EXISTS `student` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `NAME` varchar(20) NOT NULL, `AGE` int(11) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;---- 转存表中的数据 `student`--INSERT INTO `student` (`ID`, `NAME`, `AGE`) VALUES(1, 'Zara', 11),(2, 'Nuha', 2),(3, 'Ayan', 15);
2、由于是操作MySql,所以还需要mysql-connector-java.jar包:
mysql mysql-connector-java 6.0.6
实践例子:
pom.xml:
4.0.0 com.jsoft.testspring testjdbc 0.0.1-SNAPSHOT jar testjdbc http://maven.apache.org UTF-8 junit junit 3.8.1 test org.springframework spring-core 4.1.4.RELEASE org.springframework spring-context 4.1.4.RELEASE org.springframework spring-jdbc 4.1.4.RELEASE org.springframework spring-tx 4.1.4.RELEASE mysql mysql-connector-java 6.0.6
Student.java:
package com.jsoft.testspring.testjdbc;public class Student { private Integer age; private String name; private Integer id; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public Integer getId() { return id; }}
StudentDAO.java:
package com.jsoft.testspring.testjdbc;import java.util.List;import javax.sql.DataSource;public interface StudentDAO { public void setDataSource(DataSource ds); public void create(String name, Integer age); public Student getStudent(Integer id); public ListlistStudents(); public void delete(Integer id); public void update(Integer id, Integer age);}
StudentMapper.java:
package com.jsoft.testspring.testjdbc;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;public class StudentMapper implements RowMapper{ @Override public Student mapRow(ResultSet rs, int arg1) throws SQLException { // TODO Auto-generated method stub Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setAge(rs.getInt("age")); return student; }}
提示:此类的作用是通过一行数据返回一个Student的对象。
StudentJDBCTemplate.java:
package com.jsoft.testspring.testjdbc;import java.util.List;import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; @Override public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); } @Override 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; } @Override public Student getStudent(Integer id) { String SQL = "select * from Student where id = ?"; Student student = jdbcTemplateObject.queryForObject(SQL, new Object[] { id }, new StudentMapper()); return student; } @Override public ListlistStudents() { String SQL = "select * from Student"; List students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } @Override 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; } @Override 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; }}
提示:通过继承DAO接口实现相关方法。
beans.xml:
App.java:
package com.jsoft.testspring.testjdbc;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Hello world! * */public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate) context.getBean("studentJDBCTemplate"); System.out.println("------Records Creation--------"); studentJDBCTemplate.create("Zara", 11); studentJDBCTemplate.create("Nuha", 2); studentJDBCTemplate.create("Ayan", 15); System.out.println("------Listing Multiple Records--------"); Liststudents = 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()); }}
测试结果:
测试工程:
==>如有问题,请联系我:easonjim#163.com,或者下方发表评论。<==转载地址:http://evcwo.baihongyu.com/