使用hibernate在Spring mvc中存储过程调用?

时间:2017-04-05 06:53:22

标签: java hibernate spring-mvc jdbc

使用hibernate

在Spring mvc中存储过程

我在mysql中创建了程序

CREATE DEFINER=`skip-grants user`@`skip-grants host` PROCEDURE `getSmsLog`(
IN in_logId INTEGER,
OUT out_machineNo VARCHAR(20),
OUT out_cardNo  VARCHAR(20)
)
BEGIN
SELECT machineNo, cardNo
   INTO out_machineNo, out_cardNo
   FROM smslog_tbl where logId=in_logId;
END

这是我的模型类,如gatter和setter方法

package com.smart.model;

import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;

/**
 * The persistent class for the smslog_tbl database table.
 * 
 */
@Entity
@Table(name = "smslog_tbl")
@NamedQuery(name = "SmslogTbl.findAll", query = "SELECT s FROM SmslogTbl s")
public class SmslogTbl implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private int logId;

    private String cardNo;

    private String machineNo;

    @Temporal(TemporalType.TIMESTAMP)
    private Date sentDataTime;

    private String sms;

    @Temporal(TemporalType.TIMESTAMP)
    private Date transactionDateTime;

    public SmslogTbl() {
    }

    public SmslogTbl(int logId, String cardno, String machineNo, Date setdateTime,String sms,Date TransactiondateAndTime) {

            this.logId=logId;
            this.cardNo=cardno;
            this.machineNo=machineNo;
            this.sentDataTime=setdateTime;
            this.sms=sms;
            this.transactionDateTime=transactionDateTime;

    }

    public int getLogId() {
        return this.logId;
    }

    public void setLogId(int logId) {
        this.logId = logId;
    }

    public String getCardNo() {
        return this.cardNo;
    }

    public void setCardNo(String cardNo) {
        this.cardNo = cardNo;
    }

    public String getMachineNo() {
        return this.machineNo;
    }

    public void setMachineNo(String machineNo) {
        this.machineNo = machineNo;
    }

    public Date getSentDataTime() {
        return this.sentDataTime;
    }

    public void setSentDataTime(Date sentDataTime) {
        this.sentDataTime = sentDataTime;
    }

    public String getSms() {
        return this.sms;
    }

    public void setSms(String sms) {
        this.sms = sms;
    }

    public Date getTransactionDateTime() {
        return this.transactionDateTime;
    }

    public void setTransactionDateTime(Date transactionDateTime) {
        this.transactionDateTime = transactionDateTime;
    }

}

这是我的mapper类

package com.smart.app;

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

import org.springframework.jdbc.core.RowMapper;

import com.smart.model.SmslogTbl;

public class SmsMapper implements RowMapper<SmslogTbl> {

    @Override
    public SmslogTbl mapRow(ResultSet rs, int args) throws SQLException {

        SmslogTbl sms = new SmslogTbl();
        sms.setLogId(rs.getInt("logId"));
        sms.setMachineNo(rs.getString("machineNo"));
        sms.setCardNo(rs.getString("cardNo"));
        return sms;
    }

}                                         

这是业务逻辑

package com.smart.dao;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
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;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.apache.log4j.Logger;

import com.smart.app.SmsContants;
import com.smart.app.SmsStoreProcedure;
import com.smart.model.SmslogTbl;

@Transactional
@Repository
public class SmsLogDaoImpl implements SmsDao {

    static Logger log = Logger.getLogger(SmsLogDaoImpl.class);

    private DataSource dataSource;
    private SimpleJdbcCall jdbcCall;


    @Override
    public void setDataSource(DataSource ds) {
        this.dataSource = ds;
        //this.jdbctemplate= new JdbcTemplate(ds);
        this.jdbcCall = new SimpleJdbcCall(ds).withProcedureName("getSmsLog");
    }

    @Override
    public SmslogTbl getsmslog(Integer logid) {

        SqlParameterSource in = new MapSqlParameterSource().addValue("in_logid", logid);
        Map<String, Object> out = jdbcCall.execute(in);
        SmslogTbl smslogTbl = new SmslogTbl();  
        smslogTbl.setLogId(logid);
        smslogTbl.setMachineNo((String) out.get("out_machineNo"));
        smslogTbl.setCardNo((String) out.get("out_cardNo"));

        return smslogTbl;
    }

    /*@SuppressWarnings("rawtypes")
    @Override
    public List getsmslog(int logid) {


        List result=null;
        Map data=smsStoreprocedure.getSms(logid);
        result=(List)data.get(SmsContants.RESULT_LIST);

        return result;
    }*/

}

### my main classs ###

package com.smart.app;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.smart.dao.SmsLogDaoImpl;
import com.smart.model.SmslogTbl;

public class smsMain {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/java/com/smart/app/applicationContext.xml");
        SmsLogDaoImpl smslog = (SmsLogDaoImpl) context.getBean("SmsLogDaoImpl");

        System.out.println("----Listing Record with ID = 2 -----");

        SmslogTbl smslogTbl = (SmslogTbl) smslog.getsmslog(1);
        System.out.print("ID : " + smslogTbl.getLogId());
        System.out.print(", machine no : " + smslogTbl.getMachineNo());
        System.out.println(",card no : " + smslogTbl.getCardNo());
    }
}

用于设置数据库和bean的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" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:annotation-config />

    <context:component-scan base-package="demo.dao">
    </context:component-scan>
    <!-- Step 1: Define the data source -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/studentattendencedb" />
        <property name="username" value="root" />
        <property name="password" value="1234" />
    </bean>
<!-- 
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>


    <bean id="carsDAO" class="demo.dao.cars.CarsDAOImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>

 -->    <!-- <bean id="studentJDBCTemplate" class="demo.dao.student.StudentJDBCTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean> 
     -->
    <bean id="SmsLogDaoImpl" class="com.smart.dao.SmsLogDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean> 


    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplateObject">
        <property name="dataSource" ref="dataSource">
        </property>
    </bean>
<!--    
    <bean id="RoleDaoImpl" class="demo.dao.role.RoleDaoImpl1">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="StandardDaoImpl" class="demo.dao.standard.StandardDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean> -->

</beans>


#  output are  #
### output  are display in console  ###


----Listing Record with ID = 2 -----
ID : 1, machine no : null,card no : null

0 个答案:

没有答案