实训感受
1、今天我能够跟上老师了,在上午的课堂上就解决了所以的问题。在昨天的基础上,今天又实现了对用户的增、删、改、查,但是代码量并不大,也让我很直观的体会到了使用SSM框架的优点。
2、通过老师对配置代码的讲解,我对SSM框架的理解又加深了一些,从昨天的看到配置文件的代码完全懵,到现在大概知道每一块是做什么用的。
3、今天根据项目内容,对小组成员进行了分工。我们要实现的是简单版的线上购物网页,实现一些基本的增、删、改、查功能,好像想想不是很难,只是前端页面的编写可能问题比较大,看是否可以参考其他页面改一改。
4、今天在同学手把手教学下,我终于会使用git进行一些操作了,并且编写了一篇博客。我们小组打算使用gitee来托管代码,感觉功能比较强大,还很方便,但今天接触到的git还比较浅,希望可以通过本次实训锻炼我能够灵活使用git。
总的来说,今天完成课堂任务比较顺利,明天开始要逐步推进小组项目的开展了。
实训流程
1、讲解配置文件
2、导入登录、主界面(显示用户)、新增用户、更新用户信息的页面文件。
user-add.jsp:新增用户
user-list.jsp:主页面,用来显示用户信息
user-update.jsp:更新用户信息
login.jsp:登录
3、依次完成显示用户、删除、新增、更新的后台代码。
4、运行、测试
5、小组任务:项目内容确定、任务分工。
课堂任务编码实现
bean层不变
controller层
package com.zr.controller;
import com.zr.bean.User;
import com.zr.dao.IUserDao;
import com.zr.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
@RequestMapping("user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("login.do")
public ModelAndView login(String username,String password){
boolean flag=userService.login(username,password);
ModelAndView modelAndView=new ModelAndView();
if(flag){
modelAndView.setViewName("main");
}
else {
modelAndView.setViewName("../failer");
}
return modelAndView;
}
@RequestMapping("findAll.do")
public ModelAndView findAll(){
List<User> users = userService.findAll();
ModelAndView mv=new ModelAndView();
mv.setViewName("user-list");
mv.addObject("users",users);
return mv;
}
@RequestMapping("deleteById.do")
public String delete(int id){
userService.deleteById(id);
return "redirect:findAll.do"; //重定向
}
@RequestMapping("add.do")
public String add(User user){
userService.add(user);
return "redirect:findAll.do";
}
@RequestMapping("toUpdate.do")
public ModelAndView toUpdate(int id){
User user=userService.selectUserById(id);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.setViewName("user-update");
return modelAndView;
}
@RequestMapping("update.do")
public String update(User user){
userService.update(user);
return "redirect:findAll.do";
}
}
dao层
package com.zr.dao;
import com.zr.bean.User;
import java.util.List;
public interface IUserDao {
User findUserByUsername(String username);
List<User> findAll();
void deleteById(int id);
void add(User user);
User selectUserById(int id);
void update(User user);
}
service层
UserServiceImp.class
package com.zr.service.impl;
import com.zr.bean.User;
import com.zr.dao.IUserDao;
import com.zr.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public boolean login(String username, String password) {
User user=userDao.findUserByUsername(username);
if(user!=null&&user.getPassword().equals(password)){
return true;
}
else {
return false;
}
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public User selectUserById(int id) {
return userDao.selectUserById(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
}
IUserService.class
package com.zr.service;
import com.zr.bean.User;
import java.util.List;
public interface IUserService {
boolean login(String username,String password);
List<User> findAll();
void deleteById(int id);
void add(User user);
User selectUserById(int id);
void update(User user);
}
mapper层
<?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.zr.dao.IUserDao">
<select id="findUserByUsername" parameterType="String" resultType="com.zr.bean.User">
select * from tb_user where username=#{username}
</select>
<select id="findAll" resultType="user">
select * from tb_user
</select>
<delete id="deleteById" parameterType="int">
delete from tb_user where id=#{id}
</delete>
<insert id="add" parameterType="user">
insert into tb_user(username,password) values (#{username},#{password})
</insert>
<select id="selectUserById" parameterType="int" resultType="user">
select * from tb_user where id=#{id}
</select>
<update id="update" parameterType="user" >
update tb_user set username=#{username},password=#{password} where id =#{id}
</update>
</mapper>
运行效果
登录页面
主页面
显示所有用户
新增用户
点击保存后,可以发现用户新增了一个:
更新:
点击更新,可以更新用户名:
更新成功:
删除
小组项目进程汇报
1、确认需要实现的功能
思维导图如下:
2、任务分工
由于本次实现的功能比较简单,我们安排的是两-三人一组,实现负责的页面和后端代码。
3、搜集类似的源代码,参考前端页面的编写。
4、讨论数据库的建立。
5、学习使用gitee,并建立仓库。