POM配置
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency>
web.xml中指定的spring配置
<context:component-scan base-package="com.smart.daoMybatis"/> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> <!-- Mybatis config --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 指定mapper.xml配置文件 --> <property name="mapperLocations" value="classpath:mybatis/BoardMapper.xml" /> </bean>
java实现:
@Repository public class BoardMybatisDao extends BaseMybatisDao<User> { @Autowired SqlSessionFactory sqlSessionFactory; public List<Board> getAllBoards() { SqlSession sqlSession = sqlSessionFactory.openSession(); List<Board> allBoards = sqlSession.getMapper(BoardMapper.class).getAllBoards(); return allBoards; } }
开发经验:IDEA安装Free mybatis plugin插件后
1、自动对Mapper.java和Mapper.xml进行跳转关联
2、只在mapper.java定义函数,通过alt+enter生成xml的mapper语句
进一步选择SQL语句