发表于: 2019-11-06 23:04:22

2 972


今天完成的事情:因为重装了系统所以重新搭建了一下环境

IDEA配置Maven的时候出了BUG

Maven不从仓库下载JAR包(耗费了很多时间。。)

解决办法:https://www.cnblogs.com/gigi2653/p/10694202.html

大概意思就是:MAVEN有两个仓库 本地和及时快照 当本地没有就去及时快照仓库去下

但是及时快照仓库分四个时间段更新

学习Spring

Spring 最认同的技术是控制反转的依赖注入(DI)模式。控制反转(IoC)是一个通用的概念,它可以用许多不同的方式去表达,依赖注入仅仅是控制反转的一个具体的例子。

通过引入IOC容器,利用依赖关系注入的方式,实现对象之间的解耦

public class Test {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

        Customer customer = (Customer) context.getBean("customer");

        System.out.println(customer);

 

<!--指定文件路径-->

    <bean id="helloBean" class="com.cwz.HelloSpring">

        <!--通过property给参数赋值-->

        <property name="name" value="全全好棒棒"/>

    </bean>

  <!--  <bean id="customer" class="com.cwz.autowiredDemo.Customer">

        <property name="person" ref="person"/>

    </bean>

    <bean id="person" class="com.cwz.autowiredDemo.Person">

        <property name="hey" value="辰辰好棒棒"/>

    </bean>-->

    <!---->

    <!--第二种方式-->

    <!--ByName方式自动装配-->

   <!-- <bean id="customer" class="com.cwz.autowiredDemo.Customer" autowire="byName"/>

    <bean id="address2" class="com.cwz.autowiredDemo.Address">

        <property name="myaddress" value="FPS冲"/>

    </bean>-->

    <!--第三种方式-->

    <bean id="customer" class="com.cwz.autowiredDemo.Customer" autowire="byType"/>

    <bean id="address2" class="com.cwz.autowiredDemo.Address">

        <property name="myaddress" value="冲"/>

    </bean>

三种方式

第一种默认的REF

第二种byName

如果一个bean的名称与其他bean属性的名称是一样的,那么将自动装配它。例子:Customer.java 增加一个“address”属性,那么Spring会在配置文件里寻找【id=address】的bean,如果找到了就自动装配,如果没找到,就什么都不做

第三种byType

类型自动装配,通过判断一个bean属性的数据类型与其他bean的数据类型相同,自动装配

拿上面的例子说,Customer类里的属性address的类型是“Address”,与“Address” bean中的数据类型是一样的,所以Spring会通过setter方法自动装配 setAddress(Address address)

如果配置文件里创建了两个一样类型的bean,就会装配失败,程序不知道要选择哪个。
所以,如果使用按类型自动装配,那么要确保bean只有唯一一个数据类型声明。

流程猜想
执行: ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
此时根据路径找到配置文件,创建了一个上下文对象
执行: Customer customer = (Customer) context.getBean("customer");
  • 此时通过对象获取配置文件里【id=customer】的配置,
  • 根据配置自动生成Person对象(property name="person"的作用)
  • 通过 ref="person"连接到【id=person】的配置,
  • 通过【property name="hei" value="hello"】给Person类里的hei变量赋值“hello”
  • 然后通过Person.java里重写的toString获得返回值“Person{hei='hello'}”
  • 然后回传给Customer里的person变量
  • 然后通过Customer.java里重写的toString获得返回值“Customer{person=Person{hei='hello'}}”

spring+mybatis整合

测试类

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class MybatisTest {

 

    @Autowired

    private CategoryMapper categoryMapper;

 

    @Test

    public void testAdd() {

        Category category = new Category();

        category.setName("new Category");

        categoryMapper.add(category);

    }

 

    @Test

    public void testList() {

        System.out.println(categoryMapper);

        List<Category> cs=categoryMapper.list();

        for (Category c : cs) {

            System.out.println(c.getName());

        }

明天计划的事情:spring+mybatis整合的BUG调好并测试 理解一下Mybatis
遇到的问题:写在里面了
收获:能够写简单的BEAN 写测试类测试 整合了Mybatis和Spring增加了一条数据库数据

熟练安装JDK MAVEN等环境。。。


返回列表 返回列表
评论

    分享到