事务注解增删改查

事务增删改案例

查询

创建实体

/*序列化 (Serialization)*/
public class Account implements Serializable {
    private Integer accountId;
    private String accountName;
    private double balance;
    public Integer getAccountId() {
        return accountId;
    }
    public void setAccountId(Integer accountId) {
        this.accountId = accountId;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}

创建Dao接口

public interface AccountDao {
    public List<Account> getAll();
}

创建Dao实现类方法1

@Repository//默认ByName方式,如果name确实默认按照ByType方式注入,
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    @Override
    public List<Account> getAll() {
       List<Account> query = this.getJdbcTemplate().query("select * from account", new RowMapper<Account>() {
            @Override
            public Account mapRow(ResultSet rs, int RowNum) throws SQLException {
                Account account = new Account();
                account.setAccountId(rs.getInt("accountid"));
                account.setAccountName(rs.getString("accountname"));
                account.setBalance(rs.getDouble("balance"));
                return account;
            }
        });
        return query;
    }

创建Dao实现类方法2

@Repository
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    @Override
    public List<Account> getAll() {
        RowMapper<Account> ac = new BeanPropertyRowMapper<>(Account.class);
        List<Account> query = getJdbcTemplate().query("select * from account", ac);


        return query;
    }

编写service接口

public interface AccountService {
    public List<Account> getAll();
}

service接口实现类

@Service("accountServiceImpl")//用于标注业务类
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountDao dao;
	//必须需要set方法
    public void setDao(AccountDao dao) {
        this.dao = dao;
    }
    @Override
    public List<Account> getAll() {
        return dao.getAll();
    }
}

编写database.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/account?useUniCode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

编写ApplicationContext.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">
     //连接database.properties
    <context:property-placeholder location="classpath:database.properties"/>

        <!--数据源-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <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>

        <bean id="jdbcTempalate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
      <!--找到daoimpl-->
        <bean id="accountDaoImpl" class="com.jdbcTempalate.dao.impl.AccountDaoImpl">
          <property name="jdbcTemplate" ref="jdbcTempalate"></property>
        </bean>
        <bean id="accountServiceImpl" class="com.jdbcTempalate.Service.impl.AccountServiceImpl">
            <property name="dao" ref="accountDaoImpl"></property>
        </bean>
</beans>

删除修改添加

    @Override
    public int delAccount(int account) {
        String sql="delete from account where accountid=?";
        int update = getJdbcTemplate().update(sql, account);
        return update;
    }

    @Override
    public int INSERT(Account account) {
        String sql="INSERT INTO account (accountid,accountname,balance) VALUE(?,?,?)";
        int insert = getJdbcTemplate().update(sql, account.getAccountId(),account.getAccountName(), account.getBalance());
        return insert;
    }
       @Override
    public void addMoney(double balance,Integer id) {
        String sql="update account set balance=balance+? where accountid=?";
        this.jdbcTemplate.update(sql,new Object[]{balance,id});
        System.out.println(sql);
    }

注解查询

创建实体

/*序列化 (Serialization)*/
public class Account implements Serializable {
    private Integer accountId;
    private String accountName;
    private double balance;

    public Integer getAccountId() {
        return accountId;
    }

    public void setAccountId(Integer accountId) {
        this.accountId = accountId;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}

dao接口

public interface AccountDao{
    List<Account> getAll();//查询

}

dao实现类

@Repository// extends JdbcDaoSupport
public class AccountDaoImpl implements AccountDao {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<Account> getAll() {

        BeanPropertyRowMapper<Account> a = new BeanPropertyRowMapper<>(Account.class);
        List<Account> query = jdbcTemplate.query("select * from account", a);
        return query;
    }
 }

service接口

public interface AccountService {
    List<Account> getAll();
}

service接口实现类

@Service("accountServiceImpl")
public class AccountServiceImpl implements AccountService {
	//@Resource可以不需要set方法
    @Resource
    private AccountDao dao;

    @Override
    public List<Account> getAll() {
        return dao.getAll();
    }

}

ApplicationContext.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.jdbcTempalate"/>
    <context:property-placeholder location="classpath:database.properties"/>

        <!--数据源-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <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>

        <bean id="jdbcTempalate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
</beans>

database.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/account?useUniCode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

注解和事事务比较起来也就是xml和daoimpl里代码改的多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值