`

spring boot整合mybatis

阅读更多

1、新建maven工程,配置pom文件: 了解springcloud架构可以加求求:三五三六二四七二五九

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mysql连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

2、配置application.properties(放在在resources目录下) 

<!-- 配置mybatis配置文件位置 -->
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
<!--配置xml映射文件位置,这次是放在resources->mybatis->mapper目录下-->
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
<!-- 配置实体类位置 注意没有classpath在src/main/java目录下的包-->
mybatis.type-aliases-package=com.neo.entity

<!--数据库配置-->
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root

 

3、建一个main方法的入口类

注意:1、其它的所有类都必须在入口类的子包下。
2、使用MapperScan注解注入mapper接口的包。
3、使用@SpringBootApplication注解,声明此类为入口类

 @SpringBootApplication

@MapperScan("com.juan.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

5、采用mvc模式,各层类都使用注解注入分别是:@RestController 、@Service、@Controller
6、注意:springboot框架默认返回的数据都是json格式的。不需要修改返回数据格式  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics