栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring FOUR

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring FOUR

文章目录
  • 第一章:Spring框架的JDBC模板技术
    • 1. JDBC模板技术概述
    • 2. JDBC的模板类的使用
    • 3. 使用Spring框架来管理模板类
    • 4. Spring框架管理开源的连接池
    • 5. Spring框架的JDBC模板的简单操作
  • 第二章:模拟转账开发
    • 1. 完成转账代码的编写
    • 2. Dao实现类编写的方式(第二种方式)
  • 第三章:Spring框架的事务管理
    • 1. Spring框架的事务管理相关的类和API
    • 2. Spring框架声明式事务管理

第一章:Spring框架的JDBC模板技术 1. JDBC模板技术概述

什么模板技术:Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单
template模板
都是Spring框架提供XxxTemplate
提供了JDBC模板,Spring框架提供的
JdbcTemplate类,Connection 表示连接,管理事务 Statement ResultSet

2. JDBC的模板类的使用

jdbc模板类的使用,创建maven工程,引入坐标依赖


    
        org.springframework
        spring-context
        5.0.2.RELEASE
    
    
        commons-logging
        commons-logging
        1.2
    
    
        org.springframework
        spring-test
        5.0.2.RELEASE
    
    
        log4j
        log4j
        1.2.12
    
    
        junit
        junit
        4.12
        test
    
   
    
        com.alibaba
        druid
        1.1.10
    
   
    
        mysql
        mysql-connector-java
        5.1.6
    
    
    
    aopalliance
    aopalliance
    1.0

    
    
        org.springframework
        spring-aspects
        5.0.2.RELEASE
    
    
    
        org.aspectj
        aspectjweaver
        1.8.3
    
    
        org.springframework
        spring-jdbc
        5.0.2.RELEASE
    
    
        org.springframework
        spring-tx
        5.0.2.RELEASE
    

编写测试代码(自己来new对象的方式)

public class Demo4 {
    @Test
   public void run(){
        // 创建连接池对象,Spring框架内置了连接池对象
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        // 设置4个参数
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3308/spring_db");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        // 提供模板,创建对象
        JdbcTemplate template = new JdbcTemplate(dataSource);
        template.update("insert into account values(null,?,?)","告诉","2000");
    }
}
3. 使用Spring框架来管理模板类

刚才编写的代码使用的是new的方式,应该把这些类交给Spring框架来管理。
Spring管理内置的连接池




    
        
        
        
        
    
    
        
    

编写测试方法

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext_jdbc.xml")
public class Demo4 {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Test
   public void run(){
        jdbcTemplate.update("insert into account values(null,?,?)","告诉","2000");
    }
}
4. Spring框架管理开源的连接池

配置开源的连接池,使用Druid开源的连接池,引入坐标如下


com.alibaba
druid
1.1.10

将数据库连接的信息配置到属性文件中

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3308/spring_db
jdbc.username=root
jdbc.password=root

完成核心配置
注意点:spring读取属性配置文件



    
    
    
    
   
    
    
    

    
        
        
        
        
    


    
        
    


5. Spring框架的JDBC模板的简单操作

增删改查代码编写
查询是难点,需要自己编写指定接口的实现类,作用是把查出的结果集封装

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext_jdbc.xml")
public class Demo1_1 {
@Autowired
private JdbcTemplate jdbcTemplate;

@Test
public void run1(){
jdbcTemplate.update("insert into account values (null,?,?)","熊四",800);
}

@Test
public void run2(){
jdbcTemplate.update("update account set name = ?,money = ? where id = ?","光头
强",100,7);
}

@Test
public void run3(){
jdbcTemplate.update("delete from account where id = ?",7);
}

@Test
public void run4(){
Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new
BeanMapper(), 6);
System.out.println(account);
}

@Test
public void run5(){
List list = jdbcTemplate.query("select * from account", new BeanMapper());
for (Account account : list) {
System.out.println(account);
}
}
}

