记录一次idea+SpringBoot+mybatis使用中遇到的坑
2019-04-20 20:59
609 查看
1、show dependencies
<dependencies> <!--springboot自动重启,热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional><!--别的项目依赖该项目 使之不传递依赖--> </dependency> <!--web模块,里面有tomcat 启动后可以web访问--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--MyBatis Spring-Boot-Starter will help you use MyBatis with Spring Boot--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <!--mysql driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--有啥用暂时不知道--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--对象转json用的,会和lombok冲突,先注释了--> <!--<dependency>--> <!--<groupId>com.fasterxml.jackson.core</groupId>--> <!--<artifactId>jackson-core</artifactId>--> <!--<version>2.7.3</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>com.fasterxml.jackson.core</groupId>--> <!--<artifactId>jackson-databind</artifactId>--> <!--<version>2.7.3</version>--> <!--</dependency>--> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <!--自动添加setter\getter之类的,对象数据化--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!--自动重启必须加这个--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <!--//该配置必须--> </configuration> </plugin> </plugins> <!--解决编译问题--> <resources> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources> </build>
2、application.properties
spring.application.name=spring-boot-config server.port=8080 #mybatis.config-location=classpath:mybatis-config.xml #mybatis mapper文件的位置 mybatis.mapper-Locations=classpath:mapper/*Mapper.xml #扫描pojo类的位置,在此处指明扫描实体类的包,在mapper中就可以不用写pojo类的全路径名了 mybatis.type-aliases-package=com.example.demo.pojo spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.url=jdbc:mysql://localhost:3306/exercise?serverTimezone=UTC spring.datasource.username=root spring.datasource.password=Zh1203369541 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.devtools.restart.enabled=true spring.devtools.restart.additional-paths=src/main mybatis.mapper-locations=classpath:mapper/*.xml
3、项目结构
4、实体类
package com.example.demo.pojo; import lombok.Data; @Data public class User{ private String id; private String name; }
5、接口UserDao
package com.example.demo.dao; import com.example.demo.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface UserDao { User getUserById(@Param("id") String id); }
6、DemoApplication
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @ComponentScan("com.example") @MapperScan("com.example") @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); System.out.println("hello"); } }
7、HelloController
package com.example.demo.controller; import com.example.demo.dao.UserDao; import com.example.demo.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired private UserDao userDao; @RequestMapping("/hello") public User hello(){ User user = userDao.getUserById("1"); return user; } }
8、mapper
<?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="com.example.demo.dao.UserDao"> <select id="getUserById" resultType="User"> SELECT ID as id, NAME as name FROM user WHERE ID = #{id} </select> </mapper>
9、show u result
Tips:
1、idea没导包没报红错,第一次用很难受,自动导包设置:
https://www.cnblogs.com/mithrandirw/p/8819314.html
2、更改pom文件之后,注意右上角弹出的小框,import change~~
或者点击 auto import
3、注意属性文件.properties和.yml不同的配置
相关文章推荐
- 记录问题:springboot中使用Mybatis-pagehelper分页插件遇到的版本兼容问题
- 在idea里使用SpringBoot整合MyBatis时遇到的Mapper扫描不到的问题
- idea使用SpringBoot和Spring和Mybatis来实现增删改查
- 使用idea+springboot+Mybatis搭建web项目
- 记录使用SpringBootTest遇到的坑
- Idea使用Maven创建多模块SpringBoot项目问题记录
- springboot+mybatis+gradle在idea和oracle使用
- 使用IDEA构建Spring boot2.0+版本+MyBatis(properties文件配置)
- IDEA创建一个spring-boot整合Mybatis的模块时,遇到的问题
- 记录一次SpringbootJpa的使用
- 使用idea启动spring boot项目时遇到The Tomcat connector configured to listen on port 8080 failed to start
- 记录spring boot使用中遇到的问题
- 使用工具idea 2017.2开发,gradle构建项目,使用的技术有spring-boot、mybatis
- 使用idea+springboot+Mybatis搭建web项目
- 记录SpringBoot使用Druid和Mybatis配置
- 使用IDEA搭建SpringBoot项目整合Mybatis踩的坑。。。。
- 使用SpringBoot+Mybatis+postgresql在IDEA中搭建
- 记录一次 spring boot + mybatis 与分页控件整合
- 使用idea 搭建一个 SpringBoot + Mybatis + logback 的maven 项目
- 使用idea+springboot+Mybatis搭建web项目