欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  Java

springboot项目创建教程(IntelliJIDEA,springboot 2.0 +mybatis)

程序员文章站 2022-03-21 19:56:37
...

本篇文章给大家带来的内容是关于springboot项目创建教程(IntelliJIDEA,springboot 2.0 +mybatis),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

环境:

JDK8+windows10

步骤 New Module —>Spring Initializr—>next

1

springboot项目创建教程(IntelliJIDEA,springboot 2.0 +mybatis)

2.

springboot项目创建教程(IntelliJIDEA,springboot 2.0 +mybatis)

3。web勾选web,sql里面可以不勾,后续添加,另外,勾选了MyBatis会报错Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 这样的错误。这里我勾选了是为了待会解决这个错误

springboot项目创建教程(IntelliJIDEA,springboot 2.0 +mybatis)

选完直接下一步到最后就行了

自动生成的pom文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <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>
    </dependencies>

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

</project>

## 目录结构springboot项目创建教程(IntelliJIDEA,springboot 2.0 +mybatis)

因为没有配置数据库和任何文件,application是空的 ,默认端口是8080

我们需要在 在启动累忽略数据库配置

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

然后 用postman访问一下 说明项目是正常的

springboot项目创建教程(IntelliJIDEA,springboot 2.0 +mybatis)

4.配置数据库 application.yml 文件

server:
  port: 8080
  tomcat:
    uri-encoding: UTF-8
  servlet:
    context-path: /
spring:
  dataSource:
    url: jdbc:mysql://localhost:3306/db-test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false
    username: root
    password: 123456
    driverClassName: com.mysql.jdbc.Driver
mybatis:
  mapper-locations: classpath:com/example/demo/mapper/*Mapper.xml #注意:一定要对应mapper映射xml文件的所在路径
  type-aliases-package: com.example.demo.model # 注意:对应实体类的路径
  configuration:
    call-setters-on-nulls: true # 解决使用map类型接收查询结果的时候为null的字段会没有的情况
这时候 com.mysql.jdbc.Driver 标红 说明mysql架包找不到

springboot项目创建教程(IntelliJIDEA,springboot 2.0 +mybatis)

<scope>runtime</scope> 去掉, 他的意思是运行时不需要,实际上是需要的

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

改成 版本默认就行

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

然后driverClassName 恢复正常

导入 service dao xml (这里没有使用实体类返回,使用的是map返回,所以表我们可以随意自建)

UserController
@Controller
@RequestMapping("/usersDemo")
public class UserController {
    private static Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    UserService userService;

    @ResponseBody
    @RequestMapping(value = "/test", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
    public List<Map<String, Object>> test(){
        log.info("进入了test方法!");
        List<Map<String,Object>> list=userService.userQueryAll();
        return list;
    }
}
UserService
public interface UserService {
    List<Map<String, Object>> userQueryAll();
}
UserserviceImpl
@Service
public class UserserviceImpl  implements UserService {
    @Resource
    UserMapper userDao;
    @Override
    public List<Map<String, Object>> userQueryAll() {
        return userDao.userQueryAll();
    }
}

UserMapper

@Mapper
public interface UserMapper {
    List<Map<String, Object>> userQueryAll();
}

UserMapper.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="com.example.demo.dao.UserMapper">
    <select id="userQueryAll" parameterType="Map" resultType="Map">
        SELECT * FROM `users`
    </select>
</mapper>
DemoApplication
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.example.demo.dao")
@ComponentScan(basePackages = {"com.example.demo.*"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

好了 ,

接下来就是启动了

5启动

注意 :依赖注入后 如果没有去掉 exclude = {DataSourceAutoConfiguration.class}

启动会报如下错误:

Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

错误原因1:

很多人说 创建项目的时候勾选了mybatis导致
  实际上是这个架包的原因
如果不要的话也不能使用mybatis了 
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

错误原因2 ,

dataSource.type 使用默认的连接池导致的

解决方案1 :去掉 exclude = {DataSourceAutoConfiguration.class} ,就能正常启动

解决方案2 :使用其他连接池 ,比如阿里 druid 连接池

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.13</version>
</dependency>

然后修改application.yml 里面的 spring.dataSource.type=com.alibaba.druid.pool.DruidDataSource

就可以不删除 exclude = {DataSourceAutoConfiguration.class}了 ,实际上意义不大,属于低级错误

springboot项目创建教程(IntelliJIDEA,springboot 2.0 +mybatis)

6 继续启动,

日志是没错了, 然后访问数据库的时候 可能会出现

这样的错误

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.demo.dao.UserMapper.userQueryAll

错误可能原因 1. mapper.xml select 的 id写错了

解决方案:检查代码

错误可能原因 2. 程序没有编译xml文件

解决方案:pom.xml 文件 build里面 增加代码

<resources>
<resource>
    <directory>src/main/java</directory>
    <includes>
        <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
</resource>
</resources>

再次启动 :

成功

springboot项目创建教程(IntelliJIDEA,springboot 2.0 +mybatis)

最终 pom.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- alibaba的druid数据库连接池监控依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.13</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        </resources>
    </build>

</project>

以上就是springboot项目创建教程(IntelliJIDEA,springboot 2.0 +mybatis)的详细内容,更多请关注其它相关文章!

相关标签: springboot