Spring-整合JDBC-事务-远程方法调用RMI

一、spring整合JDBC

  spring整合jdbc使用了模版方法设计模式

    定义一套规范,固定流程不变,传入可变内容

  1、Maven项目添加依赖

    spring-context坐标依赖

    mysql驱动包

    c3p0连接池

    spring jdbc

  

  2、准备数据库的配置文件

  3、xml配置

    

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

    <!--配置扫描器-->
    <context:component-scan base-package="com.shsxt"/>
    <!--加载properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置数据源c3p0-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

  这里使用了c3p0数据源

    C3P0有自动回收空闲连接功能;

    dbcp没有自动回收空闲连接功能;

  

  4、使用spring jdbc完成crud操作

package com.shsxt;

import com.shsxt.po.Account;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.lang.model.element.Name;
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
@Repository
public class AccountDaoImpl implements AccountDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 添加记录返回受影响行数
     * @param account
     * @return
     */
    @Override
    public Integer saveAccount(Account account) {
        String sql="insert into account (name,type,money,remark,create_time,update_time,userId) values(?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql,account.getName(),account.getType(),account.getMoney(),account.getRemark(),account.getCreate_time(),account.getUpdate_time(),account.getUserId());
    }

    /**
     * 添加记录返回主键
     * @param account
     * @return
     */
    @Override
    public Integer saveAccountHasPrimaryKey(Account account) {
        String sql="insert into account (name,type,money,remark,create_time,update_time,userId) values(?,?,?,?,?,?,?)";
        KeyHolder keyHolder=new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                PreparedStatement ps=con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1,account.getName());
                ps.setString(2,account.getType());
                ps.setBigDecimal(3,account.getMoney());
                ps.setString(4,account.getRemark());
                ps.setObject(5,account.getCreate_time());
                ps.setObject(6,account.getUpdate_time());
                ps.setInt(7,account.getUserId());
                return ps;
            }
        },keyHolder);
        return keyHolder.getKey().intValue();
    }

    /**
     * 批量添加记录
     * @param accounts
     * @return
     */
    @Override
    public Integer saveAccountsBatch(List<Account> accounts) {
        String sql="insert into account (name,type,money,remark,create_time,update_time,userId) values(?,?,?,?,?,?,?)";
        return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setString(1,accounts.get(i).getName());
                ps.setString(2,accounts.get(i).getType());
                ps.setBigDecimal(3,accounts.get(i).getMoney());
                ps.setString(4,accounts.get(i).getRemark());
                ps.setObject(5,accounts.get(i).getCreate_time());
                ps.setObject(6,accounts.get(i).getUpdate_time());
                ps.setInt(7,accounts.get(i).getUserId());
            }

            @Override
            public int getBatchSize() {
                return accounts.size();
            }
        }).length;
    }

    /**
     * 统计账户记录-聚合查询
     * @param userId
     * @return
     */
    @Override
    public Integer countAccountsByUserId(Integer userId) {
        String sql="select count(1) from account where userId=?";
        return jdbcTemplate.queryForObject(sql,Integer.class,userId);
    }

    /**
     * 根据id查询记录详情
     * @param Id
     * @return
     */
    @Override
    public Account queryAccountById(Integer Id) {
        String sql="select id,userId,name,type,money,remark,update_time,create_time from account where id=?";
         return (Account) jdbcTemplate.queryForObject(sql,new Object[]{Id}, new RowMapper<Account>() {
            @Override
            public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
                Account account=new Account();
                account.setUpdate_time(rs.getDate("update_time"));
                account.setRemark(rs.getString("remark"));
                account.setType(rs.getString("type"));
                account.setMoney(rs.getBigDecimal("money"));
                account.setCreate_time(rs.getDate("create_time"));
                account.setId(rs.getInt("id"));
                account.setName(rs.getString("name"));
                account.setUserId(rs.getInt("userId"));
                return account;
            }
        });
    }

    /**
     * 多条件查询
     * @param userId
     * @param type
     * @param createTime
     * @param aname
     * @return
     */
    @Override
    public List<Account> queryAccountsByParams(Integer userId, String type, String createTime, String aname) {
        StringBuffer sql=new StringBuffer("select id,userId,name,type,money,remark,update_time,create_time from account where 1=1");
        List<Object> params=new ArrayList<>();
        if (null!=userId){
            sql.append(" and userId=?");
            params.add(userId);
        }
        if (StringUtils.isNotBlank(type)){
            sql.append(" and type=? ");
            params.add(type);
        }
        if (StringUtils.isNotBlank(createTime)){
            sql.append(" and createTime=?");
            params.add(createTime);
        }
        if (StringUtils.isNotBlank(aname)){
            sql.append(" and name=?");
            params.add(aname);
        }
        return  jdbcTemplate.query(sql.toString(), params.toArray(), new RowMapper<Account>() {

            @Override
            public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
                Account account=new Account();
                account.setId(rs.getInt("id"));
                account.setUserId(rs.getInt("userId"));
                account.setName(rs.getString("name"));
                account.setCreate_time(rs.getDate("create_time"));
                account.setMoney(rs.getBigDecimal("money"));
                account.setType(rs.getString("type"));
                account.setRemark(rs.getString("remark"));
                account.setUpdate_time(rs.getDate("update_time"));
                return account;
            }
        });

    }

    @Override
    public Integer updateAccount(Account account) {
        String sql="update account set name=?,type=?,money=? where id=?";
        return jdbcTemplate.update(sql,account.getName(),account.getType(),account.getMoney(),account.getId());
    }

    /**
     * 批量更新
     * @param accounts
     * @return
     */
    @Override
    public Integer updateAccountsBatch(List<Account> accounts) {
        String sql="update account set name=?,type=?,money=? where id=?";
        return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setString(1,accounts.get(i).getName());
                ps.setString(2,accounts.get(i).getType());
                ps.setBigDecimal(3,accounts.get(i).getMoney());
                ps.setInt(4,accounts.get(i).getId());
            }

            @Override
            public int getBatchSize() {
                return accounts.size();
            }
        }).length;
    }

    /**
     * 根据id删除记录
     * @param id
     * @return
     */
    @Override
    public Integer deleteAccountById(Integer id) {
        String sql="delete from account where id=?";
        return jdbcTemplate.update(sql,id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public Integer deleteAccountsBatch(Integer[] ids) {
        String sql="delete from account where id=?";
        return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setInt(1,ids[i]);
            }

            @Override
            public int getBatchSize() {
                return ids.length;
            }
        }).length;
    }

    @Override
    public Integer inMoney(Integer sourceId, BigDecimal money) {
        String sql="update account set money=money-? where id=?";
        return jdbcTemplate.update(sql,money,sourceId);
    }

    @Override
    public Integer outMoney(Integer targetId, BigDecimal money) {
        String sql="update account set money=money+? where id=?";
        return jdbcTemplate.update(sql,money,targetId);
    }
}
crud

 

