IDEA整合springBoot+myBatis
1.新建项目
①Create New Project
②点击左侧Spring Initialize
右侧Project SDK选择1.8、选择Default
点击next,下一步
③Group、Artifact可改可不改,修改是全小写
点击next,下一步
④点击左侧web 选中中间web框
在点击左侧SQL选中中间JPA、MySQL、JDBC、MyBatis
点击next,下一步
⑤选择项目名称和项目路径
点击finish,完成
2.项目内部结构
3项目搭建
1 package com.springbootmybatis.springbootdome.controller; 2 3 import com.springbootmybatis.springbootdome.domain.User; 4 import com.springbootmybatis.springbootdome.service.DomeService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 9 @RestController 10 @RequestMapping("/user") 11 public class DomeController { 12 @Autowired 13 private DomeService domeService; 14 15 @RequestMapping("/getUser") 16 private User getUser(String userName){ 17 return domeService.getUser(userName); 18 } 19 }
1 package com.springbootmybatis.springbootdome.service; 2 3 import com.springbootmybatis.springbootdome.domain.User; 4 import com.springbootmybatis.springbootdome.mapper.DomeMapper; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 @Service 9 public class DomeService { 10 @Autowired 11 private DomeMapper domeMapper; 12 13 public User getUser(String userName) { 14 return domeMapper.getUser(userName); 15 } 16 }
package com.springbootmybatis.springbootdome.mapper; import com.springbootmybatis.springbootdome.domain.User; import org.apache.ibatis.annotations.Param; public interface DomeMapper { User getUser(@Param("userName") String userName); }
1 package com.springbootmybatis.springbootdome; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 @SpringBootApplication 8 @MapperScan("com.springbootmybatis.springbootdome.mapper") 9 public class SpringbootdomeApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(SpringbootdomeApplication.class, args); 13 } 14 15 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.springbootmybatis.springbootdome.mapper.DomeMapper" > 4 <select id="getUser" resultType="com.springbootmybatis.springbootdome.domain.User"> 5 SELECT 6 * 7 FROM 8 `user` 9 WHERE 10 userName = #{userName} 11 </select> 12 </mapper>
1 spring: 2 profiles: 3 active: dev
1 server: 2 port: 8080 3 4 spring: 5 datasource: 6 username: root 7 password: 8 url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC 9 driver-class-name: com.mysql.cj.jdbc.Driver 10 11 mybatis: 12 mapper-locations: classpath:mapper/*Mapper.xml 13 type-aliases-package: com.springbootmybatis.springbootdome.domain 14 15 #showSql 16 logging: 17 level: 18 com: 19 example: 20 mapper : debug
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.4.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.springbootmybatis</groupId> 12 <artifactId>springbootdome</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>springbootdome</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-data-jpa</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-jdbc</artifactId> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-web</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>org.mybatis.spring.boot</groupId> 36 <artifactId>mybatis-spring-boot-starter</artifactId> 37 <version>2.0.1</version> 38 </dependency> 39 40 <dependency> 41 <groupId>mysql</groupId> 42 <artifactId>mysql-connector-java</artifactId> 43 <scope>runtime</scope> 44 </dependency> 45 <dependency> 46 <groupId>org.springframework.boot</groupId> 47 <artifactId>spring-boot-starter-test</artifactId> 48 <scope>test</scope> 49 </dependency> 50 </dependencies> 51 52 <build> 53 <plugins> 54 <plugin> 55 <groupId>org.springframework.boot</groupId> 56 <artifactId>spring-boot-maven-plugin</artifactId> 57 </plugin> 58 </plugins> 59 </build> 60 61 </project>
sql
注意事项:配置文件中不要出现中文注释,配置文件报红时,试试ctrl+tab,
springboot的启动类不能放在java目录下!!!必须要个包将它包进去
否则会报错误:
Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
controller、service、dao层和实体类不能放在启动类上级目录,因为启动类只能扫描到同级目录或下级目录。
为什么两个配置文件:
在项目中配置多套环境的配置方法。
因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置
文件中将用的配置文件写上就行如application.yml
笔记:在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:
application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
4.启动项目
将来的你一定会感谢今天努力的自己!加油!
~送给每一位正在拼搏的年轻人!