发表于: 2018-04-21 14:22:07

1 552


编辑日报内今天完成的事情:  

spring学习  完成sm框架对数据库的增删改查

参考

spring 标签语句学习  https://www.w3cschool.cn/wkspring/o1qy1h9q.html

ioc思想   https://www.zhihu.com/question/23277575  这篇文章很值得入门看


IOC初步理解理解


我们可以轮胎、底盘、车身、汽车理解为我们程序编写的类  在我们学习java基础时候我们总是在不断new一个对象(面向对象)进行运行,不同类在调用对象时候他们的关系耦合度很高,从上面图片看出我们正常做出整个汽车 ,我们先要设计汽车的依赖关系 我们必须先设计号轮胎,才能设计底盘,在做车身,然后设计汽车,如果我们修改汽车的底层轮胎话,我们必须修改所有相关对象。而如果我们反过来设计思路 ,当我们需要修改底层的轮胎,根据依赖关系我们就只需要修改轮胎;


为了跟好理解这篇文章提供了代码来理解这个思想


从这个实例中我们看到我们要修改轮子数据我们必须对整个项目中所有依赖轮胎的的数据进行修改 我们才能运行初始没有car.run();

控制反转的思想  就能进行如下实现汽车的修改  我们不需要去修改高层类的数据  只要修改Tire数据 只不过初始化汽车的通过新的穿件new来完成,而控制反转容器(IoC Container)可以帮我们完成这些操作       (下文我还会对依赖注入Setter  依赖注入接口  依赖构造函数传递学习      参考w3cschool)

我们可以看到这些类之间的构造函数Tire(int size) Bottom(Tire tire)  Framework(Bottom Bottom)  Car(Framework Framework) 这些类都进行构造函数依赖注入了 ,和之前Tire() Bottom()  Framework()  Car()明显不同。


控制反转容器(IoC Container)   这篇文章的图形非常便于理解ioc Containaer 帮我们做了什么


spring学习    参考https://www.w3cschool.cn/wkspring/o1qy1h9q.html

Spring 容器


Spring bean 定义    w3cschool   知识如下

Bean 定义

被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的。bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象。这些 bean 是由用容器提供的配置元数据创建的,例如,已经在先前章节看到的,在 XML 的表单中的  定义。

bean 定义包含称为配置元数据的信息,下述容器也需要知道配置元数据:

class这个属性是强制性的,并且指定用来创建 bean 的 bean 类。
name这个属性指定唯一的 bean 标识符。在基于 XML 的配置元数据中,你可以使用 ID 和/或 name 属性来指定 bean 标识符。
scope这个属性指定由特定的 bean 定义创建的对象的作用域,它将会在 bean 作用域的章节中进行讨论。
constructor-arg它是用来注入依赖关系的,并会在接下来的章节中进行讨论。
properties它是用来注入依赖关系的,并会在接下来的章节中进行讨论。
autowiring mode它是用来注入依赖关系的,并会在接下来的章节中进行讨论。

class bean类 construcror-arg  依赖构造函数注入  prooerties依赖setter注入  这里作为重点我会在下文调用自己学习的代码和.xml内容来具体讲解下。

bean生命周期  默认的初始化和销毁方法

框架使用  元素中的 default-init-methoddefault-destroy-method 属性提供了灵活地配置这种情况

<beans xmlns="http://www.springframework.org/schema/beans"
    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-3.0.xsd"
    default-init-method="init" 
    default-destroy-method="destroy">
   <bean id="..." class="...">
       <!-- collaborators and configuration for this bean go here -->
   </bean></beans>

bean依赖注入  w3cschool   知识如下

序号依赖注入类型 & 描述
1Constructor-based dependency injection

当容器调用带有多个参数的构造函数类时,实现基于构造函数的 DI,每个代表在其他类中的一个依赖关系。

2Setter-based dependency injection

基于 setter 方法的 DI 是通过在调用无参数的构造函数或无参数的静态工厂方法实例化 bean 之后容器调用 beans 的 setter 方法来实现的。

最后,如果你想要向一个对象传递一个引用,你需要使用  标签的 ref 属性,如果你想要直接传递值,那么你应该使用如上所示的 value 属性。

