springboot —+—springmvc -
mybatis 曾三改查
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
项目架构 springboot +mybatis—+springmvc
pom.xml
org.springframework.boot spring-boot-starter-parent 1.5.7.RELEASE sping-boot my-springboot-ltv 0.0.1-SNAPSHOTcontroller 层
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/{page}")
public String showPage(@PathVariable String page){
return page;
}
@RequestMapping("/addUser")
public String addUser(User user){
userService.addUser(user);
return "success";
}
@RequestMapping("/login")
public String findAll(Model model){
List<User> list =userService.findAll();
model.addAttribute("list", list);
return "index";
}
/* @RequestMapping("/login")
public String findAll(Model model){
List list =userService.findAll();
model.addAttribute(“list”, list);
return “login”;
}
*/
/**
* 根据id查询用户
*/
@RequestMapping("/findUserById")
public String findUserById(Model model,Integer id){
User user=userService.selectUserById(id);
model.addAttribute("user", user);
return "updateUser";
}
/**
*
* 更新用户
*/
@RequestMapping("/editUser")
public String editUser(User user){
System.out.println(user.getName());
System.out.println(user.getAge());
System.out.println(user.getId());
userService.updateUser(user);
return "success";
}
@RequestMapping("/delUser")
public String delUser(Integer id){
System.out.println("+ahjnakjn+");
userService.deleteUserById(id);
return "redirect:/user/login";
}
service层
import java.util.List;
import com.jzkj.pojo.User;
public interface UserService {
void addUser(User user);
List<User> findAll();
User selectUserById(Integer id);
void updateUser(User user);
void deleteUserById(Integer id);
}
实现类
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jzkj.mapper.UserMapper;
import com.jzkj.pojo.User;
import com.jzkj.service.UserService;
@Service
@Transactional
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public void addUser(User user) {
// TODO Auto-generated method stub
userMapper.addUser(user);
}
@Override
public List<User> findAll() {
// TODO Auto-generated method stub
return userMapper.findAll();
}
public User selectUserById(Integer id){
return userMapper.selectUserById(id);
}
@Override
public void updateUser(User user) {
// TODO Auto-generated method stub
userMapper.updateUser(user);
}
@Override
public void deleteUserById(Integer id) {
// TODO Auto-generated method stub
userMapper.deleteUserById(id);
}
}
mapper层
import java.util.List;
import com.jzkj.pojo.User;
public interface UserMapper {
void addUser(User user);
List<User> findAll();
User selectUserById(Integer id);
void updateUser(User user);
void deleteUserById(Integer id);
}
mapper.xml sql
PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN”
“http://mybatis.org/dtd/mybatis-3-mapper.dtd”>
update user set name=#{name},age=#{age} where id=#{id}
delete from user where id=#{id}
添加页面
用户年龄:
登入页面
<td>操作</td>
</tr>
<tr th:each="user : ${list}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td>
<a th:href="@{/user/findUserById(id=${user.id})}">更新用户</a>
<a th:href="@{/user/delUser(id=${user.id})}">删除用户</a>
</td>
</tr>
用户ID | 用户姓名 | 用户年龄 |
跳转成功页面
---------------------------la更新工作--------------
修改回显页面 共用一个借口
用户年龄:
数据源
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssmh
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.jzkj.pojo
spring.thymeleaf.cache=false
#spring.mvc.view.prefix=/WEB-INF/jsp/
#spring.mvc.view.suffix=.jsp