您的位置:首页 > 文旅 > 美景 > 网页制作与设计实训心得_龙岗网站建设公司哪家口碑好_seo快速排名案例_互联网营销师报考条件

网页制作与设计实训心得_龙岗网站建设公司哪家口碑好_seo快速排名案例_互联网营销师报考条件

2025/7/15 19:06:24 来源:https://blog.csdn.net/qq_45055856/article/details/143304777  浏览:    关键词:网页制作与设计实训心得_龙岗网站建设公司哪家口碑好_seo快速排名案例_互联网营销师报考条件
网页制作与设计实训心得_龙岗网站建设公司哪家口碑好_seo快速排名案例_互联网营销师报考条件

一.查询

根据id来查询字段

package com.gjw;import com.gjw.mapper.EmpMapper;
import com.gjw.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;@SpringBootTest
class SpringbootMybatisCrudApplicationTests {@Autowiredprivate EmpMapper empMapper;@Testvoid contextLoads() {}@Testpublic void testSelectById() {Emp e = empMapper.selectById(1);System.out.println(e);}
}
package com.gjw.mapper;import com.gjw.pojo.Emp;
import org.apache.ibatis.annotations.*;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {@Select("select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp where id = #{id}")public Emp selectById(Integer id);  
}

这样查询出来的数据会有一定的问题,如下图所示:

这三个字段的数据并没有封装到mybatis当中去,因为字段名和实体类的属性名并不一致。在数据库字段中为dept_id,而在实体类属性名中却是驼峰命名的方式。

二.数据封装问题 

 三.如何解决

解决方法1:起别名,让数据库字段别名和实体类属性名保持一致

package com.gjw.mapper;import com.gjw.pojo.Emp;
import org.apache.ibatis.annotations.*;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {// 解决方法1:起别名,让数据库字段别名和实体类属性名保持一致@Select("select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime, update_time updateTime from emp where id = #{id}")public Emp select(Integer id);
}

 解决方法2:使用@Results,@Result注解手动映射封装

package com.gjw.mapper;import com.gjw.pojo.Emp;
import org.apache.ibatis.annotations.*;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {// 解决方法2:使用@Results,@Result注解手动映射封装@Results({@Result(column = "dept_id",property = "deptId"),@Result(column = "create_time",property = "createTime"),@Result(column = "update_time",property = "updateTime")})@Select("select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp where id = #{id}")public Emp select(Integer id);
}

解决方法3:开启mybatis的驼峰命名自动映射开关 --- a_column -----> aColumn(推荐) 

我们首先要在配置文件application.properties中进行配置,将开关开启

spring.application.name=springboot-mybatis-crud
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.password=
spring.datasource.username=root# 配置在控制台输出日志信息
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl# 配置mybatis的驼峰命名自动映射开关 --- a_column -----> aColumn(camel:骆驼)
mybatis.configuration.map-underscore-to-camel-case=true
package com.gjw.mapper;import com.gjw.pojo.Emp;
import org.apache.ibatis.annotations.*;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {// 解决方法3:开启mybatis的驼峰命名自动映射开关 --- a_column -----> aColumn@Select("select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp where id = #{id}")public Emp selectById(Integer id);
}

 这样就能自动解决数据封装问题了。

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com