天天看點

Spring整合MyBatis快速入門之純注解

Spring注解整合MyBatis

分析:将Spring核心配置檔案所有内容遷移到Spring核心配置類中

Spring整合MyBatis快速入門之純注解

1、JDBC配置類

package com.project.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public class JdbcConfig1 {
    @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
    public DataSource getDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
           

2、MyBatis配置類

package com.project.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public class MyBatisConfig1 {
	/**
	 *擷取SqlSessionFactoryBean 
	 */
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource) {
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setTypeAliasesPackage("com.project.domain");
        ssfb.setDataSource(dataSource);
        return ssfb;
    }

	/**
	 *加載mybatis映射配置的掃描,作為spring的bean進行管理
	 */
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.project.dao");
        return msc;
    }
}
           

3、核心配置類,導入上面的配置

package com.project.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")//包掃描
@PropertySource("classpath:jdbc.properties")//引入外部配置檔案
@Import({JdbcConfig.class,MyBatisConfig.class})//導入其他配置資訊
public class SpringConfig {
}
           

資源檔案Jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db
jdbc.username=root
jdbc.password=admin
           

4、service的開發

package com.project.service.impl;

import com.project.dao.AccountDao;
import com.project.domain.Account;
import com.project.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
	
    @Autowired
    private AccountDao accountDao;
	/**
     * @param id
     * @return 傳回指定id的Account對象
     */
    public Account findById(Integer id) {
        return accountDao.findById(id);
    }
}
           

5、mapper接口開發,這裡使用注解

package com.project.dao;

import com.project.domain.Account;
import org.apache.ibatis.annotations.Select;

public interface AccountDao1 {
	/**
     * @param id
     * @return 傳回指定id的Account對象
     */
    @Select("select * from account where id = #{id}")
    Account findById(Integer id);
}
           

6、實體類

package com.project.domain;

import java.io.Serializable;

public class Account implements Serializable {

    private Integer id;
    private String name;
    private Double money;
    /*省略getter,setter.toString..方法*/
}
           

7、測試類(簡單測試)

import com.project.config.SpringConfig;
import com.project.dao.AccountDao;
import com.project.domain.Account;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.List;

public class App {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        AccountDao bean = ctx.getBean(AccountDao.class);
        Account beanById = bean.findById(1);
        System.out.println(beanById);
    }
}

控制台列印結果:
Account{id=1, name='1231', money=121.0}
           

總結:

将Spring核心配置檔案中所有内容遷移到Spring核心配置類中,有需要的都交給Spring容器管理