整合程序:https://www.isdxh.com/68.html
一、增——增加用戶
1.創建物體類
package com.dxh.pojo;
public class Users {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
2.創建mapper介面以及映射組態檔
package com.dxh.mapper;
import com.dxh.pojo.Users;
public interface UsersMapper {
void insertUser(Users users);
}
<?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.dxh.mapper.UsersMapper">
<!-- 在properties檔案中配置過別名了,所以parameterType不需要寫Users的包的名稱了 -->
<insert id="insertUser" parameterType="Users">
insert into users(name,age) values (#{name},#{age})
</insert>
</mapper>
3.創建業務層
package com.dxh.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dxh.mapper.UsersMapper;
import com.dxh.pojo.Users;
import com.dxh.service.UsersService;
@Service
@Transactional
public class UserServiceImpl implements UsersService{
@Autowired
private UsersMapper usersMapper;
@Override
public void addUser(Users users) {
this.usersMapper.insertUser(users);
}
}
package com.dxh.service;
import com.dxh.pojo.Users;
public interface UsersService {
void addUser(Users users);
}
4.創建Controller
package com.dxh.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dxh.pojo.Users;
import com.dxh.service.UsersService;
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
/**
* 頁面跳轉的方法
*/
@RequestMapping("/{page}")
public String showPage(@PathVariable String page) {
return page;
}
/**
* 添加用戶
*/
@RequestMapping("/addUser")
public String addUser(Users users) {
this.usersService.addUser(users);
return "ok";
}
}
5.撰寫頁面:src/main/resources/templates/ input.html 和 ok.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AddUser</title>
</head>
<body>
<form th:action="@{/users/addUser}" method="post">
userName: <input type="text" name="name"/></br>
userAge: <input type="text" name="age"/></br>
<input type="submit" value=https://www.cnblogs.com/isdxh/p/"SUBMIT">
