ssm
model(entity,pojo)
view
controller
- 基本处理
@Controller
public class testController {
@RequestMapping("aaa")
public String index() {
System.out.println("aaaa");
return "index";
}
- @ResquestMapping
@Controller 多级目录
@RequestMapping("Book")
public class testController {
@RequestMapping("index")
public String index() {
- 方法参数
- 请求参数
public String index(String txt) public String index(Type t) 调用settters
- 系统参数
向页面模板传值(request 值) public String index(Type t,ModelMap m) m.put("info", new Type(2,"dddff")); public String index(HttpServletRequest req) public String index(HttpSession s)
- 请求参数
- 方法数据库处理
@Autowired 在容器中根据类型自动获取对象 Type_Dao dao; @RequestMapping("index") public @ResponseBody List<Type> index() { return dao.select(); }```
- 方法返回
- String 视图名称
return "edit";
- String url转发
return "redirect:index.jsp";
- 对象 转换成json
public @ResponseBody Type index() { return new Type(2,"dddff"); }
- String 视图名称
容器部件
@controller
@service
@respository
@component
mybatis
- 定义接口
@Repository public interface Type_Dao { @Select("select * from type") public List<Type> select(); @Insert("insert into type (name) values(#{name})") public void insert(Type t);
- 两个扫描
- 将接口动态生成类,加入数据连接(包名一致)
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
- 加入spring容器
<context:component-scan base-package="dao"></context:component-scan>
- 将接口动态生成类,加入数据连接(包名一致)
- 使用:
- 动态注入
@Autowired 在容器中查找类型一致的对象,赋值到下面变量 Type_Dao dao;
- 查询使用
@RequestMapping("index") public @ResponseBody List<Type> index() { return dao.select(); }
- 动态注入
总结
- 配置文件(包名正确)
- dao创建
- 创建接口,@respository
- 创建增删改查方法,并加入增删改查注解,写sql,#{属性}
- 创建service,接口,方法一般与dao一致
- serviceimpl创建,类引用service接口
加入@service
加入@Autowired 指定dao
实现每个方法的dao处理 - controller创建 @controller
加入@Autowired 指定service
创建方法,指定@RequestMapping(“index”),执行方法
过程
-
浏览器发出请求
-
启动spring容器,web.xml 请求地址能够被spring收到
-
controller必须在spring容器(依赖必须在容器里,service)
- controller-service-dao-连接注入(每个都必须在容器)
- 两个扫描(包扫描,MapperScannerConfigurer扫描)
-
requestmapping地址(多层地址,地址不能冲突)
-
方法参数冲突(int,interger必有项区别)
-
代码处理错误(null,index)-sql错误
-
返回的视图名称是否能找到(路径对应)
-
jsp视图处理正确
常见异常
- 404 mapping路径/视图名称/没进入spring处理
- 500 数据库异常 ###
- 500 控制器处理 根据异常信息
- 启动spring错误 依赖容器初始化错误
- web服务启动错误,jar包不全,web.xml设置有问题,删除server重置