天天看點

Spring-Boot學習筆記(Spring內建mybatis和mybatis的基礎配置)

Spring-Boot學習筆記1

  1. Idea建立spring-boot并內建Mybatis
    1. File->new->Project 選擇Spring initializr
    2. Next—>Next 選擇Web 勾選Spring Web Starter
    3. 選擇Sql 選擇Mybatis SpringFramwork
  2. 編寫Controller ,啟動Project
@RestController

public class HelloController {



    @Autowired

    private EtcProxyService etcProxyService;



    @RequestMapping(value = "/hello")

    private String helloWord(String userPhone) {

       return "spring-boot";
           

    }

注意啟動工程需要屏蔽myBatis jar包,否則啟動會報錯

  1. 預設端口是8080,在浏覽器啊中輸入localhost:8080/hello 傳回spring-boot
  2. 修改工程預設端口和通路路徑
server.port=8090

server.servlet.context-path=/demo
           
此時通路浏覽器位址應為:localhost:8090/demo/hello
           
  1. 配置資料源

在application.properties 下新增資料庫配置檔案

extend.validationQuery=SELECT 1

extend.jdbc.url=

extend.jdbc.username=

extend.jdbc.password=

extend.jdbc.driver=com.mysql.cj.jdbc.Driver
           
  1. 建立資料源配置檔案
  2. @Configuration @MapperScan("com.he.demo.springboot.dao") public class DataSourceExtend {     @Value("${extend.validationQuery}")     private String validationQuery;     @Value("${extend.jdbc.password}")     private String password;     @Value("${extend.jdbc.url}")     private String jdbcUrl;     @Value("${extend.jdbc.username}")     private String userName;     @Value("${extend.jdbc.driver}")     private String jdbcDriver;     @Bean(name = "dataSource")     public DruidDataSource instanceDataSource() {         DruidDataSource dataSourceSource = new DruidDataSource();         dataSourceSource.setUrl(jdbcUrl);         dataSourceSource.setMaxActive(20);         dataSourceSource.setUsername(userName);         dataSourceSource.setPassword(password);         dataSourceSource.setMinIdle(0);         dataSourceSource.setDefaultAutoCommit(false);         dataSourceSource.setDriverClassName(jdbcDriver);         dataSourceSource.setValidationQuery(validationQuery);         return dataSourceSource;     }
  3. 建立mybatis-config.xml 配置檔案
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration

        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-config.dtd">



<configuration>

    <settings>

        <setting name="lazyLoadingEnabled" value="false"/>

        <setting name="mapUnderscoreToCamelCase" value="true"/>

    </settings>

</configuration>
           
  1. 建立掃描包相關配置檔案
mybatis.config=mybatis-config.xml

mybatis.typealiasespackage=com.he.demo.springboot.model

mybatis.mapper-locations=classpath*:mapper/*.xml
           

10、建立sqlSessionFactory 配置檔案

@Configuration

public class SqlSessionFactory {



    @Value("${mybatis.config}")

    String mybatisConfigFilePath;

    @Value("${mybatis.typealiasespackage}")

    String typeAliasesPackage;

    @Value("${mybatis.mapperlocations}")

    String mapperPath;



    @Autowired

    @Qualifier("dataSource")

    private DataSource dataSource;



    @Bean

    public SqlSessionFactoryBean instanceSqlSessionFactory() throws Exception{

      SqlSessionFactoryBean ssb = new SqlSessionFactoryBean();

      ssb.setConfigLocation(new ClassPathResource(mybatisConfigFilePath));

      PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

      String packagePath = mapperPath;

      ssb.setMapperLocations(resolver.getResources(packagePath));

      ssb.setDataSource(dataSource);

      ssb.setTypeAliasesPackage(typeAliasesPackage);

      return ssb;

    }





}
           

注意:1、驅動程式應有com.mysql.jdbc.Driver 換成 com.mysql.cj.jdbc.Driver

2、注意掃面的Dao包

3、配置MapperLocation時,如果配置檔案帶有ClassPath* 則在sqlsessionfactory

時,需要去掉classPath*。

  1. Mode類
  2. 編寫Dao層
  3. 實作Mapper,
  4. Service
  5. serviceImpl
  6. Controller 層傳回

備注:10-15 和spring內建mybatis 無差異

掃描mapperLocation時路徑需要正确,否則會提示org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

也可以通過【注解】的方式編寫SQL測試,掃面包是否配置正确