myBatis: Native interface development, mapper proxy interface development

table of Contents

Native Interface Development Highlights

Agent Development Highlights Interface

 


The total configuration file config.xml . Total profile environment Configuration tab substantially unchanged, primarily related to configuration database and; by mappers labels and other mapper documents relating to such AccountMapper.xml, AccountRespository.xml and other specific interface or class xml file. Account information recording principal user names, passwords etc, for which a single configuration AccountRespository.xml, easy to change the search database by using additions and deletions java way interface.

<?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>
    <!-- 配置MyBatis运⾏环境 -->
    <environments default="development">
        <environment id="development">
            <!-- 配置JDBC事务管理 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- POOLED配置JDBC数据源连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"></property>
                <property name="username" value="root"></property>
                <property name="password" value="123456"></property>
            </dataSource>
        </environment>
    </environments>
<!--    注册各mapper-->
    <mappers>
<!--        原生接口配置文件-->
        <mapper resource="com/lmybatis/mapper/AccountMapper.xml"></mapper>
<!--        代理接口配置文件-->
        <mapper resource="com/lmybatis/repository/AccountRepository.xml"></mapper>
    </mappers>
</configuration>

 

Native Interface Development Highlights

After the new good database form t_account, configured in AccountMapper.xml file, the code is as follows. Code, the file path given namespace, id parameter statement given in the method invocation, java identified by the parameter to the label, while the SQL statements identified in the tag, the database CRUD operations; Code the realization of the insert and delete operations. parameterType is passed when calling database operations function parameter type. Note that after writing good AccountMapper.xml, to be introduced in total config.xml configuration file.

<?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.lmybatis.mapper.AccountMapper">

    <insert id="save" parameterType="com.lmybatis.entity.Account">
      insert into t_account(username,password,age) values(#{username},#{password},#{age})
    </insert>

    <delete id="remove" parameterType="int">
        delete from t_account where id = #{id}
    </delete>

</mapper>

Agent Development Highlights Interface

Each database form, are provided a proxy interface, such as Account class has t_account form {id username password age} field, then set a AccountRepository interface, the interface is defined by, cut individually Account, changing, checking function, after in yet AccountRepository.xml configuration database operations. In general, should the corresponding AccountRepository.xml each AccountRepository write interfaces.

 下面测试代理接口开发方式。使用的是AccountRepository接口中定义的方法(函数),如下:

发布了126 篇原创文章 · 获赞 7 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_36880027/article/details/104457839