二、spring事务

  1、事务的四大特性(ACID)

    原子性(Atomicity):共生死,要么全部成功,要么全部失败

    一致性(Consistency):事务在执行前后,数据库中数据要保持一致性状态

    隔离性(Lsolation):事务与事务之间的执行应当是相互隔离互不影响的

    持久性(Durability):事务提交完毕后,数据库中的数据的改变是永久的

 

  2、事务配置

    1)、添加坐标依赖

      aop、spring事务

    

    2)、XML配置

      修改xml命名空间

        xmlns:tx="http://www.springframework.org/schema/tx"

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx.xsd

      

 <!--aop代理-->
    <aop:aspectj-autoproxy/>
    <!--事务配置-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--事务通知配置-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!--配置事务要拦截的方法-->
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
  <!--aop切面定义-->
    <aop:config>
        <aop:pointcut id="cut" expression="execution(* com.shsxt.service..*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="cut"/>
    </aop:config>

    <!--注解方式配置事务,可以和xml配置共同使用-->
    <tx:annotation-driven transaction-manager="txManager"/>

 

    3)、注解使用事务,xml配置后在方法上添加注解

      

    

    4)、事务传播行为

     

 

三、远程方法调用RMI

  

  Java RMI 指的是远程方法调用 (Remote Method Invocation)。它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法。 JVM 可以位于相同或不同计算机上,在多个 JVM 中,一个 JVM 可以调用存储在其它 JVM 的对象的方法。

  具体实现先不写了,我还没弄清楚。待修改

posted @ 2018-09-29 23:37  这夜景  阅读(627)  评论(0编辑  收藏  举报