Spring整合MyBatis

一、配置。

1.spring配置类

package com.example.Config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan("com.example")
@Import({JdbcConfig.class, MyBatisConfig.class})
@PropertySource("classpath:jdbc.properties")
public class SpringConfig {

}

2.jdbc配置类

package com.example.Config;

import com.alibaba.druid.pool.DruidDataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;


import javax.sql.DataSource;
import java.beans.PropertyVetoException;
//标志spring配置类加载property文件的配置

public class JdbcConfig {
    //property文件的配置使用value配置的表达式取出
    @Value("${jdbc.driver}")
    private String driver ;
    @Value("${jdbc.url}")
    private String url ;
    @Value("${jdbc.username}")
    private String username ;
    @Value("${jdbc.password}")
    private String password ;
    //标注在方法上,该方法的返回值作为bean存在spring容器内
    @Bean("dataSource")
    public DataSource getDataSource() throws PropertyVetoException {
        //存储数据源接口池
        DruidDataSource druidDataSource = new DruidDataSource();
        //设置数据源参数
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        //返回数据源
        return druidDataSource;
    }
}

3.mybatis配置类

package com.example.Config;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MyBatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
//        实例化
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//若是其他文件则修改包路径
        sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.domain"); 
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
//在这可修改其他配置类
        msc.setBasePackage("com.example.Dao");
        return msc;
    }
}

4.整合Mybatis

Spring整合MyBatis_第1张图片

Spring整合MyBatis_第2张图片

二、具体实现。

1.pom.xml文件中导入依赖,

  由于之前的总是使用的是MyBatis中内置的连接池,现在使用Druid连接池。在resources目录下导入properties文件(用于存放数据库信息)

   
    
        
        
            org.springframework
            spring-test
            5.1.2.RELEASE
            test
        
      
        
        
            org.springframework
            spring-context
            5.1.2.RELEASE
        
 
        
 
        
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
        
            com.alibaba
            druid
            1.1.23
        
        
        
            org.mybatis
            mybatis
            3.5.6
        
        
        
        
            org.mybatis
            mybatis-spring
            2.0.6
        
        
        
            org.springframework
            spring-jdbc
            5.1.2.RELEASE
        
 
        
        
        
            org.slf4j
            slf4j-api
            1.7.20
        
        
        
            ch.qos.logback
            logback-classic
            1.2.3
        
        
        
            ch.qos.logback
            logback-core
            1.2.3
        
    

2.创建AccountDao接口

Dao层是用来与数据库操作的层,AccountDao就是操作Account表的类

public interface AccountDao {
    @Insert("insert into account(name,money)values(#{name},#{money})")
    void save(Account account);
    @Delete("delete from account where id = #{id} ")
    void delete(Integer id);
    @Update("update account set name = #{name} , money = #{money} where id = #{id} ")
    void update(Account account);
    @Select("select * from account")
    List findAll();
    @Select("select * from account where id = #{id} ")
    Account findById(Integer id);
}

到这就完成了spring整合mybatis,在业务层(service)可以调用AccountDao类下的方法。

你可能感兴趣的:(Spring系列,spring,mybatis,java)