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

对spring的理解(三)(二刷视频笔记整理)

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

对spring的理解(三)(二刷视频笔记整理)

目录

一、spring整合mybatis

1、回顾mybatis

第一步加依赖:

第二步提供接口和mapper文件:

第三步编写mybatis主配置文件:

第四步加载配置文件获取代理生成的接口实现类对象:

2、spring整合mybatis

 第一步加依赖:

第二步提供接口和mapper文件:

第三步配置类中第三方bean放入spring容器:

第四步编写spring主配置文件,将用到的配置类、资源文件加载进来:

第五步加载配置文件获取代理生成的接口实现类对象:

3、spring整合junit单元测试 (课外小知识)

第一步加依赖:

第二步在测试类上加注解,编写方法测试: 


一、spring整合mybatis

1、回顾mybatis

我是这样理解的,单独用mybatis我们的步骤如下:

第一步加依赖:

mybatis依赖:

    
      org.mybatis
      mybatis
      3.5.6
    

mysql依赖:

    
      mysql
      mysql-connector-java
      5.1.47
    

第二步提供接口和mapper文件:

可以通过注解的方式将接口和mapper文件(sql语句)二合一:

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 findAll();

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

第三步编写mybatis主配置文件:

定义别名,连接信息,mapper扫描器,资源文件扫描器




    
    
        
    
    
        
            
            
                
                
                
                
            
        
    
    
        
    

第四步加载配置文件获取代理生成的接口实现类对象:
public class App {
    public static void main(String[] args) throws IOException {
        // 1. 创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        // 2. 加载SqlMapConfig.xml配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml.bak");
        // 3. 创建SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        // 4. 获取SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 5. 执行SqlSession对象执行查询,获取结果User,,,获取到了动态代理生成的实现类对象
        AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
    }
}

2、spring整合mybatis

 第一步加依赖:

mybatis依赖:

    
      org.mybatis
      mybatis
      3.5.6
    

mysql依赖:

    
      mysql
      mysql-connector-java
      5.1.47
    

spring依赖:

    
      org.springframework
      spring-context
      5.2.10.RELEASE
    

mybatis-spring依赖:

    
      org.mybatis
      mybatis-spring
      1.3.0
    

 spring连接jdbc依赖:

    
      org.springframework
      spring-jdbc
      5.2.10.RELEASE
    

 druid连接池依赖(会用到):

    
      com.alibaba
      druid
      1.1.16
    
第二步提供接口和mapper文件:

可以通过注解的方式将接口和mapper文件(sql语句)二合一:

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 findAll();

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

第三步配置类中第三方bean放入spring容器:

懂得都懂,我就不做过多赘述了。我的理解是将SqlSessionFactoryBean(实际上指的是SqlSessionFactory)和MapperScannerConfigurer两个bean放入spring容器中,spring就会通过动态代理创建出接口的实现类对象来。

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;
    }
}

第四步编写spring主配置文件,将用到的配置类、资源文件加载进来:

引入其他配置类,标签扫描器,资源文件扫描器

@Configuration
@ComponentScan("com.itheima")
//@PropertySource:加载类路径jdbc.properties文件
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}

第五步加载配置文件获取代理生成的接口实现类对象:

这里没有直接获取生成的dao接口代理类对象,而是直接从容器里获取的service对象,因为我已经把dao接口的实现类对象注入到了service属性中去了,可以通过service对象来调用dao接口实现类对象。

public class App2 {
    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);
    }
}

3、spring整合junit单元测试 (课外小知识) 第一步加依赖:

    junit
    junit
    4.12
    test



    org.springframework
    spring-test
    5.2.10.RELEASE

第二步在测试类上加注解,编写方法测试: 

设置类运行器
@RunWith(SpringJUnit4ClassRunner.class)
设置Spring环境对应的配置类
@ContextConfiguration(classes = {SpringConfiguration.class}) //加载配置类

//设置类运行器
@RunWith(SpringJUnit4ClassRunner.class)
//设置Spring环境对应的配置类
@ContextConfiguration(classes = {SpringConfiguration.class}) //加载配置类
//@ContextConfiguration(locations={"classpath:applicationContext.xml"})//加载配置文件
public class AccountServiceTest {
    //支持自动装配注入bean
    @Autowired
    private AccountService accountService;
    @Test
    public void testFindById(){
        System.out.println(accountService.findById(1));

    }
    @Test
    public void testFindAll(){
        System.out.println(accountService.findAll());
    }
}

好,先到这里,午休,下午干aop和spring事务。 

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

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

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