设为首页 加入收藏

TOP

15、Spring之基于xml的声明式事务(二)
2023-09-09 10:25:55 】 浏览:78
Tags:Spring xml 明式事
ext:property-placeholder location="jdbc.properties"></context:property-placeholder> <!-- 配置数据源 --> <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"> <!--通过${key}的方式访问外部属性文件的value--> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置 JdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!-- 装配数据源 --> <property name="dataSource" ref="datasource"></property> </bean> </beans>

15.1.4、创建持久层接口BookDao及其实现类

image

package org.rain.spring.dao;

/**
 * @author liaojy
 * @date 2023/9/3 - 17:34
 */
public interface BookDao {

    /**
     * 查询图书的价格
     * @param bookId
     * @return
     */
    Integer getPriceByBookId(Integer bookId);

    /**
     * 更新图书的库存
     * @param bookId
     */
    void updateStockOfBook(Integer bookId);

    /**
     * 更新用户的余额
     * @param userId
     * @param price
     */
    void updateBalanceOfUser(Integer userId,Integer price);

}

image

package org.rain.spring.dao.impl;

import org.rain.spring.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author liaojy
 * @date 2023/9/3 - 17:36
 */
@Repository
public class BookDaoImpl implements BookDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Integer getPriceByBookId(Integer bookId) {
        String sql = "select price from t_book where book_id = ?";
        Integer price = jdbcTemplate.queryForObject(sql, Integer.class,bookId);
        return price;
    }

    public void updateStockOfBook(Integer bookId) {
        String sql = "update t_book set stock = stock -1 where book_id = ?";
        jdbcTemplate.update(sql, bookId);
    }

    public void updateBalanceOfUser(Integer userId, Integer price) {
        String sql = "update t_user set balance = balance - ? where user_id = ?";
        jdbcTemplate.update(sql,price,userId);
    }

}

15.1.5、创建业务层接口BookService及其实现类

image

package org.rain.spring.service;

/**
 * @author liaojy
 * @date 2023/9/3 - 17:52
 */
public interface BookService {

    void buyBook(Integer bookId,Integer userId);

}

image

package org.rain.spring.service.impl;

import org.rain.spring.dao.BookDao;
import org.rain.spring.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liaojy
 * @date 2023/9/3 - 17:53
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    public void buyBook(Integer bookId, Integer userId) {

        //查询图书的价格
        Integer price = bookDao.getPriceByBookId(bookId);

        //更新图书的库存
        bookDao.updateStockOfBook(bookId);

        //更新用户的余额
        bookDao.updateBalanceOfUser(userId,price);

    }
}

15.1.6、创建控制层BookController

image

注意:因为控制层没用到接口,所以方法的访问修饰符要手动设置

package org.rain.spring.
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇电商类面试问题--01Elasticsearch.. 下一篇测试大姐提了个bug,为什么你多了..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目