自学内容网 自学内容网

Spring05——整合mybatis

整合mybatis

导入相关坐标

        <!-- 导入spring的坐标 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <!-- mybatis的坐标 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!-- 导入spring操作jdbc的坐标 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <!-- 导入spring操作mybatis的坐标 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!--数据库连接池组件  -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

这里要注意sping的版本和mybatis-spring的版本要对应 

接下来配置些文件

jdbc.properties里放连接mysql数据库的配置

properties文件里等号右边如果是字符串千万别写引号""呀!!!

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///dqlsearch?useSSL=false
jdbc.username=root
jdbc.password=123456

jdbc.properties配置好后导入到jdbcconfig里

package com.itheima.config;

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

import javax.sql.DataSource;


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;
    }
}

之后配置spring配置文件

package com.itheima.config;

import jdk.nashorn.internal.objects.annotations.Property;
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
@PropertySource("jdbc.properties")
@ComponentScan("com.itheima")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}

配置MybatisConfig

package com.itheima.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        // 对应mybatisxml配置文件下的<typeAliases>标签里的package
        ssfb.setTypeAliasesPackage("com.itheima.Object");
        // 配置数据库的基本属性(账号密码url等) 
        ssfb.setDataSource(dataSource);
        return ssfb;
    }

    @Bean  
    public MapperScannerConfigurer mapperScannerConfigurer(){  
        MapperScannerConfigurer msc = new MapperScannerConfigurer(); 
        // 对应mybatisxml配置文件下的</mappers>里的package
        msc.setBasePackage("com.itheima.mapper");  
        return msc;  
    }
    // 配置完后mybatisxml配置文件就可以不要了
}

如果这里的SqlSessionFactoryBean总是爆红那么参考下面的解决方法

https://blog.csdn.net/m0_46486963/article/details/120390842

配置完后mybatisxml配置文件就可以不要了

这时在main函数里使用即可

    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
    UserMapper userMapper = ctx.getBean(UserMapper.class);
    User ac = userMapper.SelectStudentById(2);
    System.out.println(ac);

整合JUnit

首先导入依赖

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
创建测试类文件(注意文件名)

 测试类里的代码

@RunWith(SpringJUnit4ClassRunner.class) // 加载测试方法
@ContextConfiguration(classes = SpringConfig.class) // 加载配置的spring
public class UserTest {
    @Autowired // 自动注入
    private UserMapper userMapper;

    @Test
    public void TestSelectStudentById(){
        System.out.println(userMapper.SelectStudentById(2));
    }
}

原文地址:https://blog.csdn.net/2301_80412275/article/details/144299033

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!