编程语言
首页 > 编程语言> > MyBatis创建HelloWorld程序

MyBatis创建HelloWorld程序

作者:互联网

DemoMapper接口

public interface DemoMapper {
    @Select("select 'Hello world'")
    String hello();
}
@MapperScan("cn.tedu.mapper")
public class MyBatisConfig {

    //配置SqlSessionFactory的作用是告诉mybatis如何找到目标数据库
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

}

测试

@Test
    public void testHelloWorld(){
        DemoMapper mapper = ctx.getBean("demoMapper",DemoMapper.class);
        String str = mapper.hello();
        System.out.println(str);
    }

标签:mapper,SqlSessionFactory,创建,数据库,HelloWorld,DemoMapper,bean,MyBatis,public
来源: https://blog.csdn.net/sinat_33940108/article/details/122724764