spring基础入门-综合实践(基于注解annotation)

spring基础入门-综合实践(annotation纯注解)

项目目录结构
在这里插入图片描述

建立一个普通的动态web项目,并导入所需的jar包,编写文件

IAccountDao.java

package com.zh.dao;

import com.zh.entity.Account;

public interface IAccountDao {

	/**
	 * 更新账户
	 * @param account 账户
	 */
	public abstract void updateAccount(Account account);
	
	/**
	 * 查找账户
	 * @param name 姓名
	 * @return 返回账户
	 */
	public abstract Account findAccount(String name);
	
}

AccountDaoImpl.java

package com.zh.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.zh.dao.IAccountDao;
import com.zh.entity.Account;

@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Account findAccount(String name) {
		List<Account> account = jdbcTemplate.query("select * from account where name=?",  new BeanPropertyRowMapper<Account>(Account.class),name);
		if(account.isEmpty()) return null;
		if(account.size()>1)throw new RuntimeException("结果集不唯一");
		return account.isEmpty() ? null : account.get(0);
	}

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

	

}

Account.java

package com.zh.entity;

public class Account {
	//账户id
	private Integer id;
	
	//姓名
	private String name; 
	
	//账户上的钱
	private float money;
	
	public Account() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Account(Integer id, String name, float money) {
		super();
		this.id = id;
		this.name = name;
		this.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;
	}
	@Override
	public String toString() {
		return "Account [id=" + id + ", name=" + name + ", money=" + money + "]";
	}

}

IAccountService.java

package com.zh.service;

import com.zh.entity.Account;

public interface IAccountService {
	
	/**
	 * 根据名字查询账户
	 * @param name 姓名
	 * @return 账户
	 */
	public Account findAccount(String name);
	
	
	/**
	 * 模拟转账
	 * @param sourceName 被转账账户(源)
	 * @param targetName 转账账户(目)
	 * @param money 转账数目
	 */
	public void accountTransfer(String sourceName, String targetName, float money);
}

AccountServiceImpl.java

package com.zh.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zh.dao.IAccountDao;
import com.zh.entity.Account;
import com.zh.service.IAccountService;

@Service("accountService")
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public class AccountServiceImpl implements IAccountService {
	
	@Autowired
	private IAccountDao accountDao;
	
	@Override
	public Account findAccount(String name) {
		Account account = accountDao.findAccount(name);
		return account;
	}

	@Override
	public void accountTransfer(String sourceName, String targetName, float money) {
		Account source = accountDao.findAccount(sourceName);
		Account target = accountDao.findAccount(targetName);
		source.setMoney(source.getMoney() - money);
		target.setMoney(target.getMoney() + money);
		accountDao.updateAccount(source);
		
		//模拟出现异常,检验事务是否被控制住(rollback)
		//int i = 1/0;
		accountDao.updateAccount(target);
	}

	
	
}

Test.java

package com.zh.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.zh.config.SpringConfiguration;
import com.zh.service.IAccountService;

public class Test {
	public static void main(String[] args) {
		/**
		 * 加载spring核心配置文件
		 */
		ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
		
		/**
		 * 获取AccountServiceImpl对象
		 * 使用接口接收,否则报异常
		 */
		IAccountService accountService = ac.getBean("accountService",IAccountService.class);
		
		//查看账户
		System.out.println(accountService.findAccount("zh"));
		System.out.println(accountService.findAccount("lisi"));
		
		//转账操作
		accountService.accountTransfer("zh", "lisi", 220f);
	}
}

去掉beans.xml与db.properties,添加一个包三个类

jdbcConfig.java

package com.zh.config;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class jdbcConfig {
	
	
	@Bean(name="jdbcTemplate")
	public JdbcTemplate createJdbcTemplate(DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}
	
	
	@Bean(name="dataSource")
	public DataSource createDataSource() {
		DriverManagerDataSource ds = new DriverManagerDataSource();
		ds.setDriverClassName("com.mysql.jdbc.Driver");
		ds.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false");
		ds.setUsername("root");
		ds.setPassword("9131101");
		return ds;
	}
	
}

TransactionManager.java

package com.zh.config;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

public class TransactionManager {
	@Bean(name="transactionManager")
	public PlatformTransactionManager createTransactionManager(DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}
}

SpringConfiguration.java

package com.zh.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * Spring配置类(当beans.xml用)
 * @author Administrator
 *
 */
@Configuration
@ComponentScan("com.zh")
@Import({jdbcConfig.class,TransactionManager.class})
@EnableTransactionManagement
public class SpringConfiguration {

}

运行Test.java效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值