Spring Learning_Integrate Mybatis

这么多jar:
mybatis-spring. jar
spring-tx. jar
spring-jdbc. jar
spring-expression. jar
spring-context- support. jar
spring-core. jar
spr ing-context. jar
spring-beans. jar
spring-raop. jar
spring-web. jar
commons-logging. jar
commons-jdbc. jar
ojdbc. jar
mybatis. jar
log4. jar
commons -pool. jar

之前用到了很多,就不贴链接了。

mybatis-spring. jar: https://mvnrepository.com/artifact/org.mybatis/mybatis-spring/1.3.2
1. Create tables and classes

create table mstudent(
  id number primary key ,
  name varchar2(10),
  age number
);
insert into mstudent values (1,'张三',15);
insert into mstudent values (2,'李四',25);
insert into mstudent values (3,'王五',36);
select * from mstudent;
public class Student {
    
    
    private int id;
    private String name;
    private int age;
}

2. Mybatis configuration file conf.xml (can be saved after optimization)

3. Establish a mapping relationship through mapper.xml

<?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="com.student.mapper.StudentMapper">
    <insert id="addStudent" parameterType="com.student.entity.Student">
        insert into mstudent values (#{id},#{name},#{age})
    </insert>
</mapper>

4. Generate mapper objects through spring

<?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:mybatis="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<!--    加载数据库配置文件-->
    <bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="locations">
            <array>
                <value>classpath:db.properties</value>
            </array>
        </property>
    </bean>
<!--    配置数据库信息,替代mybatis配置文件-->
    <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
<!--    在springIOC容器中创建MyBatis的核心类SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--        添加数据源-->
        <property name="dataSource" ref="datasource"/>
<!--        只能有一个主配置文件,加载mybatis配置文件-->
<!--        <property name="configLocation" value="classpath:conf.xml"/>-->
        <!--        加载mybatis的配置文件mapper-->
        <property name="mapperLocations" value="com/student/mapper/*.xml">
        </property>
    </bean>
    <bean id="studentMapper" class="com.student.Dao.Impl.StudentDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <bean id="studentService" class="com.student.service.impl.StudentServiceImpl">
        <property name="studentMapper" ref="studentMapper"/>
    </bean>
</beans>

Optimized integration

conf.xml optimization

        <!--    第一种方式生成mapper对象-->
        <!--<bean id="studentMapper" class="com.student.Dao.Impl.StudentDaoImpl">
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>-->

    <!--    第二种方式生成mapper对象-->
    <!--<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.student.mapper.StudentMapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>-->

    <!--    第三种,批量产生的mapper默认是首字母小写的接口名-->
    <bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--指定批量产生的包        -->
        <property name="basePackage" value="com.student.mapper"/>
     </bean>

May your heart be like flowers and trees

Guess you like

Origin blog.csdn.net/nbcsdn/article/details/99679874