SpringBoot整合Mybatis配置版

1、新建一個(gè)Spring Initializr項(xiàng)目
image.png
2、創(chuàng)建項(xiàng)目的文件結(jié)構(gòu)以及jdk的版本
image.png
3、選擇項(xiàng)目所需要的依賴
image.png
4、修改項(xiàng)目名,finish完成
image.png
5、來看下建好后的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>
 
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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>1.3.2</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>
6、修改配置文件

本文不使用application.properties文件,而使用更加簡潔的application.yml文件。將resource文件夾下原有的application.properties文件刪除掉,創(chuàng)建application.yml配置文件(備注:其實(shí)SpringBoot底層會把a(bǔ)pplication.yml文件解析為application.properties),本文創(chuàng)建了兩個(gè)yml文件(application.yml和application-dev.yml),分別來看一下文件中的內(nèi)容:

  • application.yml:
spring:
  profiles:
    active: dev
  • application-dev.yml:
server:
  port: 8080
 
spring:
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver
 
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.example.entity
 
#showSql
logging:
  level:
    com:
      example:
        mapper : debug

兩個(gè)文件的意思是

在項(xiàng)目中配置多套環(huán)境的配置方法。
因?yàn)楝F(xiàn)在一個(gè)項(xiàng)目有好多環(huán)境,開發(fā)環(huán)境,測試環(huán)境,準(zhǔn)生產(chǎn)環(huán)境,生產(chǎn)環(huán)境,每個(gè)環(huán)境的參數(shù)不同,所以我們就可以把每個(gè)環(huán)境的參數(shù)配置到y(tǒng)ml文件中,這樣在想用哪個(gè)環(huán)境的時(shí)候只需要在主配置文件中將用的配置文件寫上就行,如application.yml。

筆記:在Spring Boot中多環(huán)境配置文件名需要滿足application-{profile}.yml的格式,其中{profile}對應(yīng)你的環(huán)境標(biāo)識,比如:
application-dev.yml:開發(fā)環(huán)境
application-test.yml:測試環(huán)境
application-prod.yml:生產(chǎn)環(huán)境
至于哪個(gè)具體的配置文件會被加載,需要在application.yml文件中通過spring.profiles.active屬性來設(shè)置,其值對應(yīng){profile}值。

7、然后開始創(chuàng)建實(shí)體類實(shí)現(xiàn)業(yè)務(wù)流程

創(chuàng)建包c(diǎn)ontroller、entity、mapper、service。resources下創(chuàng)建mapping文件夾,用于寫sql語句,也可以用注解的方式直接寫在mapper文件里。

數(shù)據(jù)庫表結(jié)構(gòu):

CREATE TABLE `user` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `userName` varchar(32) NOT NULL,
  `passWord` varchar(50) NOT NULL,
  `realName` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

entity.java:

package com.example.entity;
 
/**
 * @Author:wjup
 * @Date: 2018/9/26 0026
 * @Time: 14:39
 */
public class User {
    private Integer id;
    private String userName;
    private String passWord;
    private String realName;
 
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public String getUserName() {
        return userName;
    }
 
    public void setUserName(String userName) {
        this.userName = userName;
    }
 
    public String getPassWord() {
        return passWord;
    }
 
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
 
    public String getRealName() {
        return realName;
    }
 
    public void setRealName(String realName) {
        this.realName = realName;
    }
 
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                ", realName='" + realName + '\'' +
                '}';
    }
}

UserController.java:

package com.example.controller;
 
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @Author:wjup
 * @Date: 2018/9/26 0026
 * @Time: 14:42
 */
 
@RestController
@RequestMapping("/testBoot")
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @RequestMapping("getUser/{id}")
    public String GetUser(@PathVariable int id){
        return userService.Sel(id).toString();
    }
}

UserService.java:

package com.example.service;
 
import com.example.entity.User;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
/**
 * @Author:wjup
 * @Date: 2018/9/26 0026
 * @Time: 15:23
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public User Sel(int id){
        return userMapper.Sel(id);
    }
}

UserMapper.java:

package com.example.mapper;
 
import com.example.entity.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
 
/**
 * @Author:wjup
 * @Date: 2018/9/26 0026
 * @Time: 15:20
 */
@Repository
public interface UserMapper {
 
    User Sel(int id);
}

UserMapping.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.mapper.UserMapper">
 
    <resultMap id="BaseResultMap" type="com.example.entity.User">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <result column="passWord" jdbcType="VARCHAR" property="passWord" />
        <result column="realName" jdbcType="VARCHAR" property="realName" />
    </resultMap>
 
    <select id="Sel" resultType="com.example.entity.User">
        select * from user where id = #{id}
    </select>
</mapper>
8、最終框架結(jié)構(gòu)
image.png
9、完成以上,下面在啟動類里加上注解用于給出需要掃描的mapper文件路徑
package com.example;
 
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@MapperScan("com.example.mapper") //掃描的mapper
@SpringBootApplication
public class DemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
10、最后啟動,瀏覽器輸入地址看看吧:http://localhost:8080/testBoot/getUser/1
image.png

基本的框架搭建成功。

贈送:

最后給個(gè)番外篇如何更改啟動時(shí)顯示的字符拼成的字母,就是更改下圖標(biāo)紅框的地方:

image.png

其實(shí)很好改,只需要在resources下新建一個(gè)txt文件就可以,命名為banner.txt,那這種字符該怎么拼出來呢,下面推薦一個(gè)網(wǎng)址,有這種工具,鏈接傳送門:字母轉(zhuǎn)字符。如下:
image.png

直接輸入要生成的字母,系統(tǒng)會自動轉(zhuǎn)換,然后復(fù)制下面轉(zhuǎn)換好的字符到新建的banner.txt文件中,重新啟動項(xiàng)目就可以了。
image.png

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容