Mybatis configuration file (mybatis-config.xml) and Mapper mapping file (XXXMapper.xml) template

Configuration file

${dirver} ---> com.mysql.jdbc.Driver

${url} ---> jdbc:mysql://localhost:3306/database name

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <environments default="development">
<!--配置数据源,即数据库信息-->
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
<!--引入映射文件,路径:src/main/resources/mapper/BlogMapper.xml-->
  <mappers>
    <mapper resource="mapper/BlogMapper.xml"/>
  </mappers>
</configuration>

Mapper mapping file

Mybatis is interface-oriented programming and needs to maintain two consistencies:

(1) The namespace in the mapping file is consistent with the full class name of the Mapper interface;

(2) The id in the sql statement in the mapping file needs to be consistent with the method name of the Mapper interface;

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
  <select id="selectBlog" resultType="Blog">
    select * from Blog where id = #{id}
  </select>
</mapper>

Guess you like

Origin blog.csdn.net/qq_53376718/article/details/133318221