public class User implements Serializable {private Long id;private String name;private String password;private Long age;private static final long serialVersionUID = 1L;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name == null ? null : name.trim();}public String getPassword() {return password;}public void setPassword(String password) {this.password = password == null ? null : password.trim();}public Long getAge() {return age;}public void setAge(Long age) {this.age = age;}@Overridepublic String toString() {StringBuilder sb = new StringBuilder();sb.append(getClass().getSimpleName());sb.append(" [");sb.append("Hash = ").append(hashCode());sb.append(", id=").append(id);sb.append(", name=").append(name);sb.append(", password=").append(password);sb.append(", age=").append(age);sb.append(", serialVersionUID=").append(serialVersionUID);sb.append("]");return sb.toString();} }
UserMapper.xml配置文件
ID, NAME, PASSWORD, AGEdelete from userwhere ID = #{id,jdbcType=BIGINT}SELECT LAST_INSERT_ID()insert into user (NAME, PASSWORD, AGE)values (#{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{age,jdbcType=BIGINT})SELECT LAST_INSERT_ID()insert into userNAME,PASSWORD,AGE,#{name,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR},#{age,jdbcType=BIGINT},update userNAME = #{name,jdbcType=VARCHAR},PASSWORD = #{password,jdbcType=VARCHAR},AGE = #{age,jdbcType=BIGINT},where ID = #{id,jdbcType=BIGINT}update userset NAME = #{name,jdbcType=VARCHAR},PASSWORD = #{password,jdbcType=VARCHAR},AGE = #{age,jdbcType=BIGINT}where ID = #{id,jdbcType=BIGINT}
UserService接口及实现类
public interface UserService {public List findAll();}@Service public class UserServiceImpl implements UserService {@AutowiredUserMapper mapper;public List findAll() {System.out.println("执行了DAO层");return mapper.selectAll();} }
UserController类
@Controller public class UserController {@AutowiredUserService userService;//查询@RequestMapping("/listUserCommon")public String listUser(Model model) {List users = userService.findAll();for (User user : users) {System.out.println(user.toString());}model.addAttribute("users", users);return "listUser";} }