首頁 > Java > java教程 > 主體

springboot專案創建教學(IntelliJIDEA,springboot 2.0 +mybatis)

不言
發布: 2019-03-08 15:44:08
轉載
4735 人瀏覽過

這篇文章帶給大家的內容是關於springboot專案創作教學(IntelliJIDEA,springboot 2.0 mybatis),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

環境:

JDK8 windows10

步驟New Module —>Spring Initializr—>next

#1

2.

3。 web勾選web,sql裡面可以不勾,後續添加,另外,勾選了MyBatis會報錯Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 這樣的錯誤。這裡我勾選了是為了待會解決這個錯誤

選完直接下一步到最後就行了

自動產生的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>
登入後複製

## 目錄結構

因為沒有配置資料庫和任何文件,application是空的,預設連接埠是8080

我們需要在啟動累忽略資料庫配置

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

然後用postman存取 說明項目是正常的

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架包找不到
登入後複製

#把runtime 去掉, 他的意思是執行時不需要,其實是需要的

<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();
}
登入後複製

UserMappererr. ##好了,

接下來就是啟動了 

5啟動

注意:依賴注入後如果沒有去掉exclude = {DataSourceAutoConfiguration.class}

#啟動會報如下錯誤:

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

錯誤原因1:##

<?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>
登入後複製

錯誤原因2 ,

DemoApplication
登入後複製

解決方案1 :去掉exclude = {DataSourceAutoConfiguration.class} ,就能正常啟動

 

解決方案2 :使用其他連接池,例如阿里druid 連接池 

@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);
    }

}
登入後複製

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

就可以不刪除exclude = {DataSourceAutoConfiguration.class}了,實際上意義不大,屬於低階錯誤

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裡面增加程式碼 

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

再次啟動:

成功 

########################################################### #最終pom.xml檔如下###
dataSource.type 使用默认的连接池导致的
登入後複製

以上是springboot專案創建教學(IntelliJIDEA,springboot 2.0 +mybatis)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!