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框架大概就先讲这么多,有什么问题欢迎大家在下面留言!