Spring—集成MyBatis

Spring—集成MyBatis

  • 把mybatis和spring集成在一起,像一个框架一样使用
  • 使用技术:ioc,因为ioc可以创建对象
  • 可以把mybatis框架中的对象交给spring统一创建,开发人员从spring获取对象
  • 开发人员就不用同时面对两个或多个框架,就面对一个spring

一、mybatis使用步骤,对象

  1. 定义dao接口,StudentDao

  2. 定义mapper文件,StudentDao.xml

  3. 定义主配置文件,mybatis.xml

  4. 创建dao的代理对象,StudentDao dao = SqlSession.getMapper(StudentDao.class);

    List student = dao.selectStudent();

  5. 需要SqlSessionFactory对象,使用Factory能获取SqlSession,有了SqlSession就能有dao,目的是获取dao对象,Factory创建需要主配置文件

主配置文件

  1. 数据库信息

    <environment id="development">
    	<!-- 事务提交方式:
    		JDBC:利用JDBC方式处理事务(commit rollback close)
    		MANAGED:将事务交由其他组件去托管(spring,jobss),默认关闭连接	
    	-->
    	<transactionManager type="JDBC" />
    	<!-- 数据源类型:
    		UNPOOLED:传统JDBC模式(每次都需要打开关闭数据库)
    		POOLED:使用数据库连接池 
    		JNDI:从tomcat中获取内置的数据库连接池
    	 -->
    	<dataSource type="POOLED">
    		<property name="driver" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://127.0.0.1:3306/s_t?useSSL=false" />
    		<property name="username" value="root" />
    		<property name="password" value="123456" />
    	</dataSource>
    </environment>
    
  2. mapper文件的位置

    <mappers>
    	<mapper resource="nuc/gjq/mapper/studentMapper.xml" />
    </mappers>
    
  3. spring会使用独立的连接池类替换mybatis默认自己带的,把连接池类也交给spring创建

spring创建的对象

  1. 独立的连接池对象,使用阿里的druid连接池
  2. SqlSessionFactory对象
  3. dao对象

主要学习以上三个对象的创建语法,使用xml的bean标签实现

二、项目步骤

  1. 新建maven项目
  2. 加入maven依赖
    1. spring依赖
    2. mybatis依赖
    3. mysql驱动
    4. spring事务的依赖
    5. mybatis和spring集成的依赖:mybatis官方用的,用来在spring项目中创建mybatis的SqlSessionFactory,dao对象
  3. 创建实体类
  4. 创建dao接口和mapper文件
  5. 创建mybatis主配置文件
  6. 创建Service接口和实现类,属性是dao
  7. 创建mybatis的配置文件:声明mybatis的对象交给spring创建
    1. 数据源
    2. SqlSessionFactory
    3. Dao对象
    4. 声明自定义的service
  8. 创建测试类,获取Service对象,通过service调用dao完成数据库的访问

三、重点

1.mybatis-mapper.xml

  • 创建一个Dao接口,就需要一个相应的xml文件

  • 代码实现

    public interface StudentDao {
          
          
        int insertStudent(Student student);
        List<Student> selectStudents();
    }
    
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="nuc.gjq.dao.StudentDao">
        <insert id="insertStudent">
            insert into student value (#{id},#{name},#{email},#{age})
        </insert>
        <select id="selectStudents" resultType="nuc.gjq.domain.Student">
            select * from student order by id desc
        </select>
    </mapper>
    

2.mybatis-cfg.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>
    <!--settins:控制mybatis全局行为-->
    <settings>
        <!--设置mybatis输出日志-->
        <setting name="" value=""/>
    </settings>
    <!--设置别名-->
    <typeAliases>
        <!-- 设置单个别名(忽略大小写) -->
        <!-- <typeAlias type="nuc.gjq.entity.Student" alias = "student"/> -->
        <!-- 批量设置别名 (自动将包中的所有类批量定义别名:别名就是类名(不带包名的类名))-->
        <package name="实体类所在的包名"/>
    </typeAliases>
    <!--sql.mapper(sql映射文件)的位置-->
    <mappers>
        <mapper resource="mapper文件所在的包名"/>
    </mappers>
</configuration>

3.applicationContext.xml(模板编程)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--把数据库的配置信息,写在一个独立的文件,编译修改数据库的配置内容,spring知道jdbc.properties文件的位置
    -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--声明数据源对象,作用是连接数据库-->
    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <!--set注入,给DruidDataSource提供连接数据库信息-->
        <!--使用属性配置文件中的数据,语法${key}-->
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="${jdbc.max}"/>
     </bean>
    <!--声明的是mybatis中提供的SqlSessionFactoryBean类,这个类内部创建SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--set注入,把数据库连接池付给了dataSource属性-->
        <property name="dataSource" ref="myDataSource" />
        <!--mybatis主配置文件的位置
            configLocation属性是Resource类型,由spring提供,读取配置文件
            他的的赋值,使用value,指定文件的路径,使用classpath:表示文件的位置
            固定格式
        -->
        <property name="configLocation" value="classpath:mybatis.xml" />
    </bean>
    <!--创建dao对象,使用SqlSession的getMapper(StudentDao.class)
        MapperScannerConfigurer:在内部调用Mapper()生成每个dao接口的代理对象
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定SqlSessionFactiony对象的id-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--指定包名,包名是dao接口所在的包名
            MapperScannerConfigurer会扫描这个包中的所有接口,把每个接口都执行一次getMapper()方法,得到每个接口的dao对象.
            创建好的dao对象放入到spring容器中
            dao对象的默认名称是接口名首字母小写
        -->
        <property name="basePackage" value="nuc/gjq/dao"/>
    </bean>
    <!--声明service-->
    <bean id="studentService" class="nuc.gjq.service.Impl.StudentServiceImpt">
        <property name="studentDao" ref="studentDao"/>
    </bean>
</beans>

猜你喜欢

转载自blog.csdn.net/qq_45414532/article/details/115361483