Spring boot step integrated MyBatis follows:
1, the configuration dependent correlation jar in pom.xml
<! - Load mybatis integration springboot -> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <-! MySQL's jdbc driver package -> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2, the configuration in the core MyBatis configuration file application.properties Springboot in Mapper.xml file location:
# MyBatis configuration file is located in the Mapper.xml mybatis.mapper-locations=classpath:com/example/springboot01/mapper/*.xml
3, the configuration data in the configuration file source application.properties core Springboot of:
# Configure Data Source spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/exam?useUnicode=true&characterEncoding=utf8&useSSL=false
4, the new dao interfaces, add annotations Mapper @Mapper MyBatis in the interface or add @MapperScan ( "com.bjpowernode.springboot.mapper") running on the main scanning type package of annotations
@Mapper public interface TClassMapper { List<TClass> list(); }
or
5, write mybatis map file, used here maven plug-in generates automatic code generation for the mybatis
New service and controller
public interface TClassService { List<TClass> list(); } @Controller public class HelloController { @Autowired private TClassService tClassService; @RequestMapping("index.do") public @ResponseBody Object list(){ Return tClassService.list (); } }
If the service being given the injection, the tool is a problem, it can change warning