construcror-arg  依赖构造函数注入

注入类中要有构造函数

prooerties依赖setter注入(TextEditor(){}中一定要有setSpellChecker()getSpellChecker()方法以便于注入SpellCheckerle类)

package com.tutorialspoint;public class TextEditor {   private SpellChecker spellChecker;   // a setter method to inject the dependency.
   public void setSpellChecker(SpellChecker spellChecker) {
      System.out.println("Inside setSpellChecker." );      this.spellChecker = spellChecker;
   }   // a getter method to return spellChecker
   public SpellChecker getSpellChecker() {      return spellChecker;
   }   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}
package com.tutorialspoint;public class SpellChecker {   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   }  
}

配置文件写法

<!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
      <property name="spellChecker" ref="spellChecker"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>


注入的类中要有get()set()方法来获取对象   如果bean类没有get()set()方法返回获取对象   就会报错空指针异常  或者加载不到该类

这个是我sm框架标签文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      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
      ">
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost:3306/mysql?useUnicode=true&amp;characterEncoding=utf8"/>
       <property name="username" value="root"/>
       <property name="password" value="123456"/>
   </bean>


   <!-- 配置SqlSessionFactory对象 -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 注入数据库连接池 -->
       <property name="dataSource" ref="dataSource"/>
       <!-- 扫描sql配置文件:mapper需要的xml文件 -->
       <property name="mapperLocations" value="com/longhang/mapper/StudentMapper.xml"/>
   </bean>

   <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
       <property name="basePackage" value="com.longhang.mapper" />
   </bean>



</beans>

我们来理解下这个配置文件中的bean类

1  先创建的类bean id="dataSource",调用时java本身“org.springframework**类”-->  DataSource dataSource=new Data***();   Data***()对象中的参数注入了"driverClassName" “url”“user”“password”相当于new Data***("driverClassName",“url”,“user”,“password);这个对象就是spring用于连接数据库的

其中的赋值一定要和自己想要查询的数据库对应,不然就会各种报错


2  bean id=“sqlSessionFactory”  这是调用spring中的工厂类创建一个  --> SqlSessionFactory sqlSessionFactory=new SqlSession*****();对象。SqlSession*****()对象中的注入的是property(setter注入)参数,ref=“dateSource”表示向对象传递应用数据库  也就是说  new  SqlSession*****(Data***  dateSource);  注入连接池同时扫描数据库的映射mapper.xml文件


3 bean class="org.mybatis.spring.***" 这里没有id 他能自己加载getbeans()获取 获取映射器类中的对象StudentMapper studentMapper=new Mapper****();  <property****>setter注入对象  就是向其中传入第二步获取的工厂类

    StudentMapper studentMappe=new  Mapper****(SqlSession***** sqlSessionFactoryBeanName);

其中调用的方法可能很复杂我就用Data***SqlSession*****Mapper****来表示。


这里我们就实现ioc的思想

当我们需要对底层学生类(我的表格是student表)的数据进行修改的时候。就只需要通过SM框架中的接口调用sql语句对student数据库进行修改(增删改查),  而不需要对整个业务逻辑有关student对象的类进行修改


我们通过下面的对比来看看这三个步骤的具体实现

单独mybatis测试类   其中MybatisTools是封装了获取sessionFactory=new SqlSessionFactoryBuilder().build(in); {sessionFactory=null;return sessionFactory.openSession();}的方法

@Test
public void insert() {
//SQL 的语句insert into students (id,name,qq,wish,create_at)
   SqlSession session = MybatisTools.getSession();
   StudentMapper studentMapper = session.getMapper(StudentMapper.class);
   Student student = new Student(201L, "张三", "244558", "修仙", 2018L);
   studentMapper.insert(student);
   session.commit();
   session.close();
   logger.info(student.toString());
}

sm测试类

@Autowired
private StudentMapper studentMapper;
@Test
public void insert() {
//SQL 的语句insert into students (id,name,qq,wish,create_at)
   Student student = new Student(271L, "张三", "244558", "修仙", 2018L);
   studentMapper.insert(student);
   logger.info(student.toString());
}

