通过注解方式把控制层类、业务层类、数据持久层类交给spring管理需要两个步骤
1、 加注解
2、 spring配置文件中扫描这些类所在的包
一、Spring标签的作用
1、web-app标签的作用是指定配置的文件地址、版本。
2、context-param配置加载类路径的配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext.xml</param-value>
</context-param>
param-value代表param-name实际的路径
3、listener标签作用是配置监听器
4、servlet作用是前端控制器(加载classpath:spring-mvc.xml 服务器启动创建servlet)
5、init-param配置初始化参数,创建完DispatcherServlet对象,加载配置文件
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
6、bean配置文件。property name命名,value是配置别名具体的变量。可以通过配置bean实现扫描特定的包,进行准确的操作,减少不必要的多余扫描。
其他的XML标签
7、dependcyManagement和dependencies的作用是管理项目的版本号其中依赖关系能实现以后用的项目都实现版本统一。
8、RequestMapping作用是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径
二、访问一个控制层方法的步骤
1、定位到主机ip的进程
2、定位到项目地址
3、定位到controller
4、定位到方法
三、具体应用
1、在Dao方法中设置一个接口。
2、在控制层中写一个具体的方法。
3、在UserMapper.xml中执行具体的SQL语句。
例子:
mapper中的插入语句
insert id="insertUsers">
insert into tb_user (id,name,sex)
values ('4','shabi','women')
</insert>
在Dao方法中设置插入方法的接口方法
public interface IUserDao {
List<User> getUsers();
void updateUsers();
void deleteUsers();
void insertUsers();
// User getUserById(Integer id);
}
在控制层中引用
@RequestMapping("/insert")
public String insert(Model model) {
userDao.insertUsers();
List<User> users=userDao.getUsers();
//键值对形式添加进model
model.addAttribute("users", users);
return "index";
}
最终可以输出
添加页面和更改页面和删除页面。