码迷,mamicode.com
首页 > 数据库 > 详细

实际开发中Dao层使用JdbcTemplate

时间:2020-02-09 09:45:12      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:over   this   pack   开发   使用   void   mon   hang   dao层   

实体类

package com.itheima.domain;

import java.io.Serializable;

/**
* @Author: lijiahao
* @Description: 账户的实体类
* @Data: Create in 1:32 2020/2/9
* @Modified By:
*/
public class Account implements Serializable {

private Integer id;
private String name;
private Float money;

@Override
public String toString() {
return "account{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
", money=" + money +
‘}‘;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Float getMoney() {
return money;
}

public void setMoney(Float money) {
this.money = money;
}
}

Dao接口
package com.itheima.Dao;

import com.itheima.domain.Account;

/**
* @Author: lijiahao
* @Description: 帐户的持久性接口
* @Data: Create in 2:30 2020/2/9
* @Modified By:
*/
public interface IAccountDao {

/**
* @Author Lijiahao
* @Description 根据id查帐户
* @Date 2:31 2020/2/9
* @Param [id]
* @return com.itheima.domain.Account
**/
Account findAccountById(Integer id);
//根据名字查帐户
Account findAccountByName(String name);
//更新帐户
void updateAccount(Account account);
}

Dao具体实现类
package com.itheima.Dao.impl;

import com.itheima.Dao.IAccountDao;
import com.itheima.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
* @Author: lijiahao
* @Description:
* @Data: Create in 2:32 2020/2/9
* @Modified By:
*/
public class AccountDaoImpl implements IAccountDao {

private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

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

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

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

测试
package com.itheima.jdbctemplate;

import com.itheima.Dao.IAccountDao;
import com.itheima.domain.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
* @Author: lijiahao
* @Description: jdbctemplate的最基本用法:CRUD
* @Data: Create in 1:34 2020/2/9
* @Modified By:
*/
public class JdbcTemplateDemo4 {
public static void main(String[] args) {

//1.获取容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
//2.获取对象
IAccountDao accountDao = (IAccountDao) applicationContext.getBean("accountDao");

Account account = accountDao.findAccountById(1);
System.out.println(account);
account.setMoney(10000f);
accountDao.updateAccount(account);
}
}

配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">

<!--配置账户的持久层-->
<bean id="accountDao" class="com.itheima.Dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--配置JdbTemplate-->
<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.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/eesy?serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8&amp;useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
</beans>

实际开发中Dao层使用JdbcTemplate

标签:over   this   pack   开发   使用   void   mon   hang   dao层   

原文地址:https://www.cnblogs.com/lijiahaoAA/p/12286069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!