天天看点

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测试,扫面包是否配置正确