JdbcTemplate 方法使用

作者QQ:1095737364    QQ群:123300273     欢迎加入!

execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;

update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;

batchUpdate方法用于执行批处理相关语句;

query方法及queryForXXX方法:用于执行查询相关语句;

call方法:用于执行存储过程、函数相关语句。

1.添加一条语句:

String sql = "INSERT INTO emailconfig ( smtp , smtpport,) VALUES('"+emailConf.getSmtp()+"','"+emailConf.getSmtpport()+"')";//使用拼接的方式传递参数
this.jdbcTemplate.update(sql);

2.修改一条语句:

String sql="update temporary_email_conf set IsProcess=1 where emailName=? AND IsProcess=0";
this.jdbcTemplate.update(sql,new Object[]{emailConf.getSername()});//使用Object 数组来传递参数

3.删除一条语句:

String sql="delete FROM emailconfig WHERE emailsuffix ='"+emailConf.getEmailsuffix()+"'"
this.jdbcTemplate.update(sql);

4.查询:

String sql="select * from temporary_email_conf where (IsSuccess=0 OR pop3='' OR pop3 is null OR imap='' OR imap is null) and IsProcess=0 ";
return this.jdbcTemplate.query(sql,new BeanPropertyRowMapper<TemporaryEmailConf>(TemporaryEmailConf.class));//查询一个list的集合对象

String sql="select * from temporary_email_conf where emailName=? and IsProcess=0 ";
return this.jdbcTemplate.queryForObject(sql,new Object[]{emailName},new BeanPropertyRowMapper<TemporaryEmailConf>(TemporaryEmailConf.class));//查询单个对象

String sql="select count(*) from emailconfig WHERE emailsuffix='"+emailConf.getEmailsuffix()+"'";
return jdbcTemplate.queryForInt(sql);//查询整形的数字

  



原文地址:https://www.cnblogs.com/yysbolg/p/6699484.html