MySQL|使用Java Spring消费MySQL中的数据库存储过程
进行这个练习的一些先决条件。
创建一张student数据库表:
CREATE TABLE Student( ID INT NOT NULL AUTO_INCREMENT, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL, PRIMARY KEY (ID));
创建一个存储过程:
DELIMITER $$DROP PROCEDURE IF EXISTS `TEST`.`getRecord` $$CREATE PROCEDURE `TEST`.`getRecord` (IN in_id INTEGER,OUT out_name VARCHAR(20),OUT out_age INTEGER)BEGIN SELECT name, age INTO out_name, out_age FROM Student where id = in_id;END $$DELIMITER ;
创建一个数据访问对象接口文件 StudentDAO.java:
import java.util.List;import javax.sql.DataSource;public interface StudentDAO { /*** This is the method to be used to initialize * database resources ie. connection. */ 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
创建一个POJO Student.java:
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; }}
StudentMapper.java,负责将mySQL的数据映射成Java POJO Student对象实例:
import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;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.setName(rs.getString("name")); student.setAge(rs.getInt("age")); return student; }}
StudentJDBCTemplate.java,实现了DAO 接口 StudentDAO:
import java.util.Map;import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;import org.springframework.jdbc.core.namedparam.SqlParameterSource;import org.springframework.jdbc.core.simple.SimpleJdbcCall;public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private SimpleJdbcCall jdbcCall; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcCall = new SimpleJdbcCall(dataSource). withProcedureName("getRecord"); } public void create(String name, Integer age) { JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource); 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) { SqlParameterSource in = new MapSqlParameterSource(). addValue("in_id", id); Map<String, Object> out = jdbcCall.execute(in); Student student = new Student(); student.setId(id); student.setName((String) out.get("out_name")); student.setAge((Integer) out.get("out_age")); return student; } public List
MainApp.java:
import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.tutorialspoint.StudentJDBCTemplate;public class MainApp { 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--------" ); List
推荐阅读
- 电子身份证你用过没有?开通两月 4.6万余人次重庆市民使用登记住宿|电子身份证你用过没有?开通两月 4.6万余人次重庆市民使用登记住宿
- 减少门诊就医负担、个人账户使用范围扩大……你关心的医保改革都在这
- 优惠|全面实施电子客票后 购买使用学生票有什么变化?
- TSMC|台积电和Graphcore准备使用3纳米工艺制造AI加速芯片
- 安全|开车还在玩手机?重庆严查驾车时使用手机!监控自动抓拍还会曝光
- IT|英国拟允许临时授权紧急使用新冠疫苗
- 疫苗|英国拟允许临时授权紧急使用新冠疫苗
- 法律|这个买卖不敢做?小心五年内名下手机卡、银行卡无法正常使用
- 女性|迷你世界:女性玩家使用的装扮代表着性格?看看你属于哪一种?
- 个人征信|正常使用花呗会影响个人信用?关于个人征信的这些谣言,你中招了吗?