class BeanMapper implements RowMapper{

@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
Account account = new Account();
account.setId(resultSet.getInt("id"));
account.setName(resultSet.getString("name"));
account.setMoney(resultSet.getDouble("money"));
return account;
}
}
第二章:模拟转账开发 1. 完成转账代码的编写

搭建开发的环境
service代码的编写

public interface AccountService {

public void pay(String out,String in,double money);
}

package com.qcby.demo2;

public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}

@Override
public void pay(String out, String in, double money) {
// 调用dao方法
accountDao.outMoney(out,money);
accountDao.inMoney(in,money);
}
}

dao代码的编写(第一种方式)
package com.qcby.demo2;

public interface AccountDao {

public void outMoney(String out,double money);

public void inMoney(String in,double money);
}

package com.qcby.demo2;
import org.springframework.jdbc.core.JdbcTemplate;

public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Override
public void outMoney(String out, double money) {
jdbcTemplate.update("update account set money = money ‐ ? where name = ?",money,out);
}

@Override
public void inMoney(String in, double money) {
jdbcTemplate.update("update account set money = money + ? where name = ?",money,in);
}
}

配置文件代码编写

























测试代码编写

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext_dao1.xml")
public class Demo2 {
@Autowired
private AccountService accountService;

@Test
public void testPay(){
accountService.pay("熊大","熊二",100);
}
}
2. Dao实现类编写的方式(第二种方式)

父类:JdbcDaoSupport(父类中有jdbc模板和连接池的实现)
dao编写

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

@Override
public void outMoney(String out, double money) {
this.getJdbcTemplate().update("update account set money = money ‐ ? where name =
?",money,out);
}

@Override
public void inMoney(String in, double money) {
this.getJdbcTemplate().update("update account set money = money + ? where name =
?",money,in);
}
}

配置文件编写















‐‐>







‐‐>




测试方法

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext_dao2.xml")
public class Demo3 {
@Autowired
private AccountService accountService;

@Test
public void testPay(){
accountService.pay("熊大","熊二",100);
}
}
第三章:Spring框架的事务管理 1. Spring框架的事务管理相关的类和API

PlatformTransactionManager接口 – 平台事务管理器。该接口有具体的实现类,根据不同的持久层框架,需要选择不同的实现类!
接口方法如下:

void commit(TransactionStatus status)
void rollback(TransactionStatus status)

如果使用的Spring的JDBC模板或者MyBatis框架,需要选择DataSourceTransactionManager实现类
如果使用的是Hibernate的框架,需要选择HibernateTransactionManager实现类

2. Spring框架声明式事务管理

配置文件的方式(需要引入事务的约束)
































‐‐>







‐‐>




配置文件+注解的方式
























service代码
@Transactional可以加到类上,就是类中所有的方法都开始事务管理;可以加到方法中,那就是此方法开始事务管理

@Service
@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;

@Override
public void pay(String out, String in, double money) {
// 调用dao方法
accountDao.outMoney(out,money);// 模拟异常
int a = 1/0;
accountDao.inMoney(in,money);
}
}

纯注解的方式

@Configuration
@ComponentScan(basePackages="cn.tx.demo6")
@EnableTransactionManagement // 开启事务注解
public class SpringConfig {

@Bean(name="dataSource")
public DataSource createDataSource() throws Exception{
// 创建连接池对象,Spring框架内置了连接池对象
DriverManagerDataSource dataSource = new DriverManagerDataSource();
// 设置4个参数
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring_db");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}

@Resource(name="dataSource") // 不仅可以作用在属性上,也可以作用方法上。
@Bean(name="jdbcTemplate") // 把JdbcTemplate保存到IOC容器中
public JdbcTemplate createJdbcTemplate(DataSource dataSource){
JdbcTemplate template = new JdbcTemplate(dataSource);
return template;
}

@Resource(name="dataSource")
@Bean(name="transactionManager")
public PlatformTransactionManager createTransactionManager(DataSource dataSource){
DataSourceTransactionManager manager = new
DataSourceTransactionManager(dataSource);
return manager;
}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271522.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号