idea-转账案例1-Java-Spring-AOP-Mapper层

转账案例1

实体类层


```java
package cn.csy.account.entity;

import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;

@Setter
@Getter
public class Account {
	private Long id;
	private BigDecimal balance;
}

## 配置文件
(与数据库建立连接)

```java
mysql.driverName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8&useSSL=false
mysql.username=root
mysql.password=密码

Spring-AOP-mapper层

spring_dao.xml配置文件

写法1:## 标题

<?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-4.3.xsd">
    <context:property-placeholder location="classpath:conf/db.properties"></context:property-placeholder>
    <!--  扫描制定的包,这个包下及其子包下所有类顶上有四大注解的类,创建对象,放入到spring容器中 -->
   <!-- <context:component-scan base-package="cn.csy.account.mapper"></context:component-scan>-->
    <!--<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" scope="prototype"
          init-method="init" destroy-method="close">-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="driverClassName" value="${mysql.driverName}"></property>
        <property name="url" value="${mysql.url}"></property>
        <property name="username" value="${mysql.username}"></property>
        <property name="password" value="${mysql.password}"></property>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--连接池(数据源)-->
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis/mybatis.xml"></property>
        <!--起别名-->
        <property name="typeAliasesPackage" value="cn.csy.account.entity"></property>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!--指向具体的包,这个包下的所有的接口是生成实现类-->
        <property name="basePackage" value="cn.csy.account.mapper"></property>
    </bean>
</beans>

## 写法2:

```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-4.3.xsd">
    <context:property-placeholder location="classpath:conf/db.properties"></context:property-placeholder>
    <!--  扫描制定的包,这个包下及其子包下所有类顶上有四大注解的类,创建对象,放入到spring容器中 -->
   <!-- <context:component-scan base-package="cn.csy.account.mapper"></context:component-scan>-->
    <!--<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" scope="prototype"
          init-method="init" destroy-method="close">-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="driverClassName" value="${mysql.driverName}"></property>
        <property name="url" value="${mysql.url}"></property>
        <property name="username" value="${mysql.username}"></property>
        <property name="password" value="${mysql.password}"></property>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--连接池(数据源)-->
        <property name="dataSource" ref="dataSource"></property>
        <!--代替mybatis.xml-->
        <property name="configLocation" value="classpath:mybatis/mybatis.xml"></property>
        <!--起别名-->
        <property name="typeAliasesPackage" value="cn.csy.account.entity"></property>
        <!--建立关联-->
        <property name="mapperLocations" value="classpath:cn/csy/account/mapper/*Mapper.xml"></property>
    </bean>
    <!--给接口动态生成实现类-->
   <bean id="accountMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        <property name="mapperInterface" value="cn.csy.account.mapper.AccountMapper"></property>
    </bean>
</beans>

接口

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
public interface AccountMapper {
    
    
    public void addBalance(@Param("balance") BigDecimal balance, @Param("id") Long id);
    public void subBalance(@Param("balance") BigDecimal balance,@Param("id") Long id);

}

AccountMapper.xml配置文件

注意:这里的配置文件的文件名要与接口接口名相同

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.csy.account.mapper.AccountMapper" >
   <resultMap  id="basicMap" 
   		type="cn.csy.account.entity.Account">
   		<id property="id" column="id"/>
   		<result property="balance" column="balance"/>
   </resultMap>

	<update id="addBalance">
		update account set balance=balance+#{balance}
			where id=#{id}
	</update>
	<update id="subBalance">
		update account set balance=balance-#{balance}
			where id=#{id}
	</update>
</mapper>

猜你喜欢

转载自blog.csdn.net/weixin_44939526/article/details/109050783