上次我们讲了如何利用IEDA+Maven整合SSM框架,实现简单的增删改查功能。这次我们再将项目工程结构翻新一下,这次的项目工程结构如下
首先new一个project,选择Maven,直接Next
Groupld与artfactld中的内容需要自己填上,自己随便起名,然后Next
点击Finish,项目便创建好了
在ssm_parent目录下新建四个Module,分别命名为ssm_bean,ssm_dao,ssm_service,ssm_web
在新建ssm_bean,ssm_dao,ssm_service时,Create from archetype不需要点勾,直接Next点到底,最后Finish
而在新建ssm_web时,Create from archetype要点勾,并且要选webapp
注意下面这些地方需要配置
然后点击加号,新添下面这个,点击OK,然后就完成了
注意每个文件夹下的java文件夹要要设为Sources Root
resources文件夹设为Resources Root
接下来创建类与接口,具体如下
在ssm_bean\src\main\java\com\zhongruan\bean目录下新建UserInfo类
package com.zhongruan.bean;
public class UserInfo {
private int id;
private String username;
private String password;
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserInfo() {
}
public UserInfo(String username, String password) {
this.username = username;
this.password = password;
}
}
在ssm_dao\src\main\java\com\zhongruan\dao目录下新建IUserDao接口
package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserDao {
public List<UserInfo> findAll();
public UserInfo doLogin(UserInfo userInfo);
boolean addUserInfo(UserInfo userInfo);
boolean updateUserInfo(UserInfo userInfo);
boolean deleteUserInfoById(int id);
}
在ssm_dao\src\main\resources\mapper目录下新建usermapper.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.zhongruan.dao.IUserDao" >
<select id="findAll" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo
</select>
<select id="doLogin" parameterType="com.zhongruan.bean.UserInfo" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo where username=#{username} and password=#{password}
</select>
<insert id="addUserInfo" parameterType="Userinfo">
insert into userinfo(username,password) VALUE (#{username},#{password})
</insert>
<update id="updateUserInfo" parameterType="UserInfo">
update userinfo set username=#{username},password=#{password} where id=#{id}
</update>
<delete id="deleteUserInfoById" parameterType="int">
DELETE FROM userinfo WHERE id=#{id}
</delete>
</mapper>
在ssm_service\src\main\java\com\zhongruan\service目录下新建IUserService接口
package com.zhongruan.service;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserService {
public List<UserInfo> findAll();
public boolean doLogin(UserInfo userInfo);
public boolean addUserInfo(UserInfo userInfo);
public boolean updateUserInfo(UserInfo userInfo);
public boolean deleteUserInfoById(int id);
}
在ssm_service\src\main\java\com\zhongruan\service\impl目录下新建UserService类
package com.zhongruan.service.impl;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<UserInfo> findAll()
{
return userDao.findAll();
}
@Override
public boolean doLogin(UserInfo userInfo)
{
UserInfo userInfo1=userDao.doLogin(userInfo);
if(null!=userInfo1)
{
return true;
}
else
{
return false;
}
}
@Override
public boolean addUserInfo(UserInfo userInfo) {
return userDao.addUserInfo(userInfo);
}
@Override
public boolean updateUserInfo(UserInfo userInfo) {
return userDao.updateUserInfo(userInfo);
}
@Override
public boolean deleteUserInfoById(int id) {
return userDao.deleteUserInfoById(id);
}
}
在ssm_web\src\main\java\com\zhongruan\controller目录下新建UserController类
package com.zhongruan.controller;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("findAll.do")
public ModelAndView findAll()
{
List<UserInfo> infos=userService.findAll();
ModelAndView mv=new ModelAndView();
mv.addObject("infos",infos);
mv.setViewName("user-list");
return mv;
}
@RequestMapping("login.do")
public ModelAndView doLogin(UserInfo userInfo)
{
boolean flag = userService.doLogin(userInfo);
ModelAndView mv=new ModelAndView();
if(flag)
{
System.out.println("1111111");
mv.setViewName("main");
}
else
{
mv.setViewName("../failer");
}
return mv;
}
@RequestMapping("save.do")
public String save (UserInfo userInfo)
{
userService.addUserInfo(userInfo);
return "redirect:/user/findAll.do";
}
@RequestMapping("toUpdate.do")
public String toUpdate()//可能有点问题,public String toUpdate(Model model, @RequestParam int id)
{
return "user-update";
}
@RequestMapping("update.do")
public String update(UserInfo userInfo){
userService.updateUserInfo(userInfo); //执行操作
return "redirect:/user/findAll.do"; //重定向页面
}
@RequestMapping("delete.do") //响应前端的delete.do事件
public String delete(@RequestParam int id){ //Get方法传递参数。获取传递的id
userService.deleteUserInfoById(id); //执行删除用户操作
return "redirect:/user/findAll.do"; //重定向
}
}
最后的页面结果展示
输入用户名和密码,点击登录
进入如下页面
点击左侧栏的系统管理,看到可以选择用户管理和角色管理
点击用户管理,可以查看全部用户
点击新建,即添加新用户
输入id,用户名称,密码,点击保存
在用户表添加成功
点击更新按钮
输入新的用户名与密码,点击保存
用户信息更新成功