SpringBoot+Mybatis+postgresql

SpringBoot:正是在這樣的一個背景下被抽象出來的開發框架,它本身並不提供Spring框架的核心特性以及擴展功能,只是用於快速、敏捷地開發新一代基於Spring框架的應用程序。也就是說,它並不是用來替代Spring的解決方案,而是和Spring框架緊密結合用於提升Spring開發者體驗的工具。總一個一個字爽

postgresql:PostgreSQL是一個開源的、對象關係型數據庫管理系統(ORDBMS)

廢話不多說上代碼!

第一步:新建項目

點擊finaly結束

第二步:下載jar包

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 加載postgresql驅動 -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- 加載jdbc連接數據庫 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- 加載mybatis jar包 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- 數據源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.9</version>
        </dependency>

第三步:編寫實體類 dao層 service層 控制層 *.xml

實體類

dao層

service層

控制層

 

寫*.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">
<!--namespace mapper接口 必填 隨便填值 防止SQL語句ID重名-->
<mapper namespace="com.szxs.dao.HaCmdDao">

    <!--查詢前5條數據-->
    <select id="queryHaCmdList" resultType="HaCmd">
          select * from ha_cmd limit 5;
    </select>

</mapper>

第四步 :最重要的配置

 

#端口號
server.port=8080 
#前綴後綴
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.jsp
#thymeleaf start
spring.thymeleaf.mode=jsp
spring.thymeleaf.encoding=UTF-8
#開發時關閉緩存,不然沒法看到實時頁面
spring.thymeleaf.cache=false
#thymeleaf end
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#配置url
spring.datasource.url=jdbc:postgresql://地址:5432/數據庫?useUnicode=true&characterEncoding=utf-8
#用戶名
spring.datasource.username=用戶名
#密碼
spring.datasource.password=密碼
#驅動
spring.datasource.driver-class-name=org.postgresql.Driver
mybatis.mapper-locations=classpath:/mybatis/*.xml
mybatis.type-aliases-package=com.szxs.demo
mybatis.configuration.auto-mapping-behavior=full
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

第五步:配置啓動類

package com.xka;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication
@ComponentScan("com.xka")       //加載@Service @Control註解類
@MapperScan(value = "com.xka.dao")  //mybatis 需要掃描mapper接口 dao層
@EnableWebMvc                   //啓用mvc
@EnableTransactionManagement    //啓用事務管理
public class DemoApplication {

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

}

但是如果要運行,必須要使用json格式,也就是說要在controller控制層打

@ResponseBody

 

發佈了28 篇原創文章 · 獲贊 15 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章