@ruoli
2016-08-28T16:08:11.000000Z
字数 1225
阅读 1728
SpringMvc
现有两个实体Java类,Contract.java,Act.java,其中Act对象做为一个属性存在于Contract类中,代码如下:
public class Contract {
private String id;
private String name;
private Act act;
//getter,setter 省略
}
public class Act {
private String id;
private String name;
//getter,setter 省略
}
url 请求路径如下:
http://localhost/SpringMvc/index?id=1&act.id=2
Controller控制类方法如下:
@RequestMapping("index")
public @ResponseBody String hello(Contract contract){
System.out.println(contract.getId()+"--"+contract.getAct().getId());
//此处输出:1--2
return "index";
}
以下是Mapper类
@Mapper
public interface UserMapper {
@Select("SELECT 1 'id',2 'name' ,3 'age',4 'act.id',5 'act.name' FROM dual")
User findUser();
}
以下是User.java,Act.java
public class Contract {
private String id;
private String name;
private Act act;
//getter,setter 省略
}
public class Act {
private String id;
private String name;
//getter,setter 省略
}
以下是测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
@Rollback
public void findByName() throws Exception {
User u=userMapper.findUser();
System.out.println(u.getName()+"--"+u.getAct().getName());
//返回结果 : 2--4
}
}