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

  • Post author:
  • Post category:其他




maven结构

在这里插入图片描述





原始mybatis Dao操作



mybatis配置(

https://mybatis.org/mybatis-3/zh/configuration.html

)



maven依赖
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>8.0.29</version>
</dependency>

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.3.21</version>
</dependency>


1. mybatis.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 加载resources下properties文件 -->
    <properties resource="jdbc.properties"></properties>

    <!-- 类型别名: Java 类型设置一个缩写名字 -->
    <typeAliases>
        <!-- 1. 直接指定类 -->
<!--        <typeAlias alias="User" type="com.codehorse.domain.User"/>-->
        <!-- 2. 指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean, 类名为Bean的首字母小写的非限定类名-->
        <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>

    <!-- 映射器:告诉mybatis << SQL映射语句 >> 在哪里 -->
    <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>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.29</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.11</version>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.9</version>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.7</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.21</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.21</version>
</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 {
}



版权声明:本文为m1015422754原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。