原始mybatis Dao操作 和 spring整合mybatis Dao操作(注解形式)

maven结构

原始mybatis Dao操作 和 spring整合mybatis Dao操作(注解形式)_第1张图片


原始mybatis Dao操作

mybatis配置(https://mybatis.org/mybatis-3/zh/configuration.html)

maven依赖

<dependency>
	<groupId>mysqlgroupId>
	<artifactId>mysql-connector-javaartifactId>
	<version>8.0.29version>
dependency>

<dependency>
	<groupId>org.springframeworkgroupId>
	<artifactId>spring-contextartifactId>
	<version>5.3.21version>
dependency>

1. mybatis.xml配置文件


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    
    <properties resource="jdbc.properties">properties>

    
    <typeAliases>
        

        
        <package name="com.codehorse.dao"/>
    typeAliases>

    
    <environments default="development">
        <environment id="development">
            
            <transactionManager type="JDBC"/>
            
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            dataSource>
        environment>
    environments>

    
    <mappers>
    	
        <package name="com.codehorse.dao"/>
    mappers>
configuration>

2. mybatis执行Dao

	 //1. 读取mybatis配置文件
	 InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
	 //2. 创建sqlSessionFactoryBuilder对象
	 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
	 //3. 创建工厂类sqlSessionFactory
	 SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
	 //4. 通过sqlSessionFactory工厂类实例对象sqlSession 
	 SqlSession sqlSession = sqlSessionFactory.openSession();
	 //5. 获取Dao对象
	 UserDao userDao = sqlSession.getMapper(UserDao.class);
	 //6. 执行Dao操作
	 User user = userDao.findById(1);
	 System.out.println(user);
	//7. 关闭连接
	sqlSession.close();`

spring整合mybatis

mave依赖

<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>8.0.29version>
dependency>

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>1.2.11version>
dependency>

<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>3.5.9version>
dependency>

<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatis-springartifactId>
    <version>2.0.7version>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
    <version>5.3.21version>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>5.3.21version>
dependency>

1. 先配置jdbc数据源(配置在spring配置中读入了)

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

2. 配置MybatisConfig

public class MybatisConfig {
    // sqlSession工厂类  (使用spring整合的工厂类)
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean (DataSource dataSource) {
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        //类别名
        ssfb.setTypeAliasesPackage("com.codehorse");
        //数据源
        ssfb.setDataSource(dataSource);
        return ssfb;
    }

    // mapper扫描映射器
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        // 将包内的映射器接口实现全部注册为映射器
        msc.setBasePackage("com.codehorse.dao");
        return msc;
    }
}

3. spring配置文件

@Configuration
@ComponentScan("com.codehorse")
@PropertySource("jdbc.properties")
@Import({JdbcConfig.class, MybatisConfig.class})
public class SpringConfig {
}

你可能感兴趣的:(Spring家族,mybatis,spring,java)