Mybatis application.properties file configuration steps

Steps to create a mybatis project:
    1. Create the database and the corresponding table
    2. Configure the data source
        2.1. Create the db.properties file, which contains information about connecting to the database
        2.2. Configure the information to be applied to the data source. The corresponding data source class is BasicDataSource ( Configure in the applicationContext.xml file)
        <!-- Read the db.properties file-->
        <util:properties id="dbconfig" location="classpath:db.properties"/>
        <!-- Configure the data source-- >
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="#{dbconfig.driver}"/>
            <property name="url" value= "#{dbconfig.url}"/>
            <property name="username" value="#{dbconfig.username}"/>
            <property name="password" value="#{dbconfig.password}"/>
            <property name="initialSize" value="#{dbconfig.initialSize}"/>
            <property name="maxActive" value="#{dbconfig.maxActive}"/>
        </bean>
    3. Insert data
        mybatis through mybatis Coding mode:
            1) Create an interface and declare the abstract method of the data range;
            2) Configure the xml mapping corresponding to the abstract method (the mapping is a one-to-one correspondence)
         3.1. Create "cn.tedu.mybatis.mapper.UserMapper" (hint The interface is generally placed in the mapper package)
         3.2, create "from n.tedu.mubatis.entity.User" entity class
         3.3, create a folder under "src/main/resource" (because mybatis has more mapping files, it is convenient Management) mappers, used to store the mapping file of mybatis (the mapping file name is recommended to be consistent with the interface name)
         3.4. Configuration mapping file (UserMapper.xml)
            <mapper namespace="cn.tedu.mybatis.mapper.UserMapper">
                 <!-- id: abstract method name
                      parameterType: the type of the parameter in the abstract method (delete or change without adding the paramterType attribute)
                  -->
                 <insert id="insert" parameterType="cn.tedu.mybatis.entity.User">
                     INSERT INTO t_user (
                         username,password,age ,phone,email
                     ) VALUES(
                         #{username},#{password},#{age},#{phone},#{email}
                     )
                 </insert>
            </mapper>
    4. Configure SqlSessionFactoryBean: tell mybatis how to connect to the database , What are the configuration files with sql statement
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- Specify the data source, the value is the data source configured above -->
            <property name="dataSource" ref="dataSource"/>
            <!-- Specify the location of the xml mapping file-->
            <property name="mapperLocations" value="classpath:mappers/*.xml"/>
        </bean>
    5. Configure MapperScannerConfigurer: tell mybatis where the interface is
        <bean class ="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- Specify the data source, the value is the data source configured above -->
            <property name="dataSource" ref="dataSource"/>
            <!-- Specify the xml mapping File location-->
            <property name="mapperLocations" value="classpath:mappers/*.xml"/>
        </bean>

Guess you like

Origin blog.csdn.net/D_J1224/article/details/107529901