wuheng 2 anni fa
parent
commit
4243b6a9d4

+ 8 - 5
day02/src/main/java/com/lovecoding/aop/CounterPorxy.java → day02/src/main/java/com/lovecoding/aop/CounterProxy.java

@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
 
 @Aspect
 @Component
-public class CounterPorxy {
+public class CounterProxy {
 
     /**
      * 接收通知的方法
@@ -39,19 +39,22 @@ public class CounterPorxy {
     @Around("execution(public int com.lovecoding.aop.CountterImpl.*(..))")
     public Object around( ProceedingJoinPoint joinPoint ){
         try {
-            System.out.println( "环绕通知" );
-            return joinPoint.proceed(joinPoint.getArgs());
+            System.out.println( "环绕通知前" );
+            Object proceed = joinPoint.proceed(joinPoint.getArgs());
+            System.out.println( "环绕通知后" );
+            return proceed;
         } catch (Throwable e) {
             e.printStackTrace();
         }
         return null;
     }
 
-    @AfterThrowing("execution(public int com.lovecoding.aop.CountterImpl.*(..))")
-    public void AfterThrowing(JoinPoint joinPoint){
+    @AfterThrowing(value = "execution(public int com.lovecoding.aop.CountterImpl.*(..))", throwing = "e")
+    public void AfterThrowing(JoinPoint joinPoint, Throwable e){
         Signature signature = joinPoint.getSignature();
         String name = signature.getName();
         System.out.println( "方法 :" + name + "发生异常" );
+        System.out.println( e.getMessage() );
     }
 
 }

+ 38 - 4
day02/src/main/java/com/lovecoding/aop/XmlAop.java

@@ -2,15 +2,49 @@ package com.lovecoding.aop;
 
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
+
 /**
  * 使用 XML 配置 AOP 切片
  *  效果和配置类无异样
  */
 public class XmlAop {
     public static void main(String[] args) {
-        ClassPathXmlApplicationContext con =
-                new ClassPathXmlApplicationContext("anno.xml");
-        Counter countterImpl = con.getBean("countterImpl", Counter.class);
-        countterImpl.add(6,6);
+
+
+        /**
+         * Java JDK 的动态代理实际上就是一个
+         * 泛型的 反射封装
+         */
+        ClassLoader classLoader = XmlAop.class.getClassLoader();
+        Class[] cl = { Counter.class };
+        class MyInvocationHandler<T> implements InvocationHandler{
+            T t;
+            public MyInvocationHandler(T t) {
+                this.t = t;
+            }
+            @Override
+            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+                System.out.println( "前置方法" );
+                Object invoke = method.invoke( t , args);
+                System.out.println( "后置方法" );
+                return invoke;
+            }
+        }
+        Counter o =  (Counter) Proxy.newProxyInstance(
+                classLoader,
+                cl,
+                new MyInvocationHandler( new CountterImpl() )
+        );
+        o.add( 1,1 );
+
+
+//        ClassPathXmlApplicationContext con =
+//                new ClassPathXmlApplicationContext("anno.xml");
+//        Counter countterImpl = con.getBean("countterImpl", Counter.class);
+//        countterImpl.add(6,6);
     }
 }

+ 4 - 0
day02/src/main/resources/mysql.properties

@@ -0,0 +1,4 @@
+mysql.username = root
+mysql.url = jdbc:mysql://127.0.0.1:13306/test?charEncoding=utf8&useSSL=false
+mysql.passwd = 0JZBdtlYoiOepddh
+mysql.driver = com.mysql.cj.jdbc.Driver

BIN
day03/Spring-事务.pdf


+ 55 - 0
day03/pom.xml

@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>spring</artifactId>
+        <groupId>com.lovecoding.spring</groupId>
+        <version>1.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>day03</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+
+
+    <dependencies>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid</artifactId>
+            <version>1.2.6</version>
+        </dependency>
+
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>8.0.25</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-jdbc</artifactId>
+            <version>5.3.22</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+            <version>5.3.22</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-beans</artifactId>
+            <version>5.3.22</version>
+        </dependency>
+
+    </dependencies>
+
+
+</project>

+ 52 - 0
day03/src/main/java/com/lovecoding/jdbc/Application.java

@@ -0,0 +1,52 @@
+package com.lovecoding.jdbc;
+
+import com.alibaba.druid.pool.DruidDataSource;
+import com.lovecoding.jdbc.dao.Test;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+import java.util.List;
+
+public class Application {
+    public static void main(String[] args) {
+
+        ClassPathXmlApplicationContext context =
+                new ClassPathXmlApplicationContext("jdbc.xml");
+        JdbcTemplate template = context.getBean("jdbcTemplate", JdbcTemplate.class);
+
+        //获取数据库单独字段
+//        Integer integer = template.queryForObject(
+//                "SELECT count(*) AS c FROM test",
+//                Integer.class
+//        );
+        //新增操作
+//        int update = template.update(
+//                "INSERT into test values (?), (?)",
+//                5, 6
+//        );
+        //更新操作
+//        int update = template.update(
+//                "UPDATE test set id = ? where id = ?",
+//                100,  1
+//        );
+        //删除操作
+//        int update = template.update(
+//                "DELETE from test where id = ?",
+//                100
+//        );
+        //查询数据实体
+//        Test test = template.queryForObject(
+//                "SELECT * FROM test LIMIT 1",
+//                new BeanPropertyRowMapper<>(Test.class));
+
+        //查询一个数据列表
+//        List<Test> query = template.query(
+//                "SELECT * FROM test",
+//                new BeanPropertyRowMapper<>(Test.class)
+//        );
+
+
+
+    }
+}

+ 16 - 0
day03/src/main/java/com/lovecoding/jdbc/ConfigTx.java

@@ -0,0 +1,16 @@
+package com.lovecoding.jdbc;
+
+import com.lovecoding.jdbc.service.UserBuyBookService;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+
+public class ConfigTx {
+    public static void main(String[] args) {
+        AnnotationConfigApplicationContext context =
+                new AnnotationConfigApplicationContext(SpringConfig.class);
+
+        UserBuyBookService bean = context.getBean(UserBuyBookService.class);
+
+        bean.buyBook(1, 1);
+
+    }
+}

+ 18 - 0
day03/src/main/java/com/lovecoding/jdbc/JdbcTest.java

@@ -0,0 +1,18 @@
+package com.lovecoding.jdbc;
+
+import com.lovecoding.jdbc.service.UserBuyBookService;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class JdbcTest {
+
+    public static void main(String[] args) {
+
+        ClassPathXmlApplicationContext context =
+                new ClassPathXmlApplicationContext("jdbc.xml");
+        UserBuyBookService bean = context.getBean(UserBuyBookService.class);
+
+        bean.buyBook( 1, 2 );
+
+    }
+
+}

+ 40 - 0
day03/src/main/java/com/lovecoding/jdbc/SpringConfig.java

@@ -0,0 +1,40 @@
+package com.lovecoding.jdbc;
+
+import com.alibaba.druid.pool.DruidDataSource;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+
+@Configuration
+@ComponentScan("com.lovecoding")
+@EnableTransactionManagement
+public class SpringConfig {
+
+    @Bean
+    public DruidDataSource getDruidDataSource(){
+        DruidDataSource druidDataSource = new DruidDataSource();
+        druidDataSource.setUsername("root");
+        druidDataSource.setUrl("jdbc:mysql://127.0.0.1:13306/test?characterEncoding=utf-8&useSSL=false");
+        druidDataSource.setPassword("0JZBdtlYoiOepddh");
+        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
+        return druidDataSource;
+    }
+
+    @Bean
+    public JdbcTemplate getJdbcTemplate(){
+        JdbcTemplate template = new JdbcTemplate();
+        template.setDataSource( getDruidDataSource() );
+        return template;
+    }
+
+    @Bean
+    public DataSourceTransactionManager getDataSourceTransactionManager(){
+        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
+        dataSourceTransactionManager.setDataSource(getDruidDataSource());
+        return dataSourceTransactionManager;
+    }
+}

+ 7 - 0
day03/src/main/java/com/lovecoding/jdbc/dao/Book.java

@@ -0,0 +1,7 @@
+package com.lovecoding.jdbc.dao;
+
+public interface Book {
+    int getBook(int bookid);
+
+    void subBook(int bookid);
+}

+ 41 - 0
day03/src/main/java/com/lovecoding/jdbc/dao/BookImpl.java

@@ -0,0 +1,41 @@
+package com.lovecoding.jdbc.dao;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public class BookImpl implements Book {
+
+    @Autowired
+    JdbcTemplate jdbcTemplate;
+
+    /**
+     * 查询书的价格
+     * @param bookid
+     * @return
+     */
+    @Override
+    public int getBook(int bookid) {
+        Object [] id = {bookid};
+        Integer integer = jdbcTemplate.queryForObject(
+                "SELECT price FROM t_book WHERE book_id = ?",
+                id,
+                Integer.class
+        );
+        return integer;
+    }
+
+    /**
+     * 扣减书的库存
+     * @param bookid
+     */
+    @Override
+    public void subBook(int bookid) {
+        int update = jdbcTemplate.update(
+                "UPDATE t_book set stock = stock - 1 WHERE book_id = ?",
+                bookid
+        );
+    }
+}

+ 20 - 0
day03/src/main/java/com/lovecoding/jdbc/dao/Test.java

@@ -0,0 +1,20 @@
+package com.lovecoding.jdbc.dao;
+
+public class Test {
+    private int id;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    @Override
+    public String toString() {
+        return "Test{" +
+                "id=" + id +
+                '}';
+    }
+}

+ 5 - 0
day03/src/main/java/com/lovecoding/jdbc/dao/User.java

@@ -0,0 +1,5 @@
+package com.lovecoding.jdbc.dao;
+
+public interface User {
+    void balance(int i, int uid);
+}

+ 25 - 0
day03/src/main/java/com/lovecoding/jdbc/dao/UserImpl.java

@@ -0,0 +1,25 @@
+package com.lovecoding.jdbc.dao;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public class UserImpl implements User {
+
+    @Autowired
+    JdbcTemplate jdbcTemplate;
+
+    /**
+     * 扣减用户钱包
+     * @param value
+     * @param uid
+     */
+    @Override
+    public void balance(int value, int uid) {
+        jdbcTemplate.update(
+                "UPDATE t_user set balance = balance - ? WHERE user_id = ?",
+                value, uid
+        );
+    }
+}

+ 5 - 0
day03/src/main/java/com/lovecoding/jdbc/service/UserBuyBookService.java

@@ -0,0 +1,5 @@
+package com.lovecoding.jdbc.service;
+
+public interface UserBuyBookService {
+    void buyBook(int uid, int bookid);
+}

+ 36 - 0
day03/src/main/java/com/lovecoding/jdbc/service/UserBuyBookServiceImpl.java

@@ -0,0 +1,36 @@
+package com.lovecoding.jdbc.service;
+
+import com.lovecoding.jdbc.dao.Book;
+import com.lovecoding.jdbc.dao.User;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+public class UserBuyBookServiceImpl implements UserBuyBookService{
+
+    /**
+     * 买书 分为一下及步骤
+     * 0 查库存
+     * 1 扣库存
+     * 2 扣用户余额
+     * @param uid
+     * @param bookid
+     */
+
+    @Autowired
+    private Book book;
+    @Autowired
+    private User user;
+
+    @Override
+    //@Transactional
+    public void buyBook(int uid, int bookid) {
+        //查询书的价格
+        int price = book.getBook(bookid);
+        //扣减书的库存
+        this.book.subBook(bookid);
+        //扣减用户钱包
+        user.balance( price, uid );
+    }
+}

+ 35 - 0
day03/src/main/resources/jdbc.xml

@@ -0,0 +1,35 @@
+<?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"
+       xmlns:tx="http://www.springframework.org/schema/tx"
+
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
+       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+    <context:component-scan base-package="com.lovecoding" />
+    <context:property-placeholder location="mysql.properties" />
+
+<!--  配置数据库事务对象  -->
+    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
+        <property name="dataSource" ref="dataSource" />
+    </bean>
+
+<!-- 启动注解配置数据库事务 -->
+    <tx:annotation-driven transaction-manager="dataSourceTransactionManager" />
+
+<!-- 数据源 -->
+    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
+        <property name="username" value="${mysql.username}" ></property>
+        <property name="driverClassName" value="${mysql.driver}" ></property>
+        <property name="password" value="${mysql.passwd}" ></property>
+        <property name="url" value="${mysql.url}" ></property>
+    </bean>
+<!-- 配置 Spring jdbc 对象 -->
+    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
+        <property name="dataSource" ref="dataSource" />
+    </bean>
+
+</beans>

+ 4 - 0
day03/src/main/resources/mysql.properties

@@ -0,0 +1,4 @@
+mysql.username = root
+mysql.url = jdbc:mysql://127.0.0.1:13306/test?characterEncoding=utf-8&useSSL=false
+mysql.passwd = 0JZBdtlYoiOepddh
+mysql.driver = com.mysql.cj.jdbc.Driver

+ 1 - 0
pom.xml

@@ -12,6 +12,7 @@
         <module>day01</module>
         <module>demo01</module>
         <module>day02</module>
+        <module>day03</module>
     </modules>
 
     <properties>