• Spring的jdbcTemplate查询执行原生sql


    摘自:http://www.cnblogs.com/blog411032/p/5949670.html

    在spring与hibernate整合时进行数据库检索,执行原生sql:

      

    复制代码
    public AppointmentEvaluateVo searchMyfeedbackDetail(String accountId, String fbId) {
    
            String sql = "select ae.id as fbId ,ae.app_id as appId, a.app_no as appNo,"
                    + "    si.service_item AS serItem,a.app_service_time as feedTime,ae.remark as"
                    + " feedCon, cou.attitudeScore AS attitudeScore,cou.qualityScore AS qualityScore,"
                    + "cou.prescriptionScore AS prescriptionScore from appointment_evaluate ae LEFT JOIN"
                    + " appointment a ON ae.app_id=a.id LEFT JOIN organization_service_item     osi  "
                    + "ON a.item_id=osi.id LEFT JOIN service_item si ON osi.service_item_id=si.id "
                    + "LEFT JOIN  ( SELECT _a.item_id, count(_a.id)    AS orderNum,  _ae.attitude as "
                    + "attitudeScore, _ae.quality as qualityScore, _ae.prescription as prescriptionScore  "
                    + " FROM appointment _a RIGHT JOIN appointment_evaluate _ae ON `_a`.id = `_ae`.app_id "
                    + " GROUP BY _a.item_id) cou ON osi.id = cou.item_id where 1=1 and     ae.creator='" + accountId
                    + "'  and ae.id='" + fbId + "';";
    
            List rows = jdbcTemplate.queryForList(sql);
            AppointmentEvaluateVo av = new AppointmentEvaluateVo();
            Iterator ite = rows.iterator();
            while (ite.hasNext()) {
                Map avMap = (Map) ite.next();
                av.setFbId(avMap.get("fbId").toString());
                av.setAppId(avMap.get("appId").toString());
                av.setAppNo(avMap.get("appNo").toString());
                av.setSerItem(avMap.get("serItem").toString());
                av.setFeedTime(avMap.get("feedTime").toString());
                av.setFeedCon(avMap.get("feedCon").toString());
                av.setTotScore(avMap.get("prescriptionScore").toString());
                av.setTquaScore(avMap.get("qulityScore").toString());
                av.setSerScore(avMap.get("attitudeScore").toString());
            }
            return av;
        }
    复制代码

      第二种:

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public Student findStudentById(int id){
        String sql = "select * from tb_student where id=?";
        final Student student = new Student();
        jdbcTemplate.query(sql,new Object[]{id},new RowCallbackHandler(){
            public void processRow(ReultSet resultSet) throws SQLException{
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
            }
        });
        return student;
    }

      

    增删改查简单示例:

    复制代码
    package com.demo.manager;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    import java.util.UUID;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.RowMapper;
    import org.springframework.stereotype.Repository;
    import org.springframework.util.StringUtils;
    
    import com.demo.pojo.Account;
    
    @Repository
    public class AccountManager {
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        /**
         * 添加
         * 
         * @param ac
         */
        public void addAccount(Account account) {
    
            jdbcTemplate.update("insert into tb_account(id,name,address,age) values (?,?,?,?)",
                    new Object[] { StringUtils.hasText(account.getId()) ? account.getId() : UUID.randomUUID(),
                            StringUtils.hasText(account.getName()) ? account.getName() : "",
                            StringUtils.hasText(account.getAddress()) ? account.getAddress() : "", account.getAge() });
        }
    
        /**
         * 根据id获取账户信息
         * @param a
         * @return
         */
        public Account getAccount(Account a) {
            Account account = jdbcTemplate.queryForObject("select * from tb_account where id=?", new Object[] { a.getId() },
                    new BeanPropertyRowMapper<>(Account.class));
            return account;
        }
    
        public List<Account> getAll() {
            List<Account> accounts = jdbcTemplate.query("select * from tb_account",new RowMapper<Account>() {
    
                @Override
                public Account mapRow(ResultSet rs, int num) throws SQLException {
                    
                    Account account = new Account();
                    account.setId(rs.getString(1));
                    account.setName(rs.getString(2));
                    account.setAddress(rs.getString(3));
                    account.setAge(rs.getInt(4));
                    return account;
                }
            });
            return accounts;
        }
        
        /**
         * 删除
         * 
         * @param ac
         */
        public void deleteAccount(Account account) {
    
            jdbcTemplate.update("delete from tb_account where id=?",
                    new Object[] { account.getId() });
        }
        
        /**
         * 更新
         * 
         * @param ac
         */
        public void updateAccount(Account account) {
    
            jdbcTemplate.update("update  tb_account set name=?,address=? where id=?",
                    new Object[] { account.getName(),account.getAddress(),account.getId() });
        }
    }
    复制代码
  • 相关阅读:
    【洛谷3778】[APIO2017] 商旅(分数规划+Floyd)
    【AT4114】[ARC095D] Permutation Tree(简单题)
    【AT4352】[ARC101C] Ribbons on Tree(容斥+DP)
    【AT4169】[ARC100D] Colorful Sequences(DP)
    【洛谷4581】[BJOI2014] 想法(随机算法)
    【洛谷5659】[CSP-S2019] 树上的数(思维)
    【AT4439】[AGC028E] High Elements(线段树)
    【CF590E】Birthday(AC自动机+二分图匹配)
    【洛谷4298】[CTSC2008] 祭祀(Dilworth定理+二分图匹配)
    【洛谷3774】[CTSC2017] 最长上升子序列(杨表)
  • 原文地址:https://www.cnblogs.com/GaoAnLee/p/7229359.html
Copyright © 2020-2023  润新知