sm框架  直接给我获取一个StudentMapper studentMapper对象  该对象直接调用原来接口中的方法就可以直接进行数据操作了。

而不需要想mybatis 那样 去创建一个工厂类来完成这些加载

跟不用和jdbcTemplate这个类 还需要实现接口  复写方法来操作语句  jdbc操作语句就更加原始了。


面向对象思想


sm框架实现数据库操作

昨天我把MybatisTset项目操作数据库所有代码都贴上去了 ,其实这个修改就是添加AppliactionContaxt.xml文件,  这个文件我在上面已经贴出来了,  然后把MybatisTest项目中的MybatisTools这个工具类删掉; 对test类修改如下

就完成成了SpringMybatisTest  SM框架的实现

import com.longhang.mapper.StudentMapper;
import com.longhang.model.Student;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringMybatisTestTest {
private static Logger logger = Logger.getLogger(SpringMybatisTestTest.class);
   @Autowired
   private StudentMapper studentMapper;
   @Test
   public void insert() {
//SQL 的语句insert into students (id,name,qq,wish,create_at)
       Student student = new Student(271L, "张三", "244558", "修仙", 2018L);
       studentMapper.insert(student);
       logger.info(student.toString());
   }
@Test
   public void update() {
//SQL 的语句update student set wish=#{wish} where Id=#{id}
       Student student = new Student(9L, "厉害");
       studentMapper.update(student);
   }
@Test
   public void delete() {
//SQL 的语句update student set wish=#{wish} where Id=#{id}
      studentMapper.delete(271L);
   }
@Test
   public void select() {
//SQL 的语句update student set wish=#{wish} where Id=#{id}
       Student student = studentMapper.select(3L);
       logger.info(student.toString());
   }
@Test
   public void getAll() {
//SQL 的语句update student set wish=#{wish} where Id=#{id}

       List<Student> student = studentMapper.getAll();
       Iterator<Student> it = student.iterator();
       while (it.hasNext()) {
// System.out.println(it.next());
           logger.info("" + it.next());
       }
//logger.info(student.toString());
   }
}


19.学习Spring,配置Spring和Junit

20.编写单元测试的代码,注意,你也可以尝试一下,先写单元测试的代码,再写接口,再写实现类


接着学习练习调试,学会查看单步执行时的变量值,Spring思想


Spring AOP   参考https://www.cnblogs.com/hongwz/p/5764917.html

https://www.w3cschool.cn/wkspring/omps1mm6.html

AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。OOP引入封装、继承、多态等概念来建立一种对象层次结构,用于模拟公共行为的一个集合。不过OOP允许开发者定义纵向的关系,但并不适合定义横向的关系,例如日志功能。日志代码往往横向地散布在所有对象层次中,而与它对应的对象的核心功能毫无关系对于其他类型的代码,如安全性、异常处理和透明的持续性也都是如此,这种散布在各处的无关的代码被称为横切(cross cutting),在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。

使用"横切"技术,AOP把软件系统分为两个部分:核心关注点横切关注点

AOP核心概念

1、横切关注点对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

2、切面(aspect)类是对物体特征的抽象,切面就是对横切关注点的抽象

3、连接点(joinpoint)

被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

4、切入点(pointcut)对连接点进行拦截的定义

5、通知(advice)所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

6、目标对象代理的目标对象

7、织入(weave)将切面应用到目标对象并导致代理对象创建的过程

8、引入(introduction)在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

Spring 中基于 AOP 的 XML架构  对xml文件初步看了下   明天再继续学文件里具体内容  今天对aop思想和概念熟悉  log4j应该就是这个思想做出来的


接着学习练习调试,学会查看单步执行时的变量值

参考如下

    http://www.cnblogs.com/chiangchou/archive/2017/09/05/idea-debug.html#_label3


变量值在断点这行的后面 

variables中显示所有的变量




明天计划的事情:  22.买一台服务器,阿里云或者是金山云都可以。部署数据库到远程DB,从本地直接连远程                         
遇到的问题: ApplacitionContext.xml文件配置内容了解 认识                          
收获:ioc思想


返回列表 返回列表
评论

    分享到