spring接口管理sql语句和spring的声明式的事务

1.使用接口管sql的语句:

(1):新建一个接口

public interface ProductMapper {
    /**新增的方法
     * @param product 商品的实例
     * */
    @Insert("insert into product(id,name,price) values (#{id},#{name},#{price}) ")
    public void insert(Product product);
    /**修改的方法
     * @param product  商品的实例
     * */
    @Update("update product set name = #{name} ,price = #{price} where id = #{id}")
    public void update(Product product);
    /**删除的方法
     * @param id  商品的编号
     * */
    @Delete("delete from product where id = #{product.id}")
    public void delete(int id);
    /**按照id的查询的方法
     * @param id 商品的编号
     * @return Product
     * */
    @Select("select * from product where id = #{id}")
    public Product selectById(int id);
    /**
     * 按分页查询的方法
     * @param map    查询的map的集合,里面存储了start 和 num
     * @return List<Product>   将查询的结果返回在一个list的集合里面
     * */
    @Select("select * from product limit #{start},#{num}")
    public List<Product> selectByPage(Map map);
}

(2):编写spring的配置的文件

<?xml version="1.0" encoding="UTF-8" ?>
<!--xmlns在这里设置的是命名的空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:annotation-config/>
    <!--对.properties的文件读取-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="${jdbc.max}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="minIdle" value="${jdbc.min}"/>
    </bean>
    <!--启用sqlSession的工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--对mapper的里面的文件进行扫描,并将mapper里面的接口配置成bean-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入了一包,value是包的地址-->
       <property name="basePackage" value="com.www.mapper"/>
    </bean>
    <!--注解驱动的方式来管理实务-->
    <!--启用注解的方式管理事务-->
    <tx:annotation-driven/>

    <!--事务管理器,是真正的执行事务的管理  注意id放的值一定是:transactionManager-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="productService" class="com.www.service.ProductService">
    </bean>
    <bean id="product" class="com.www.entity.Product"/>
</beans>

(3):利用ClassPathXmlApplicationContext 产生一个service的对象,执行service的方法

public class TestProduct {
    static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Product-spring.xml");
    @Test
    public void test1(){
        ProductService productService = context.getBean(ProductService.class);
        Product product = new Product();
        product.setId(100000);
        product.setName("hhhhhhhhhhhhhhhh");
        product.setPrice(22222222);
        productService.biz(product);
    }
    @Test
    public void delete(){
        ProductService productService = context.getBean(ProductService.class);
        productService.biz2(100000);
    }
}

但是接口的管理的sql的语句只有适用于简单的增删改查的语句,若是sql语句变得复杂的话,还是要使用.xml的语句
注意的事项:
(1):xml的id的值方法的名字一致
(2):xml文件的目录与就接口的目录的结构是一致的
(3):nameSpace的值与接口的包名和类名是一致的
(4):mybatis的一般默认的方法接收的参数是一个,如果有多个的话就要利用list的集合或者是Map的集合

2.spring 的声明式的事务

使用的步骤

步骤:
(1):在xml的文件上加入tx的命名的空间
(2):启用注解驱动方式来管理事务
(3):在xml配置事务的管理器
(4):使用@Transaction的注解
注解的位置:
1.方法上:表示这个方法受事务的管理
2.类名上:整个类受事务的管理

@Transaction的详解

1:@Transactional(rollbackFor = *.class):遇见指定的的异常就进行回滚
2:@Transactional(noRollbackFor = *.class):遇见指定的异常不进行回滚
3: @Transactional(isolation = Isolation.READ_UNCOMMITTED) 设置数据库的隔离的级别
4:@Transactional(readOnly = true) :为true的时候是只能进行查询的的操作,但是在为false的时候还能执行增删改的操作
如果在readOnly为true的时候执行了增删改的操作的话就会出现如下的错误
Error updating database. Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
5:@Transactional(propagation = 传播行为)
REQUIRED (默认值) required 必须的 (如果没有事务,开始新的;如果有了用已有的)
SUPPORTS 支持的 (如果没有事务,不开始新的;如果有了用已有的)
REQUIRES_NEW 需要新的 (总会开始新事务)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值