需求描述:使用SpringBoot集成MyBatis.
需求分析:
需要完成的几点要求:
1. 建议采用xml进行管理sql语句,不建议使用MyBatis提供的Sql注解功能。这样方便维护,在我们直接的企业业务系统中,一条sql语句可能会有很复杂的关联关系,直接写的接口中不好。
2. 自动扫描sql语句的xml文件,不需要在MyBatis的配置文件中再去做mapper映射
集成步骤:
1. 新建Maven项目
展示下我的项目结构:
- 修改pom文件,引入相关依赖
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 编写配置文件application.properties(application.yml)
application.properties:
#jdbc
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
#MyBatis
#mybatis.config-location=classpath:mybatis-config.xml
#mybatis.typeAliasesPackage=com.sivalabs.demo.domain
mybatis.mapperLocations=classpath:/mapper/*.xml
application.yml:
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
mybatis:
mapper-locations: /mapper/*.xml
application.properties和application.yml选一个就可以了
- 新建dao及dao的xml实现
dao接口代码:
@Mapper
public interface UserMapper {
@Select(value = { "select * from sfb_capital_user_project" })
public List<Map<String,Object>> list();
public List<Map<String,Object>> listxml();
}
Sql语句的xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.tyrone.springboot.integrate.mybatis.dao.UserMapper">
<select id="listxml" parameterType="java.util.Map" resultType="java.util.Map">
select * from sfb_capital_user_project
</select>
</mapper>
注意:
1. 在接口上是需要增加@Mapper这个注解。这个注解是用来向Spring窗口中注入的,如果采用自动扫描机制的话,用Spring的注解是不行的。若使用SqlSession方式,则可以使用Component注解。
2. 由于在设置了自动扫描的配置,所以这里可以用MyBatis的注解,例如:@Select,@Update(本人不推荐这种方式)。
- 创建启动类测试
application.java核心代码:
//@Configuration
//@EnableAutoConfiguration
//@ComponentScan
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application implements CommandLineRunner {
@Autowired private UserMapper userMapper;
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
public void run(String... arg0) throws Exception {
List<Map<String, Object>> list = userMapper.list();
System.out.println(list);
}
}
这里启动类实现CommandLineRunner这个接口进行测试
源代码链接:https://github.com/myNameIssls/springboot-study
参考链接:
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
SpringBoot快速入门
SpringBoot快速入门,不依赖SpringBoot父项目依赖