目录
1、在ssm配置版的基础上修改成注解版并完成CRUD
项目结构如下
配置版在如下链接
https://blog.csdn.net/weixin_48500300/article/details/118030142
2、修改applicationContext.xml的配置,添加如下配置
<!-- 开启SpringMVC注解驱动 -->
<mvc:annotation-driven />
<!--静态页面,如html,css,js,images可以访问-->
<mvc:default-servlet-handler />
<!-- 使用扫描机制扫描包 -->
<context:component-scan base-package="ltl"/>
完整配置如下
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:apo="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1、关联数据库配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!--2、配置数据源 连接池 dbcp:半自动化操作,不能自动连接
c3p0:自动化操作(自动化的加载配置文件,并且可以自动化设置到对象中)
druid-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 数据库连接池中的最大的数据库连接数 -->
<property name="maxPoolSize" value="30"/>
<!-- 数据库连接池中的最小的数据库连接数 -->
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--3、配置mybitas工厂,同时指定数据源,并与MyBatis完美整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 4、配置dao接口扫描包,动态实现dao接口可以注入到spring容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 要扫描的dao包-->
<property name="basePackage" value="ltl.mapper"/>
</bean>
<!-- 5、配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置jsp 显示ViewResolver视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 开启SpringMVC注解驱动 -->
<mvc:annotation-driven />
<!--静态页面,如html,css,js,images可以访问-->
<mvc:default-servlet-handler />
<!-- 使用扫描机制扫描包 -->
<context:component-scan base-package="ltl"/>
3、编写UserServiceImpl实现类添加注解
package ltl.service.impl;
import ltl.domain.User;
import ltl.mapper.UserMapper;
import ltl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
//service调dao层
@Autowired
private UserMapper userMapper;
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
@Override
public int deleteUserById(int id) {
return userMapper.deleteUserById(id);
}
@Override
public User queryUserById(int id) {
return userMapper.queryUserById(id);
}
@Override
public List<User> queryAllUser() {
return userMapper.queryAllUser();
}
@Override
public int updateUser(User user) {
return userMapper.updateUser(user);
}
}
4、编写控制类UserController添加注解
package ltl.controller;
import ltl.domain.User;
import ltl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
//进入用户信息页
@RequestMapping("/allUser")
public String allUser(Model model){
List<User> list = userService.queryAllUser();
model.addAttribute("list", list);
//跳转到allUser.jsp
return "allUser";
}
//添加
@RequestMapping("/addUser")
public String addUser(User user){
System.out.println("addUser=>" + user);
int i = userService.addUser(user);
//重定向到/user/allUser请求
return "redirect:/user/allUser";
}
//跳转到修改页面
@RequestMapping("/toUpdate")
public String toUpdate(int id, Model model) {
User user = userService.queryUserById(id);
model.addAttribute("user", user);
//跳转到updateUser.jsp
return "updateUser";
}
//修改用户信息
@RequestMapping("/updateUser")
public String updateUser(User user){
int i = userService.updateUser(user);
if (i > 0) {
System.out.println("修改用户信息成功" + user);
}
//重定向到/user/allUser请求
return "redirect:/user/allUser";
}
//删除用户
@RequestMapping("/deleteUser")
public String deleteUser(int id){
int i = userService.deleteUserById(id);
if(i>0){
System.out.println("删除用户成功");
}
//重定向到/user/allUser请求
return "redirect:/user/allUser";
}
}
5、编写jsp页面
1、index.jsp如下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<h3>
<a href="${pageContext.request.contextPath}/user/allUser">进入书籍页面</a>
</h3>
</body>
</html>
2、allUser.jsp如下
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户信息</title>
</head>
<body>
<table>
<thead>
<tr>
<th>用户编号</th>
<th>用户名称</th>
<th>用户密码</th>
<th>用户邮箱</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${list}">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.email}</td>
<td>
<a href="${pageContext.request.contextPath}/user/toUpdate?id=${user.id}">修改</a>
|
<a href="${pageContext.request.contextPath}/user/deleteUser?id=${user.id}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<form action="${pageContext.request.contextPath}/user/addUser" method="post">
<label>用户名</label>
<input type="text" name="username" required>
<br>
<label>密码</label>
<input type="text" name="password" required>
<br>
<label>邮箱</label>
<input type="text" name="email" required>
<br>
<input type="submit" value="添加">
</form>
</body>
</html>
3、updateUser.jsp如下
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改用户信息</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/updateUser" method="post">
<input type="hidden" name="id" value="${user.id}">
<label>用户名</label>
<input type="text" name="username" value="${user.username}" required>
<br>
<label>密码</label>
<input type="text" name="password" value="${user.password}" required>
<br>
<label>邮箱</label>
<input type="text" name="email" value="${user.email}" required>
<br>
<input type="submit" value="修改">
</form>
</body>
</html>
6、运行效果