zoukankan      html  css  js  c++  java
  • Spring之jdbctemplate

    一个持久层接口

    public interface IAccountDao {

    /**
    * 根据Id查询账户
    * @param accountId
    * @return
    */
    Account findAccountById(Integer accountId);

    /**
    * 根据名称查询账户
    * @param accountName
    * @return
    */
    Account findAccountByName(String accountName);

    /**
    * 更新账户
    * @param account
    */
    void updateAccount(Account account);
    }


    一个持久层实现类

    public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {

    @Override
    public Account findAccountById(Integer accountId) {
    List<Account> accounts = super.getJdbcTemplate().query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
    return accounts.isEmpty()?null:accounts.get(0);
    }

    @Override
    public Account findAccountByName(String accountName) {
    List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);
    if(accounts.isEmpty()){
    return null;
    }
    if(accounts.size()>1){
    throw new RuntimeException("结果集不唯一");
    }
    return accounts.get(0);
    }

    @Override
    public void updateAccount(Account account) {
    super.getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
    }
    }

    此时使用bean.xml 配置IOC和jdbctemplate

    <!-- 配置账户的持久层-->
    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
    <!--<property name="jdbcTemplate" ref="jdbcTemplate"></property>-->
    <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/eesy"></property>
    <property name="username" value="root"></property>
    <property name="password" value="1234"></property>
    </bean>
    </beans>


    根据xml获取容器得到JDBCTemplate
    public class JdbcTemplateDemo2 {

    public static void main(String[] args) {
    //1.获取容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    //2.获取对象
    JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class);
    //3.执行操作
    jt.execute("insert into account(name,money)values('ddd',2222)");

    }

    sql语句多种形式
            jt.update("insert into account(name,money)values(?,?)","eee",3333f);
    // 更新
    jt.update("update account set name=?,money=? where id=?","test",4567,7);
    // 删除
    jt.update("delete from account where id=?",8);
    List<Account> accounts = jt.query("select * from account where money > ?",new BeanPropertyRowMapper<Account>(Account.class),1000f);
    返回Account类型的List 通过
    new BeanPropertyRowMapper<Account>(Account.class) 设置返回类型

    Long count = jt.queryForObject("select count(*) from account where money > ?",Long.class,1000f);


    实现类开启注解配置
    @Repository
    public class AccountDaoImpl2 implements IAccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public Account findAccountById(Integer accountId) {
    List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
    return accounts.isEmpty()?null:accounts.get(0);
    }
    }

     */
    public class JdbcTemplateDemo4 {

    public static void main(String[] args) {
    //1.获取容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    //2.获取对象
    IAccountDao accountDao = ac.getBean("accountDao",IAccountDao.class);

    Account account = accountDao.findAccountById(1);
    System.out.println(account);

    account.setMoney(30000f);
    accountDao.updateAccount(account);
    }
    }
    成功运行
  • 相关阅读:
    会议10
    会议09
    回忆8
    会议07
    团队报告三
    深圳展会问题关注
    移动电源频率设置
    安卓java设置字体颜色
    安卓取消默认的标题栏方法
    安卓,按钮清晰的事件注册写法
  • 原文地址:https://www.cnblogs.com/yitaqiotouto/p/12602620.html
Copyright © 2011-2022 走看看