UserConller.java
package com.itheima.controller;
import java.util.List;
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 org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.po.User;
import com.itheima.service.UserService;
@Controller
@RequestMapping("/user")
public class UserConller {
@Autowired
private UserService userService;
//通过学号查询
@RequestMapping("/queryUserByUid")
public String queryUserByUid(int sno,Model model) {
User user=userService.queryUserByUid(sno);
System.out.println(user);
model.addAttribute("user",user);
return "userinfo";
}
//查询所有信息
@RequestMapping("/getAllUser")
public String queryall(Model model,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "2")int pageSize) {
PageHelper.startPage(pageNum, pageSize);//开始分页
List<User> u=userService.queryall();//查询所有用户信息
PageInfo<User> p=new PageInfo<User>(u);//封装了分页后的信息
model.addAttribute("ulist",u);
model.addAttribute("pageinfo",p);
model.addAttribute("flag", 1);
return "userinfo";
}
//通过姓名查询
@RequestMapping("/queryByUname")
public String queryByUname(String sname,Model model,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "2")int pageSize) {
PageHelper.startPage(pageNum, pageSize);//开始分页
//PageHelper.startPage(1, 2);
List<User> u1=userService.queryByUname(sname);//查询所有用户信息
PageInfo<User> p1=new PageInfo<User>(u1);//封装了分页后的信息
model.addAttribute("ulist",u1);
model.addAttribute("pageinfo",p1);
model.addAttribute("sname", sname);
model.addAttribute("flag", 0);
return "userinfo";
}
//添加信息
@RequestMapping("/toAddUser")
public String toAddUser() {
return "addUser";
}
@RequestMapping("/addUser")
public String addUser(User user) {
int t=userService.addUser(user);
if(t>0) {
return "forward:getAllUser";
}else {
return "erro";
}
}
//删除用户
@RequestMapping("/delUser")
public String delUser(int sno) {
int t=userService.delUser(sno);
if(t>0) {
return "forward:getAllUser";
}else {
return "erro";
}
}
//修改用户信息
@RequestMapping("/update")
public String update(int sno,Model model) {
User user=userService.queryUserByUid(sno);
model.addAttribute("user",user);
return "updateUser";
}
@RequestMapping("/updateUser")
public String updateUser(User user) {
int t=userService.updateUser(user);
if(t>0) {
return "forward:getAllUser";
}else {
return "erro";
}
}
}
UserMapper.java
package com.itheima.mapper;
import java.util.List;
import com.itheima.po.User;
public interface UserMapper {
//通过用户id查找用户
public User queryUserByUid(int sno);
public List<User> queryall();
public List<User> queryByUname(String sname);
public int addUser(User user);
public int delUser(int sno);
public int updateUser(User user);
}
?
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.itheima.mapper.UserMapper">
<select id="queryUserByUid" parameterType="Integer" resultType="com.itheima.po.User">
select * from student where sno=#{_parameter}
</select>
<select id="queryall" resultType="com.itheima.po.User">
select * from student
</select>
<select id="queryByUname" parameterType="String" resultType="com.itheima.po.User">
select * from student where sname like concat('%',#{_parameter},'%')
</select>
<insert id="addUser" parameterType="com.itheima.po.User">
insert into student(sname,age) values(#{sname},#{age})
</insert>
<delete id="delUser">
delete from student where sno=#{_parameter}
</delete>
<update id="updateUser" parameterType="com.itheima.po.User">
update student set sname=#{sname},age=#{age} where sno=#{sno}
</update>
</mapper>
User.java
package com.itheima.po;
public class User {
private Integer sno;
private String sname;
private Integer age;
public User(Integer sno, String sname, Integer age) {
super();
this.sno = sno;
this.sname = sname;
this.age = age;
}
public User() {
super();
}
public Integer getSno() {
return sno;
}
public void setSno(Integer sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [sno=" + sno + ", sname=" + sname + ", age=" + age + "]";
}
}
UserService.java
package com.itheima.service;
import java.util.List;
import com.itheima.po.User;
public interface UserService {
public User queryUserByUid(int sno);
public List<User> queryall();
public List<User> queryByUname(String sname);
public int addUser(User user);
public int delUser(int sno);
public int updateUser(User user);
}
UserServiceImpl.java
package com.itheima.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itheima.mapper.UserMapper;
import com.itheima.po.User;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User queryUserByUid(int sno) {
return userMapper.queryUserByUid(sno);
}
@Override
public List<User> queryall() {
return userMapper.queryall();
}
@Override
public List<User> queryByUname(String sname) {
return userMapper.queryByUname(sname);
}
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
@Override
public int delUser(int sno) {
return userMapper.delUser(sno);
}
@Override
public int updateUser(User user) {
return userMapper.updateUser(user);
}
}
|