芯が強い人になるESTJ-A

# 手写xml,不用注解,mybatis3的头部

IT開発 Tags: 无标签 阅读: 222
<?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="命名空间填写的是xml对应那个dao文件,接口包,写全路径">
    <select id="dao接口对应的方法名" resultType="返回值实体类包! Category,com.xuwen.javamall.pojo.Category">
        select * from mall_category where id = #{id}
    </select>

</mapper>

<?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.xuwen.javamall.dao.CategoryMapper">
    <select id="queryById" resultType="com.xuwen.javamall.pojo.Category">
        select * from mall_category where id = #{id}
    </select>

</mapper>

interface
dao包=mapper

package com.xuwen.javamall.dao;


import com.xuwen.javamall.pojo.Category;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;


public interface CategoryMapper {

    //mybatis注入写法
    @Select("select * from mall_category where id = #{id}")
    Category findById(@Param("id") Integer id);

    //xml写法
    Category queryById(Integer id);

}

别忘记配置,xml路径,否则报错,找不到路径


spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: mall
    password: passMall123
    url: jdbc:mysql://rm-uf6t9m0yx22dxrya5no.mysql.rds.aliyuncs.com:3306/mall?characterEncoding=UTF8&useSSL=false

mybatis:
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mappers/*.xml
logging:
  pattern:
    console: "[%thread] %-5level %logger{36} - %msg%n"

提取关键信息:

<?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.xuwen.javamall.dao.CategoryMapper">
    <sql id="Base_Column_List">
        id,parent_id,name,status,sort_order,create_time,update_time
    </sql>
    <select id="queryById" resultType="com.xuwen.javamall.pojo.Category">
        select
         <include refid="Base_Column_List"></include>
        from mall_category where id = #{id}
    </select>

</mapper>