实体类
private Long id; private Integer empNo; private Date birthDate; private String firstName; private String lastName; private String gender; private Date hireDate; private List<SalariesPo> salariesPos; private TitlesPo titlesPo; private DepartmentsPo departmentsPo;
mapper
/** * 根据条件分页查询 * @param qo * @param pageBounds * @return */ List<EmployeesPo> queryPage(@Param("qo") EmployeesQo qo,PageBounds pageBounds);
mapper配置文件
<resultMap id="EmployeesMap" type="com.linesum.model.po.EmployeesPo" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="emp_no" property="empNo" jdbcType="INTEGER" />
<result column="birth_date" property="birthDate" jdbcType="DATE" />
<result column="first_name" property="firstName" jdbcType="VARCHAR" />
<result column="last_name" property="lastName" jdbcType="VARCHAR" />
<result column="gender" property="gender" jdbcType="CHAR" />
<result column="hire_date" property="hireDate" jdbcType="DATE" />
<collection property="salariesPos" ofType="com.linesum.model.po.SalariesPo">
<id column="salary_id" property="id" jdbcType="BIGINT" />
<result column="emp_no" property="empNo" jdbcType="INTEGER" />
<result column="salary" property="salary" jdbcType="INTEGER" />
<result column="salary_from_date" property="fromDate" jdbcType="DATE" />
<result column="salary_to_date" property="toDate" jdbcType="DATE" />
</collection>
<association property="titlesPo" ofType="com.linesum.model.po.TitlesPo">
<id column="title_id" property="id" jdbcType="BIGINT" />
<result column="emp_no" property="empNo" jdbcType="INTEGER" />
<result column="title" property="title" jdbcType="VARCHAR" />
<result column="title_from_date" property="fromDate" jdbcType="DATE" />
<result column="title_to_date" property="toDate" jdbcType="DATE" />
</association>
<association property="departmentsPo" ofType="com.linesum.model.po.DepartmentsPo">
<id column="dept_id" property="id" jdbcType="BIGINT" />
<result column="dept_no" property="deptNo" jdbcType="CHAR" />
<result column="dept_name" property="deptName" jdbcType="VARCHAR" /
</association>
</resultMap>
<!--员工分页查询 -->
<select id="queryPage" resultMap="EmployeesMap" parameterType="com.linesum.model.qo.EmployeesQo" >
SELECT
a.id,a.emp_no,a.birth_date,a.first_name,a.last_name,a.gender,a.hire_date,
b.id AS title_id,b.from_date AS title_from_date,b.to_date AS title_to_date,
c.id AS salary_id,c.from_date AS salary_from_date,c.to_date AS salary_to_date,c.salary,
d.id AS dept_id,d.dept_name,d.dept_no
FROM
employees a,titles b,salaries c,departments d,dept_emp e
WHERE
a.emp_no = b.emp_no
AND
a.emp_no = c.emp_no
AND
e.dept_no = d.dept_no
<if test="qo.birthDate != null and birthDate != ''">
AND a.birth_date = #{qo.birthDate,jdbcType=DATE},
</if>
<if test="qo.gender != null and qo.gender != ''" >
AND a.gender = #{qo.gender,jdbcType=CHAR},
</if>
<if test="qo.hireDate != null and qo.hireDate != ''" >
AND a.hire_date = #{qo.hireDate,jdbcType=DATE},
</if>
<if test="qo.salaryStart != null and qo.salaryStart != ''" >
AND b.salary >= #{qo.salaryStart,jdbcType=INTEGER},
</if>
<if test="qo.salaryEnd != null and qo.salaryEnd != ''" >
AND b.salary <= #{qo.salaryEnd,jdbcType=INTEGER},
</if>
</select>
报错
[com.alibaba.druid.pool.DruidDataSource]{dataSource-1} closed
[org.springframework.test.context.TestContextManager]Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@2a389bdf] to prepare test instance [userweb.TestEmployeeService@6a816320]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:103)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:73)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
加载上下文失败,你的配置文件是不是配置错了
1.从错误信息来看,你这是用springtest运行测试的吧。 spring上下下文加载失败了。检查你的spring配置文件是否有误。
如果有多个配置文件需要加载,可以在ContextConfiguration 里边逗号分隔。类似:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:config/applicationContext.xml","classpath:spring.xml"})
@WebAppConfiguration(value = "test-web/WebRoot")
2. 你的分页查询里边有 a.id ,b.id 这种得取别名吧,好久没写mybatis了,都忘记了。