Spring与MyBatis的整合

MyBatis-动态SQL语句控制数据库
Spring-创建业务逻辑层管理
两者整合-通过Spring业务逻辑层管理jdbc数据

搭建基础环境:
1.导入mybatis与Spring相关jar包;
2.创建applicationContext.xml与mybatis-config.xml文件;

//mybatis-config.xml文件内容


<configuration>
    
    <typeAliases>
        <package name="com.pojo.entity" />
    typeAliases>
configuration>
//applicationContext.xml内容



<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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/user?
                        useUnicode=true&characterEncoding=utf-8"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml">property>
bean>

<bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.pojo.dao.PersonMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory">property>
bean>

  <context:component-scan base-package="com.pojo.dao,com.pojo.service"/>
beans>

3.创建对象实体类,创建逻辑层(mapper),创建service层与其实现层(比mybatis省略了逻辑层的实现层,由mapper.xml文件的动态语句代替)。

你可能感兴趣的:(spring,mybatis,学习笔记)