1用户添加

springboot整合jdbc-实现添加用户

  1. 创建实体类(pojo/Users)

    public class Users {
        private Integer id;
        private String username;
        private String usersex;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getUsersex() {
            return usersex;
        }
    
        public void setUsersex(String usersex) {
            this.usersex = usersex;
        }
    }
    
  2. 编写页面(resources/templates/addUser.html)

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <form th:action="@{/user/addUser}" method="post">
            <input type="text" name="username"><br>
            <input type="text" name="usersex"><br>
            <input type="submit" value="ok">
        </form>
    </body>
    </html>
    
  3. 创建PageController(controller/PageController)

    //页面跳转controller
    @Controller
    public class PageController {
        //页面跳转方法
        @RequestMapping("/{page}")
        public String showPage(@PathVariable String page){
            return page;
        }
    }
    
  4. 创建UsersController(controller/UsersController)

    @Controller
    @RequestMapping("/user")
    public class UsersController {
        @Autowired
        private UsersService usersService;
        @PostMapping("/addUser")
        public String showInfo(Users users){
            try{
                this.usersService.addUser(users);
            }catch (Exception e){
                e.printStackTrace();
                return "error";
            }
            return "redirect:/ok";
        }
    }
    
  5. 编写service接口和实现类

    (service/UsersService)

    public interface UsersService {
        void addUser(Users users);
    }
    

    (service/impl/UsersServiceImpl)

    //用户管理业务层
    @Service
    public class UsersServiceImpl implements UsersService {
        @Autowired
        private UsersDao usersDao;
        //添加用户
        @Override
        @Transactional
        public void addUser(Users users) {
            this.usersDao.insertUsers(users);
        }
    }
    
  6. 编写dao接口和实现类

    (dao/UsersDao)

    public interface UsersDao {
        void insertUsers(Users users);
    }
    

    (dao/impl/UsersDaoImpl)

    //持久层
    @Repository
    public class UsersDaoImpl implements UsersDao {
        @Autowired
        private JdbcTemplate jdbcTemplate;
        //添加用户
        @Override
        public void insertUsers(Users users) {
            String sql = "insert into users(username,usersex) value(?,?)";
            this.jdbcTemplate.update(sql,users.getUsername(),users.getUsersex());
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值