<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                轉:愛信-韓曉彤 ## 一.Mybatis實體屬性與數據庫表列名映射的四種方法 ### 1. 通過xml映射文件resultMap ~~~ <mapper namespace="data.UserMapper"> <resultMap type="data.User" id="userResultMap"> <!-- 用id屬性來映射主鍵字段 --> <id property="id" column="user_id"/> <!-- 用result屬性來映射非主鍵字段 --> <result property="userName" column="user_name"/> </resultMap> </mapper> ~~~ 通過里面的id標簽和result標簽來建立映射關系,由property和column分別指定實體類屬性和數據表的列名。 ### 2. 通過注解 @Results 和 @Result > 這兩個注解是與XML文件中的標簽相對應的: > @Results對應resultMap > @Result對應result **這兩個注解是應用在方法的級別上的,也就是在mapper方法上,如下:** ~~~ @Select("select * from t_user where user_name = #{userName}") @Results( @Result(property = "userId", column = "user_id"), @Result(property = "userName", column = "user_name") ) User getUserByName(@Param("userName") String userName); ~~~ 由于注解是針對方法的,對于Mapper中的每個操作數據庫的方法都必須有相同的注解完成映射關系的建立,導致很多的配置是重復的; * 缺點: 如果要避免配置重復的問題,可以采用在XML配置文件中配置這個resultMap,然后再@Result中通過id屬性引用這個resultMap, 但是這樣感覺很麻煩(由于使用了兩種配置方式),不如直接使用基于XML的resultMap配置方式; ### 3. 通過屬性配置完成映射(推薦) > 駝峰命名法(Camel-Case): > 當變量名或函式名是由一個或多個單字連結在一起,而構成的唯一識別字時,首字母以小寫開頭,每個單詞首字母大寫(第一個單詞除外)。如userName Mybatis給我們提供了一種映射方式,如果屬性的命名是遵從駝峰命名法的,數據列名遵從下劃線命名 > 那么可以使用這種方式,類似如下: > userName對應user_name; > userId對應user_id; 配置代碼如下: ~~~ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); Configuration configuration = new Configuration(); configuration.setMapUnderscoreToCamelCase(true); sqlSessionFactoryBean.setConfiguration(configuration); ~~~ ### 4.通過使用SQL字段別名完成映射 這種方式最直接,直接在SQL語句中建立別名來完成映射,如下: ~~~ @Select("select user_name as userName, user_id as userId from t_user where user_name = #{userName}")User getUserByName(@Param("userName") String userName); ~~~ 參考 :http://blog.csdn.net/lmy86263/article/details/53150091 ## 二.在SpringBoot中完成mybatis自動掃描及屬性映射 1. mapper自動掃描 增加@MapperScan配置(注意請遵守工程目錄文件夾規范) ~~~ @SpringBootApplication @MapperScan(basePackages = {"net.aexit.infrastructure.*.mapper"}) public class InfrastructureApplication { public static void main(String[] args) { SpringApplication.run(InfrastructureApplication.class, args); }} ~~~ 2, 駝峰標示自動映射 **通過對四種映射方法比較,我們認為第三種是最規范,最有效率的!** 只需在application.properties增加配置即可! ~~~ mybatis.configuration.mapUnderscoreToCamelCase=true ~~~ 參考: http://www.cnblogs.com/zhangdong92/p/6986653.html ## 三.在SpringBoot中完成mybatis-plus自動掃描及屬性映射 通常我們的項目中會使用mybatis-plus,使用分頁插件 0. maven 引入 ~~~ <!-- mybatis-plus begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatisplus-spring-boot-starter</artifactId> <version>${mybatisplus-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatisplus.version}</version> </dependency> <!-- mybatis-plus end --> ~~~ 1. 配置mybatis-plus 將MapperScan配置由Application放到mybatis-plus配置上 ~~~ @Configuration @MapperScan(basePackages = {"net.aexit.infrastructure.*.mapper"}) public class MybatisPlusConfig { /** * mybatis-plus分頁插件<br> * 文檔:http://mp.baomidou.com<br> */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } ~~~ 2. 修改配置文件 mybatis配置換成mybatis-plus ~~~ mybatis-plus: configuration: mapUnderscoreToCamelCase: true ~~~ 3. 使用分頁插件 mapper接口定義 ~~~ List<SysUser> listUser(@Param("userId")String userId, @Param("userName")String userName, @Param("orgId")String orgId, @Param("phone")String phone, @Param("sex")Byte sex, @Param("email")String email, Page<SysUser> page); ~~~ mapper的xml定義,就是簡單的select,不含分頁,分頁由plus完成 ~~~ <select id="listUser" resultType="net.aexit.infrastructure.common.model.SysUser"> SELECT <include refid="Base_Column_List" /> FROM sys_user </select> ~~~ 調用 Page<SysUser> page = new Page<>(pageNo,pageSize); List<SysUser> sysUsers = sysUserExtMapper.listUser(userId,userName,orgId,phone,sex,email,page);、 ## 4. 返回List<Map> ~~~ <select id="selectByDetectSn" resultType="java.util.HashMap"> SELECT r.vehicle_no,report.loc_edit_time,report.detect_result,report.id FROM eds_detection_record r INNER JOIN eds_detection_report report ON r.detect_sn = report.detect_sn WHERE r.vehicle_no IN (SELECT a.vehicle_no FROM app_vehicle a WHERE a.user_id = #{userId}) <if test="vehicleNo != null"> AND r.vehicle_no LIKE "%"#{vehicleNo,jdbcType=VARCHAR}"%" </if> ORDER BY loc_edit_time DESC ; </select> ~~~ ~~~ List<Map<String,Object>> selectByDetectSn(@Param("userId")String userId, @Param("vehicleNo")String vehicleNo); ~~~ ~~~ /** * 獲取檢測結果 * @param userId * @param vehicleNo * @return */ public List<Map<String,Object>> getByDetectSn(String userId, String vehicleNo){ return edsDetectionRecordMapper.selectByDetectSn(userId,vehicleNo); } ~~~ ~~~ /** * 檢測結果 */ @RequestMapping(value = "/detectedRecord", method = RequestMethod.GET) @ResponseBody public AjaxCommonObject detectedRecord(@RequestParam("userId") String userId, @RequestParam(required = false) String vehicleNo) { AjaxCommonObject ajaxCommonObject = new AjaxCommonObject(); try { ajaxCommonObject.setData(bizService.getByDetectSn(userId,vehicleNo)); } catch (BizCommonException e) { return new AjaxCommonObject(e); } return ajaxCommonObject; } ~~~ ![](https://box.kancloud.cn/0aca0db2d48fb1d9dc2584d64c8680f2_797x525.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看