使用mybatis查询数据库返回路径(地址)的原因

使用idea查询数据库中表的数据的时候,会出现地址,而不是表里面的信息,出现问题如下:

解决方案,在实体类中重写toString方法,要不然就会运行结果出现地址。

 

 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要创建一个学生信息表和一个教师信息表,以及一个关联表来表示学生和教师之间的关系。这里假设学生信息表为 "student",包含 "id"、"name"、"gender"、"age"、"teacher_id" 等字段;教师信息表为 "teacher",包含 "id"、"name" 等字段;关联表为 "student_teacher",包含 "id"、"student_id"、"teacher_id" 等字段。 接下来,我们使用 MyBatis 创建一个学生信息查询的映射文件 "StudentMapper.xml",包含一个 "select" 标签,用于查询所有学生信息以及他们所属的教师信息。其中,"resultMap" 标签用于定义查询结果集的映射关系,"association" 标签用于指定学生实体类中包含教师实体类的属性,"select" 属性用于指定该属性的查询 SQL 语句。 StudentMapper.xml 文件内容如下: ```xml <?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.example.mapper.StudentMapper"> <resultMap id="studentResultMap" type="com.example.entity.Student"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="gender" property="gender"/> <result column="age" property="age"/> <association property="teacher" column="teacher_id" select="com.example.mapper.TeacherMapper.getTeacherById" /> </resultMap> <select id="getAllStudents" resultMap="studentResultMap"> SELECT * FROM student </select> </mapper> ``` 其中,"resultMap" 标签中的 "type" 属性指定了映射的实体类类型,"id"、"result" 标签用于指定查询结果集中的列名和对应的属性名,"association" 标签用于指定学生实体类中包含教师实体类的属性,"column" 属性用于指定该属性在查询结果集中的列名,"select" 属性用于指定该属性的查询 SQL 语句。 接下来,我们需要创建一个教师信息查询的映射文件 "TeacherMapper.xml",包含一个 "select" 标签,用于查询教师信息。其中,"parameterType" 属性用于指定输入参数的类型,"resultMap" 属性用于指定查询结果的映射关系。 TeacherMapper.xml 文件内容如下: ```xml <?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.example.mapper.TeacherMapper"> <resultMap id="teacherResultMap" type="com.example.entity.Teacher"> <id column="id" property="id"/> <result column="name" property="name"/> </resultMap> <select id="getTeacherById" parameterType="int" resultMap="teacherResultMap"> SELECT * FROM teacher WHERE id=#{id} </select> </mapper> ``` 其中,"parameterType" 属性用于指定输入参数的类型,这里是一个整数类型的教师 ID,"resultMap" 属性用于指定查询结果的映射关系。 最后,我们需要创建一个 MyBatis 配置文件 "mybatis-config.xml",配置数据源和映射文件。具体来说,我们需要配置 "dataSource" 标签和 "mappers" 标签,前者用于指定数据源,后者用于指定映射文件路径mybatis-config.xml 文件内容如下: ```xml <?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis_test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/mapper/StudentMapper.xml"/> <mapper resource="com/example/mapper/TeacherMapper.xml"/> </mappers> </configuration> ``` 其中,"dataSource" 标签用于指定数据源,这里使用了一个 POOLED 类型的数据源;"mappers" 标签用于指定映射文件路径,这里配置了两个映射文件的路径。 完成以上步骤后,我们可以使用 MyBatis 查询所有学生信息以及他们所属的教师信息。具体来说,我们可以使用以下代码在 Java 中查询: ```java public class Main { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); try { List<Student> students = session.selectList("com.example.mapper.StudentMapper.getAllStudents"); for (Student student : students) { System.out.println(student); } } finally { session.close(); } } } ``` 其中,"SqlSessionFactory" 类用于创建 "SqlSession" 类的实例,"SqlSession" 类用于执行 SQL 语句。在查询所有学生信息的示例中,我们使用了 "selectList" 方法查询所有学生信息,"com.example.mapper.StudentMapper.getAllStudents" 参数用于指定查询语句的唯一标识符。查询结果将以 "List<Student>" 类型返回,我们可以使用循环遍历每个学生信息并输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值