主要目的
主要实现对数据库的增删改查
对应工具及环境
所用工具:idea,Navicat
数据库:MYSQL
jdk1.8
数据库
页面阅览
主要实现对应功能,暂不考虑页面布局
导入jar包
部分代码
各个包名
UserHander
package com.aynu.handler;
import com.aynu.beans.User;
import com.aynu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class UserHandler {
@Autowired
IUserService userService;
@RequestMapping("/register.do")
public String register(User user){
userService.InsertUser(user);
return "/welcome.jsp";
}
@RequestMapping("/delete.do")
public String delet(int id){
userService.DeleteUser(id);
return "/welcome.jsp";
}
@RequestMapping("/update.do")
public String update(User user){
userService.UpdateUser(user);
return "/welcome.jsp";
}
@RequestMapping("/find.do")
public ModelAndView find(){
List<User> list = userService.SelectAllUsers();
ModelAndView mv=new ModelAndView();
mv.addObject("list",list);
mv.setViewName("find.jsp");
return mv;
}
}
IUserServiceImpl
package com.aynu.service;
import com.aynu.beans.User;
import com.aynu.dao.IUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class IUserServiceImpl implements IUserService{
@Autowired
private IUserDao userDao;
public void InsertUser(User user) {
userDao.InsertUser(user);
}
public void DeleteUser(int id) {
userDao.DeleteUser(id);
}
public void UpdateUser(User user) {
userDao.UpdateUser(user);
}
public List<User>SelectAllUsers(){
List<User> list=userDao.SelectAllUsers();
return list;
}
}
IUserDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aynu.dao.IUserDao">
<insert id="InsertUser">
insert into user(name,password) value(#{name},#{password})
</insert>
<delete id="DeleteUser">
DELETE from user where id=#{id}
</delete>
<select id="SelectAllUsers" resultType="user">
select * from user
</select>
<update id="UpdateUser">
update user set name = #{name},password=#{password} where id=#{id}
</update>
</mapper>
注意
修改对应数据库名称和密码
注意配置tomcat(我所使用的版本是9.0.55)
有问题请留言,看到了就回
下载链接
链接: link.