您的位置:首页 > 编程语言 > Java开发

spring 3.05 + spring 3.05 mvc + mybatis (注解驱动) maven整合应用示例

2011-09-20 15:37 375 查看

包结构如下:

 

 

 

贴下关键代码.


 dao 层:

 

public interface TestMapper {
void insertTestTable(@Param("testTable") TestTable testTable);
@Select("select * from TEST_TABLE")
List<TestTable> getList();
}

 也可以全部使用map 不存在po映射, 这样可以自己写一个这样的通用dao

public interface CommonMapper {

@Update("${sql}")
Integer update(@Param("sql") String sql);

@Select("${sql}")
List<Map> list(@Param("sql") String sql);

@Delete("${sql}")
Integer delete(@Param("sql") String sql);

@Select("${sql}")
Object selectOne(@Param("sql") String sql);

}

 业务层可以这样写:

 

public void pringList(){
List<Map> mapList = commonMapper.list("select * from TEST_TABLE");
for (Map map : mapList) {
System.out.println(map.get("name"));
}
}
@Override
public void insertTestTable(TestTable testTable) {
testMapper.insertTestTable(testTable);
}
@Override
public List<TestTable> getTableList() {
return testMapper.getList();
}

 mvc控制层:

 

@Controller
public class TestController {

@Autowired
private TestService testService;

@RequestMapping(value = "/test/insertTable", method = RequestMethod.GET)
public String insertTable(ModelMap modelMap) {
TestTable testTable = new TestTable();
// testTable.setId(1);
testTable.setName("dddd");
testService.insertTestTable(testTable);
modelMap.addAttribute("msg", "添加成功! 添加了用户:"+testTable.getName());
return "/success/msg.jsp";
}

@RequestMapping(value = "/test/list", method = RequestMethod.GET)
public String list(ModelMap modelMap) {
modelMap.addAttribute("testTableList", testService.getTableList());
return "/success/list.jsp";
}

}

 

上面简单的贴了主体代码.

 

详情还是看附件. 工程是maven构建的. 具体的jar包就不提供了

 

jdk是用1.6编译的.换成1.5的话.需要自己把类中得 override 删除掉

 

数据库连接用的是jndi . 在src / config 下

 

原始工程上传留作备用.

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: