## 代碼
~~~
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使用數據庫認證