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