## 代码
~~~
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.10.10:3306/hibernate
jdbc.username=homestead
jdbc.password=secret
~~~
~~~
package com.like.springconfig;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
@ComponentScan(basePackages = "com.like")
@EnableTransactionManagement // <tx:annotation-driven transaction-manager="transactionManager"/>
public class SpringConfig
{
//创建C3P0
@Bean(name = "c3p0")
public DataSource createDataSourceC3p0() throws Exception
{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://192.168.10.10:3306/hibernate");
dataSource.setUser("homestead");
dataSource.setPassword("secret");
return dataSource;
}
@Bean(name = "jdbcTemplate")
public JdbcTemplate createJdbcTemplate(@Qualifier("c3p0") DataSource dataSource)
{
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
@Bean(name = "transactionManager")
public DataSourceTransactionManager createTransactionManager(@Qualifier("c3p0") DataSource dataSource)
{
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
}
~~~
~~~
package com.like.dao;
public interface TransferDao
{
void toMoney(String toUser, Double money);
void inMoney(String inUser, Double money);
}
~~~
~~~
package com.like.daoImpl;
import com.like.dao.TransferDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository("transferDao")
public class TransferDaoImpl implements TransferDao
{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void toMoney(String toUser, Double money)
{
String sql = "update account set money = money + ? where username = ?";
jdbcTemplate.update(sql, money, toUser);
}
@Override
public void inMoney(String inUser, Double money)
{
String sql = "update account set money = money - ? where username = ?";
jdbcTemplate.update(sql, money, inUser);
}
}
~~~
~~~
package com.like.service;
public interface TransferService
{
void transfer(String toUser, String inUser, Double money);
}
~~~
~~~
package com.like.serviceImpl;
import com.like.dao.TransferDao;
import com.like.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("transferService")
public class TransferServiceImpl implements TransferService
{
@Autowired
private TransferDao transferDao;
@Override
@Transactional
public void transfer(String toUser, String inUser, Double money)
{
//加钱
transferDao.toMoney(toUser, money);
//减钱
transferDao.inMoney(inUser, money);
}
}
~~~
~~~
import com.like.service.TransferService;
import com.like.springconfig.SpringConfig;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(classes = SpringConfig.class)
@RunWith(value = SpringJUnit4ClassRunner.class)
public class Test
{
@Autowired
private TransferService transferService;
@org.junit.Test
public void test()
{
transferService.transfer("jack", "milan", 500.0);
}
}
~~~
## JDBCTemplate方式
~~~
package com.like.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
@ComponentScan("com.like")
@EnableTransactionManagement
public class SpringConfig
{
@Bean
public DataSource createDataSource()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://192.168.10.10:3306/jdbc");
dataSource.setUsername("homestead");
dataSource.setPassword("secret");
return dataSource;
}
@Bean("jdbcTemplate")
public JdbcTemplate createJt(DataSource dataSource)
{
return new JdbcTemplate(dataSource);
}
@Bean("transactionManager")
public PlatformTransactionManager createTransactionManager(DataSource dataSource)
{
return new DataSourceTransactionManager(dataSource);
}
}
~~~
~~~
package com.like.domain;
import java.io.Serializable;
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;
}
}
~~~
~~~
package com.like.dao;
import com.like.domain.Account;
public interface IAccountDao
{
Account findAccountByName(String name);
void updateAccount(Account account);
}
~~~
~~~
package com.like.dao.impl;
import com.like.dao.IAccountDao;
import com.like.domain.Account;
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 java.util.List;
@Repository
public class AccountDaoImpl implements IAccountDao
{
@Autowired
private JdbcTemplate jt;
public Account findAccountByName(String name)
{
List<Account> accounts = jt.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), name);
if (accounts.isEmpty()) {
return null;
}
if (accounts.size() > 1) {
throw new RuntimeException("结果集不唯一");
}
return accounts.get(0);
}
public void updateAccount(Account account)
{
jt.update("update account set money = ? where name = ?", account.getMoney(), account.getName());
}
}
~~~
~~~
package com.like.service;
public interface IAccountService
{
void transfer(String sourceName,String targetName,Float money);
}
~~~
~~~
package com.like.service.impl;
import com.like.dao.IAccountDao;
import com.like.domain.Account;
import com.like.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class AccountServiceImpl implements IAccountService
{
@Autowired
private IAccountDao accountDao;
@Transactional
public void transfer(String sourceName, String targetName, Float money)
{
Account source = accountDao.findAccountByName(sourceName);
Account target = accountDao.findAccountByName(targetName);
source.setMoney(source.getMoney() - money);
target.setMoney(target.getMoney() + money);
accountDao.updateAccount(source);
int a = 1/0;
accountDao.updateAccount(target);
}
}
~~~
测试:
~~~
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
IAccountService accountService = (IAccountService) context.getBean("accountServiceImpl");
accountService.transfer("jack", "milan", 500f);
~~~
- Maven
- 概述
- 常用命令
- 生命周期
- scope详解
- maven概念模型图
- IDEA创建maven工程
- 创建maven web项目
- 冲突解决
- pom文件标签详解
- maven工程拆分与聚合的思想
- 父子工程的创建
- 工程和模块的关系以及集成和依赖的概念
- 父子工程添加依赖
- 父子工程的三种启动方式
- Struts2
- 执行流程
- 配置
- action的使用
- 获取Servlet的API
- result标签的视图配置
- struts2属性封装
- struts2模型封装
- OGNL
- 值栈(ValueStack)
- 值栈context区存数据
- 值栈root区
- struts2对el的增强
- #,%,$符号使用
- 值栈扩展
- 拦截器
- 自定义拦截器
- 方法拦截器
- 注解方式
- Hibernate
- 配置
- 简单crud
- 持久化类编写规范
- OID
- 持久化类三种状态
- 一级缓存
- 查询
- session与当前线程绑定
- hibernate一对多配置
- 冗余SQL语句的原因
- hibernate多对多配置
- 级联操作
- 对象导航查询
- JPA
- JPA单表CRUD
- JPA多种查询
- JPA一对多关系映射
- JPA一对多操作
- JPA多对多关系映射
- JPA多对多操作
- QBC查询
- 离线条件查询(DetachedCriteria)
- SpringMVC
- 环境搭建
- 常用注解
- 请求参数绑定
- 绑定基本类型和字符串
- 绑定实体类型
- 解决中文乱码
- 绑定集合类型
- 自定义类型转换器
- 获取Servlet的API
- 响应字符串
- 响应void
- 响应ModelAndView
- 响应forward和redirect
- 响应过滤静态资源
- 响应json
- 文件上传基础
- SpringMVC上传文件
- 跨服务器文件上传
- 拦截器
- SpringMVC异常
- ControllerAdvice
- SpringMVC默认处理方式
- 概述
- @ExceptionHandler
- 消息转换器
- SpringMVC跨域
- Spring
- 概述
- IoC快速入门
- ApplicationContext三个常用实现类
- beanFactory和ApplicationContext区别
- bean创建三种方式
- POJO和Javabean的区别
- bean作用范围
- bean对象生命周期
- spring的依赖注入
- DI的属性注入方式
- 常用注解
- Spring新注解
- Spring整合连接池
- Spring的IOC注解配置
- Spring完全使用注解
- Spring整合junit
- AOP
- AOP配置
- JDBCTemplate
- JDBCTemplate在IOC中使用
- JDBCTemplate的CRUD
- JDBCTemplate在dao中使用
- 声明式事务
- spring事务API
- 事务XML配置
- 事务注解配置
- 全注解事务
- Spring编程式事务
- 整合SSH(XML版本)
- 整合SSH(半XML半注解)
- Spring5新特性
- MyBatis
- 概述
- 用maven创建MyBatis
- 将数据库配置单独文件
- typeAliases标签
- MyBatis的CRUD
- MyBatis实现Dao层开发
- 使用dao和代理类的区别
- MyBatis连接池
- MyBatis事务
- MyBatis动态SQL
- 多表操作
- 多表一对一
- 多表一对多
- 多表多对多
- JNDI
- 延迟加载和立即加载
- 延迟加载
- 一级缓存
- 二级缓存
- MyBatis注解
- MyBatis注解CRUD
- 注解实体类属性和字段对应关系
- MyBatis注解一对一和一对多
- MyBatis注解二级缓存
- SSM整合
- 搭建环境
- SpringBoot
- SpringBoot核心功能
- 快速入门
- SpringBoot配置文件
- yml配置文件语法
- 配置文件与配置类的属性映射方式
- 端口和映射路径
- 日志级别
- 访问静态资源
- SpringBoot注入方式一
- SpringBoot注入方式二
- 拦截器
- HikariCP连接池
- SpringBoot集成MyBatis
- 通用mapper
- SpringBoot事务
- SpringBoot集成Junit
- SpringBoot集成DataJPA
- SpringBoot集成Redis
- 使用SpringBoot提供的测试启动类
- 使用MockMvc
- SpringCloud
- RestTemplate
- Eureka概述
- Eureka快速入门
- Eureka集群
- Eureka客户端
- Eureka失效剔除和自我保护
- 负载均衡Ribbon
- Hystrix
- Hystrix服务降级
- Hystrix服务熔断
- Feign
- Feign的熔断机制
- Feign的请求压缩和日志级别
- Zuul网关
- Zuul快速入门
- Zuul路由规则
- Zuul过滤器
- Zuul自定义过滤器
- Zuul负载均衡和熔断
- Zuul高可用
- Zuul网关缓存
- SpringSecurity
- 快速入门
- SpringSecurity使用数据库认证