1. 程式人生 > >spring boot (1) 配置mybatis (idea)

spring boot (1) 配置mybatis (idea)

話不多說,直接上程式碼和截圖:

第一步:(IDEA,其實就是引用了官網的初始化地址,使用maven構建)

第二步:

 

第三步:

Restful 和websocket這些包先不引用,用的時候在新增

第四步:

選擇資料使用的包,mybatis,postgre,jdbc 

 

第五步:

 

上面這些沒啥意思,估計人人都會,配置在下面,這裡使用了spring boot 提供的

mybatis-spring-boot-starter依賴,整合mybatis只需要宣告引數和路徑就可以了

 

第六步:

新增配置 

 

mybatis.type-aliases-package=com.kgo.helpmanual.common.entity
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml


spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url = jdbc:postgresql://localhost:5432/vendor?useUnicode=true&characterEncoding=utf8&autoReconnect=true&stringtype=unspecified
spring.datasource.username = postgres
spring.datasource.password = dream

 

第一行:實體類 

 

package com.kgo.helpmanual.common.entity;

public class ArticleEntity {


    private String id ;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


}

第二行:mybatis配置檔案路徑

 

第三行:mapper檔案路徑

第四行:資料庫驅動名

spring.datasource.driverClassName=org.postgresql.Driver

第五行:資料庫連線地址

spring.datasource.url = jdbc:postgresql://localhost:5432/vendor?useUnicode=true&characterEncoding=utf8&autoReconnect=true&stringtype=unspecified

第六行:使用者名稱

第七行:密碼

 

第七步:宣告Dao層包掃描

 

package com.kgo.helpmanual;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@MapperScan("com.kgo.helpmanual.common.dao")
public class HelpmanualApplication {

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

 

實體程式碼

package com.kgo.helpmanual.common.entity;

public class ArticleEntity {


    private String id ;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


}

dao程式碼

package com.kgo.helpmanual.common.dao;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ArticleDao {
    int getArticle();
}

 

mybatis配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>    
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>


 

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.kgo.helpmanual.common.dao.ArticleDao">

<select id="getArticle" parameterType="String" resultType="Integer">
	select count(*)
	from uum.authority a 

</select>


   
</mapper>

 

controller 程式碼

package com.kgo.helpmanual.controller;

import com.kgo.helpmanual.common.dao.ArticleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ArticleController {
    @Resource
    private ArticleDao articleDao;
    @RequestMapping("/artitle")
    public String getArticle(){
        return "測試文章介面";
    }
    @RequestMapping("/artitle2")
    public String getArticle2(){
        return String.valueOf(articleDao.getArticle());
    }

}

啟動類程式碼

package com.kgo.helpmanual;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@MapperScan("com.kgo.helpmanual.common.dao")
public class HelpmanualApplication {

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

 

注: 以上僅僅是demo 

 

工程原始碼:https://download.csdn.net/download/qq_26462567/10748167