ssm小小小练手实验(在b站springmvc)
0.引入jar包:mysql,mybatis,spring,dbcp,mybats-spring
1.整理dao
spring管理mapper接口,用mapper扫描器
配置mybatis.xml
配置applicationcontext-dao.xml(datasourse,sqlsessionfactory,并将它们注入mapper中,能产生sqlsession执行mapper.xml的sql语句)
2.整理service
让mapper注入到service,调用mapper进行各种增删改查操作
spring管理service接口
写service的实现类serviceimpl
配置applicationcontext-service.xml
3.整理springmvc
不需整合,本来就是spring的一部分
配置springmvc.xml
4.配置applicationcontext-transaction
让spring管理事务
5.在web.xml中启动spring容器并配置前端控制器DispatcherServlet
这是启动spring
6.编写controller
将service注入到controller中,可以调用service进行增删改查
例子:
修改商品
1.开发mapper
(1)根据id查询商品信息
(2)根据id更新表格数据
2.开发service
(1)根据id查询商品信息
(2)修改商品信息
3.开发controller
(1)商品修改页面显示
(2)商品修改信息提交
controller和jsp的数据交互
从controller传数据到jsp
在controller中有往jsp中设置一个叫itemList
名字的数据模型
jsp中这么接收到参数(EL表达式)注意${pageContext.request.contextPath}是指工程名
从jsp到controller
在jsp页面中给controller传递id的值
在controller中以这种方式接收参数(就像jsp向servlet传递参数一样)
或者用参数绑定(就是从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上 )