发表于: 2018-03-16 21:56:00
1 669
今日完成
日报回复:
1,如果文件中存在多个properties文件,需要怎么处理?
原生的mybatis我没有查到可以加载多个.properties文件的资料,只查到了可以通过spring方式 加载多个properties文件:
<!-- 将多个配置文件位置放到列表中 -->
<bean id="propertyResources" class="java.util.ArrayList">
<constructor-arg>
<list>
<!-- 这里支持多种寻址方式:classpath和file -->
<value>classpath:/opt/demo/config/demo-db.properties</value>
<!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
<value>file:/opt/demo/config/demo-mq.properties</value>
<value>file:/opt/demo/config/demo-remote.properties</value>
</list>
</constructor-arg>
</bean>
<!-- 将配置文件读取到容器中,交给Spring管理 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" ref="propertyResources" />
</bean>
2,注解式和配置式mybatis有哪些优缺点?
注解
优点:
1、在class文件中,可以降低维护成本
2、提高开发效率。
缺点:
1、如果对annotation进行修改,需要重新编译整个工程。
2、业务类之间的关系不如XML配置那样一目了然。
3、程序中过多的annotation,对于代码的简洁度有一定影响。
4、annotation貌似功能没有xml配置文件齐全
xml配置文件
优点:
1、降低耦合,使容易扩展。
2、对象之间的关系一目了然。
3、xml配置文件比注解功能齐全。
缺点:
1、配置文件配置工作量相对注解要大
Mybatis的一对一查询
城市表city
省份表
项目结构
实体类,省略getter和setter
映射文件,第一种查询方式:嵌套结果映射
运行结果显示:null
原来是忘了传入参数
报错:### Cause: org.apache.ibatis.reflection.ReflectionException:
Could not set property 'int' of 'class com.ycc.model.Province' with value '1' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'int' in 'class com.ycc.model.Province'
原因:应该是我实体类声明的是long类型,这里用的是int类型的缘故
将其改为Long类型后,报错:
Cause: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
原因:Integer不可转换为Long类型,由图可以知道传入的参数是int类型的,需要传入的是Long类型
解决:
查询结果:
第二种方式:嵌套查询
运行成功。
Mybatis的一对多查询
首先是建立三张表:teacher,class,student
然后建立实体类,省略getter和setter
用一个List<Student>集合属性表示班级拥有的学生
public class Classes {
private int id;
private String name;
private Teacher teacher;
private List<Student> student;
public class Student {
private int id;
private String name;
public class Teacher {
private int id;
private String name;
配置文件
<mapper namespace="com.ycc.mapping.classMapper">
<!--第一种方式:嵌套结果 -->
<select id="getClass" parameterType="int" resultMap="ClassResultMap">
select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}
</select>
<resultMap id="ClassResultMap" type="com.ycc.model.Classes">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" javaType="com.ycc.model.Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
<!-- ofType指定students集合中的对象类型 -->
<collection property="student" ofType="com.ycc.model.Student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
</collection>
</resultMap>
</mapper>
测试类
@Test
public void testGetClasses3() {
SqlSession sqlSession = MybatisUtil.getSqlSession(true);
String statement ="com.ycc.mapping.classMapper.getClass";
Classes classes = sqlSession.selectOne(statement,1);
sqlSession.close();
System.out.println(classes);
}
运行结果:
Classes{id=1, name='class_a', teacher=Teacher{id=1, name='teacher1'}, student=[Student{id=1, name='student_A'}, Student{id=2, name='student_B'}, Student{id=3, name='student_C'}]}
遇到的困惑
感觉这种多表联查的配置文件,很容易出错,今天感觉排了一天的错。理解以后思路还是能看懂,但是就是配置比较复杂,以后会多加练习。
明日计划
学习spring整合mybatis
收获
了解了mybatis实现一对一和一对多的查询方式。
评论