当前位置: 移动技术网 > IT编程>开发语言>Java > Spring整合MyBatis快速入门之纯注解

Spring整合MyBatis快速入门之纯注解

2020年07月17日  | 移动技术网IT编程  | 我要评论

Spring注解整合MyBatis
分析:将Spring核心配置文件所有内容迁移到Spring核心配置类中
整合流程

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容器管理

本文地址:https://blog.csdn.net/drug1910951117/article/details/107379042

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网