我想通过这种查询将表中的数据插入另一个
insert into mytable1 ( c1, c2 ) SELECT b1,b2 from mytable2;
我使用spring并且有一个使用
的DAO类的工作示例 package gp.dao;
import gp.model.Medic;
import gp.model.MvtMedic;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedProperty;
import javax.sql.DataSource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import gp.interfaces.InterfaceDao;
public class MvtMedicDaoImpl implements InterfaceDao, Serializable {
private static final long serialVersionUID = 1L;
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void insert(Object obj) {
// TODO Auto-generated method stub
MvtMedic mvtMedic = (MvtMedic) obj;
String sql = "INSERT INTO mvtstock ( codeproduit, typemvt, quantite,"+
"date_arrivee, date_expir, fournisseur, numbco, prix_unit)"+
" VALUES (?,?,?,?,?,?,?,?)";
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update(sql, new Object[]{ mvtMedic.getCodeproduit(),"E",mvtMedic.getQuantite(),
mvtMedic.getDate_arrivee(), mvtMedic.getDate_expir(), mvtMedic.getFournisseur(), mvtMedic.getNumbco(),
mvtMedic.getPrix_unit()});
}
@Override
public void delete(Object obj) {
// TODO Auto-generated method stub
}
@Override
public Object findById(Object ref) {
// TODO Auto-generated method stub
return null;
}
@Override
public void modify(Object obj) {
// TODO Auto-generated method stub
}
@Override
public List<?> execRequete(String sql) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
@SuppressWarnings("unchecked")
List<MvtMedic> listMvtMed = jdbcTemplate.query(sql, new BeanPropertyRowMapper(MvtMedic.class));
return listMvtMed;
}
@Override
public List<MvtMedic> findAll() {
// TODO Auto-generated method stub
String sql = " SELECT m.codeproduit as codeproduit ,p.libelle as libelle,m.quantite as quantite,"
+ "coalesce(m.prix_unit, 0) as prix_unit,coalesce(m.prix_total, 0) as prix_total,m.fournisseur as fournisseur,m.numbco as numbco,"
+ "m.date_arrivee as date_arrivee,m.date_expir as date_expir"
+ " FROM mvtstock m LEFT JOIN stockpharm p ON m.codeproduit=p.codeproduit where typemvt='E'"
+ "order by m.date_arrivee desc" ;
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
@SuppressWarnings("unchecked")
List<MvtMedic> listMvtMed = jdbcTemplate.query(sql, new BeanPropertyRowMapper(MvtMedic.class));
return listMvtMed;
}
@Override
public Object getLastId() {
// TODO Auto-generated method stub
return null;
}
}
现在我的问题是如何使用insert方法来提供选择
(从mytable2插入mytable1(c1,c2)SELECT b1,b2;) 结果值为jdbcTemplate.update(...
public void insert(Object obj) {
// TODO Auto-generated method stub
MvtMedic mvtMedic = (MvtMedic) obj;
String sql = "INSERT INTO mvtstock ( codeproduit, typemvt, quantite,"+
"date_arrivee, date_expir, fournisseur, numbco, prix_unit)"+
" VALUES (?,?,?,?,?,?,?,?)";
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update(sql, new Object[]{ // DATAS of The selec});
}
答案 0 :(得分:0)
jdbcTemplate会执行您要执行的任何操作。所以你可以这样做:
@Override
public void insert(Object obj) {
String sql = "insert into mytable1 ( c1, c2 ) SELECT b1,b2 from mytable2";
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update(sql);
}
PS:最好将JdbcTemplate bean创建为单例并将其注入dao.Dont&#39;每次创建一个新的。