这里我们用ssm框架整合来实现一个增删改查的功能
第一步:先就是导入相关的架包(导入myabtis相关包,spring相关包, spring mvc相关包)
第二步:使用逆向工程生产mapper Mapper.xml 实体类(前提是弄一个数据库、关于逆向工程怎么使用我们在前面的博客已经说明了所以在这里就不详细写了)
第三步:编写beans-datasource.xml将mybatis交给spring 的bean工厂管理
代码如下(相关的路径自己记得改哈)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1、加载数据库的配置信息 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--2、datasource数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}"/>
<property name="username" value="${userName}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 3、sqlSessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 别名 -->
<property name="typeAliasesPackage" value="com.cc.entity"></property>
<!-- mapper XML映射 -->
<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"></property>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4、扫描mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cc.mapper"></property>
</bean>
</beans>
第4 步:编写UserService接口和UserServiceImpl类的list方法,查询所有用户的所有信息
实现类
@Service
public class UsersServiceImpl implements UsersService{
@Autowired
private UsersMapper usersMapper;
@Override
public List<Users> list() {
return usersMapper.selectByExample(null);
}
@Override
public Users selectById(int uid) {
return usersMapper.selectByPrimaryKey(uid);
}
@Override
public int update(Users user) {
return usersMapper.updateByPrimaryKey(user);
}
}
接口
public interface UsersService {
获取所有用户的所有信息
List<Users> list();
根据id查询用户信息
Users selectById(int uid);
根据id来修改用户信息
int update(Users user);
}
在完成第四步后可以先弄一个单元测试测一下
第5步:编写UserController类,编写list方法
UserController-------UserService-------UserMapper-----数据库
@Controller
@RequestMapping("/user")
public class UsersController {
@Autowired
private UsersService userService;
/**
* @Title: list
* @Description: 查询所有用户的所有信息
* @param @return
* @return ModelAndView
* @throws
*/
@RequestMapping("/list")
public ModelAndView list() {
ModelAndView mv= new ModelAndView();
//1.调用用户的service层的方法获取所有用户所有数据
List<Users> userses = userService.list();
//2.将数据绑定mv上
mv.addObject("userses",userses);
//3.设置视图
mv.setViewName("user/list");
//4.放回mv
return mv;
}
/**
* @Title: toUpdate
* @Description: 去修改页面
* @param @param uid
* @param @return
* @return ModelAndView
* @throws
注意:1.默认情况下这个方法的参数名必须和请求的url请求参数名一致
*/
@RequestMapping("/to_edit")
public ModelAndView toUpdate(int uid) {
System.out.println("====进入toUpdate===uid=>"+uid);
ModelAndView mv = new ModelAndView();
//1.查询该用户的所有信息 web--servcice-maper-数据
Users users = userService.selectById(uid);
//2.将该用户数据绑定到mv
mv.addObject("user", users);
//3.将修改页面的jsp名称绑定到mv中
mv.setViewName("user/edit");
//4.返回mv
return mv;
}
/**
* @Title: update
* @Description: 更新用户信息的方法
* @param @param user
* @param @return
* @return ModelAndView
* @throws
注意:edit.jsp表单元素的name值必须与update的参数实体类
的对应属性名一致!!
*/
@RequestMapping("/update")
public ModelAndView update(Users user) {
System.out.println("====进入update===user="+user);
ModelAndView mv=new ModelAndView();
//1.调用service更新用户信息
int result = userService.update(user);
//2.绑定视图
if(result>0) {//更新成功
//重定向 ,user/list的映射的list()方法,再由list()跳转到user/list.jsp
mv.setViewName("redirect:../user/list");
}else {//更新失败
}
//3.返回mv
return mv;
}
}
第6 步:在web.xml中配置spring容器交给tomcat启动
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>nd-spring05-base</display-name>
<!--一、spring的ioc容器配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 三、spring的编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 二、中央处理器(DispatcherServlet):映射器、适配器、视图解析器 -->
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
在beans.xml导入beans-datasource.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 1、注解扫描 -->
<!-- 2、datasource:mybatis -->
<import resource="beans-datasource.xml"/>
</beans>
第7步:spring-web.xml中扫描com.cc下面的包及子包
<context:component-scan base-package=“com.cc.**” />
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 开启注解扫描 .** 所有所有包以及子包-->
<context:component-scan base-package="com.cc.**" />
<!-- 1、处理器映射器:HandlerMapping url==>handler -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
<!-- 2.适配器: HandlerAdatper 调用 Handler==>Controller -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->
<!--为了使用@DateTimeFormat 使用注解驱动来加载适配器和映射器 -->
<mvc:annotation-driven/>
<!-- 3、视图解析器: -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 解析jstl标签 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- 动态页面的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 动态页面的后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
第8步:编写jsp页面WEB-INF/jsp/user/list.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="80%" border="1">
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>生日</th>
<th>地址</th>
<th>操作</th>
</tr>
<c:forEach items="${userses}" var="user">
<tr>
<th>${user.id }</th>
<th>${user.username}</th>
<th>${user.sex}</th>
<th>${user.birthday }</th>
<th>${user.address}</th>
<th>
<a href="${pageContext.request. contextPath}/user/to_edit?uid=${user.id}">修改</a>
</th>
</tr>
</c:forEach>
</table>
</body>
</html>
最后去启动tomcat访问页面