@Test

mybatis

mybatis-test

@Test
    public  void  getAllUser(){
        //获取获得SqlSession对象
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        //执行Sql
        UserDao mapper = sqlSession.getMapper(UserDao.class);//接口
        //获取UserDao接口的方法
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        //关闭SqlSession
        sqlSession.close();
    }

spring

spring-annotation-test

//这个也会Spring容器托管,注册到容器中,因为他本来就是个@Component
//@Configuration代表这是一个配置类,就和之前的beans.xml一样
@Configuration
//扫描包
@ComponentScan("com.my.pojo")
//引入新的bean

public class myConfig {
    //注册一个bean,就相当于我们之前写的一个bean标签
    //这个方法的名字,就相当于bean标签中的id属性值
    //这个方法的返回值,就相当于bean标签中的class属性
  @Bean
    public User getUser(){
      return new User();
  }
}
 @Test
    public void test1() {
                  //如果完全使用了配置类方式去做,我们就只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载。
                  ApplicationContext applicationContext = new AnnotationConfigApplicationContext(myConfig.class);
                  User  user = (User) applicationContext.getBean("getUser");//取方法名
                  String name = user.getName();
                  System.out.println(name);

              }

spring-xml-test

  @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");//id名
        userService.delete();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值