栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring

优势:

1 方便程序测试

2 方便集成各种优秀的框架

3 AOP面向切面编程

4 方便解耦,简化开发

5 降低javaEE API的开发难度

6 优秀的Java代码学习源码

7 程序书写符合高内聚,低耦合的远测,

耦合: 代码书写过程中所使用技术的结合紧密度,用于衡量软件中各个模块之间的互联程度

内聚:代码书写过程中单个模块内部各组成部分间的联系,用于衡量软件中各个功能模块内部的功能联系

Spring 的ioc容器

其实就是一个大的Map集合,ioc(控制反转)其实就是控制应用程序所需要的外部资源,将Spring运行需要的资源全部放到ioc容器中

      Bean的属性

id:唯一标识

class:指向资源对应的类

name:别名

scope:控制产生对象的数量是否为单例(单例与非单例创建时机)

init-method:生命周期创建

destroy-method:生命周期销毁

工厂:兼容遗留系统使用工厂创建对象的方式

整合第三方技术:使用工厂方式得到第三方技术的对象





    
    



    
    



       
    


    
    


    
    
    
    

Spring的DI(依赖注入) 

应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称为注入

注入方式

    1  set注入

引用类型注入:ref配合定义bean

简单类型注入:value,直接写 

  1. 构造器注入:关注顺序(了解)
  2. 集合注入(5种,重点掌握list,props-prop)
  3. P命名空间(了解)
  4.  Spring EL(了解)



    
        
        
        
        
    

    
    
        
        
        
    


    
        
        
        
    

    
        
        
        
    


    
        
        
        
    

    
        

        
        
        
        
    

    
        
        
        
    


    
        
        
    

    
        
            
                ma
                66666
            
        
        
            
                a666
                666666
            
        
        
            
                123456
                66666
            
        
        
            
                dgf
                66666
            
        
        
            
                
                
            
        
    


    


    
        
        
        
        
    


实用配置:

1 加载properties文件

(1)开启context命名空间:

(2)加载配置文件

(3)引用配置文件中的相关属性${name}



    
    
    
        
        
    

    
    
        
        
    

    
        
    

2 团队开发:

3 整合第三方技术:DI注入

4 核心上下文对象 ApplicationContext:

整合mybatis:

maven坐标


        
        org.mybatis
        mybatis
        3.5.3
    
        
            mysql
            mysql-connector-java
            5.1.47
        
        
            org.springframework
            spring-context
            5.1.9.RELEASE
        
        
            org.springframework
            spring-jdbc
            5.1.9.RELEASE
        
        
            com.alibaba
            druid
            1.1.16
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
    

核心配置·文件




    
    
    

    
    
        
        
        
        
    

    
    
        
    

    
    
        
        
    

    
    
        
    


propertiesw

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db
jdbc.username=root
jdbc.password=itheima
基于xml开发的加载Spring配置文件并且获取容器中的相关bean
public class App {
    public static void main(String[] args) {
        //参数是字符串的可变数组
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据bean的id获取相关的bean
        AccountService accountService = (AccountService) ctx.getBean("accountService");
        Account ac = accountService.findById(2);
        System.out.println(ac);
    }
}
纯注解开发:

jdbc配置类:

public class JDBCConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")
    public DataSource getDataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}

mybatis配类:

public class MyBatisConfig {

    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setTypeAliasesPackage("com.itheima.domain");
        ssfb.setDataSource(dataSource);
        return ssfb;
    }

    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setbasePackage("com.itheima.dao");
        return msc;
    }

}

Spring配置类:

@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@import({JDBCConfig.class,MyBatisConfig.class})
public class SpringConfig {
}

加载配置类:

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
    }
}

 jdbc配置文件:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/具体的数据库名
jdbc.username=root
jdbc.password=root

maven坐标


        
        
            org.mybatis
            mybatis
            3.5.3
        
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
        
            org.springframework
            spring-context
            5.1.9.RELEASE
        
        
        
            org.springframework
            spring-jdbc
            5.1.9.RELEASE
        
        
        
            com.alibaba
            druid
            1.1.16
        
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
        
            junit
            junit
            4.12
        
        
        
            org.springframework
            spring-test
            5.1.9.RELEASE
        
        
            org.springframework
            spring-context
            5.3.4
            compile
        
    

基本的框架搭建完成!

回顾一下mybatis纯注解开发!

//仅仅作为参考示例!
public interface AccountDao {

    @Insert("insert into account(name,money)values(#{name},#{money})")
    void save(Account account);

    @Delete("delete from account where id = #{id} ")
    void delete(Integer id);

    @Update("update account set name = #{name} , money = #{money} where id = #{id} ")
    void update(Account account);

    @Select("select * from account")
    List findAll();

    @Select("select * from account where id = #{id} ")
    Account findById(Integer id);
}

完成基本的xml文件开发集和纯注解开发!基本上可以作为参考示例!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/338058.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号