您的位置:首页 > 编程语言 > Java开发

记录一次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不同的配置

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: