Java笔记七:spring框架

spring框架是什么:

spring的理念,整合现有框架,使现有的框架更加实用。spring可以控制反转(IOC),面向切面(AOP)。是一个非侵入式轻量级框架(容器)。

spring核心概念:
  • IOC控制反转:使用对象时,由主动new产生对象转换为外部提供对象,此过程中对象创建控制权利由程序转到了外部,此思想称为控制反转。通俗来说就是不自己new对象,让spring帮咱们new。
  • IOC思想的实现:spring提供了一个容器,称为“IOC容器”。IOC容器负责对象的创建、初始化等工作,被创建的或者管理的对象,在IOC中有一个名词叫:Bean
  • DI依赖注入:在容器中创建Bean与Bean之间的关系,就叫做依赖注入。
依赖注入的方式:
  • setter注入:引用类型注入
    在这里插入图片描述

  • setter注入:简单类型注入
    在这里插入图片描述

  • 构造方法注入:引用类型注入
    在这里插入图片描述

  • 构造方法注入:简单类型注入
    在这里插入图片描述

spring注解开发:

这里有几个重点注解:

  • @Controller:用于表现层bean定义
  • @Service:用于业务层bean定义
  • @Repository:用于数据层bean定义
纯注解开发:
  • @Configuration注解用于设定当前类为配置类
  • @ComponentScan注解用于设定扫描路径,此注解只能添加一次,多个数据请用数组格式
  • 读取Spring核心配置文件初始化容器对象切换为读取Java配置类初始化容器对象
//加载配置文件初始化容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//加载配置类初始化容器
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
注解开发依赖注入:

使用@Autowired注解开启自动装配模式(按类型)
使用@Value实现简单类型注入

spring整合mybatis

整合mybatis之前先了解一下注解开发管理第三个bean

  • 先定义一个配置类,在配置类中定义一个方法,用@Beab注解注释,表示当前方法的返回值是一个bean对象,添加到IOC容器中。用引用类型装配,
  • 说明:引用类型注入只需要为bean定义方法设置形参即可,容器会根据类型自动装配对象
注解开发总结:

在这里插入图片描述
回头来看spring整合mybatis,直接上代码实现。

  • 第一步、先导入依赖。在pom.xml中添加spring-context、druid、mybatis、mysql-connector-java等基础依赖。准备service和dao层基础代码
public interface AccountService {

    void save(Account account);

    void delete(Integer id);

    void update(Account account);

    List<Account> findAll();

    Account findById(Integer id);

}
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    public void save(Account account) {
        accountDao.save(account);
    }

    public void update(Account account){
        accountDao.update(account);
    }

    public void delete(Integer id) {
        accountDao.delete(id);
    }

    public Account findById(Integer id) {
        return accountDao.findById(id);
    }

    public List<Account> findAll() {
        return accountDao.findAll();
    }
}
public interface AccountDao {

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

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

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

    @Select("select * from tbl_account")
    List<Account> findAll();

    @Select("select * from tbl_account where id = #{id} ")
    Account findById(Integer id);
}
  • 第二步:创建JDBCConfig配置DataSource数据源
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
    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}
  • 第三步:创建mybatisConfig整合mybatis
public class MybatisConfig {
    //定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setTypeAliasesPackage("com.itheima.domain");
        ssfb.setDataSource(dataSource);
        return ssfb;
    }
    //定义bean,返回MapperScannerConfigurer对象
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.itheima.dao");
        return msc;
    }
}
  • 第四步:创建springConfig主配置类进行包扫描和加载其他配置类
@Configuration
@ComponentScan("com.itheima")
//@PropertySource:加载类路径jdbc.properties文件
@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);

        AccountService accountService = ctx.getBean(AccountService.class);

        Account ac = accountService.findById(1);
        System.out.println(ac);
    }
}

spring框架大概就先讲这么多,有什么问题欢迎大家在下面留言!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值