基于上篇springboot框架搭建的基础上,我们来做一个注册的小例子
一:添加依赖
在spring boot下我们用idea内置的tomcat,该tomcat不支持jsp文件需要添加依赖
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.31</version>
</dependency>
二:java代码
1.实体类domain
java-com.example.springbootexam1.domain
package com.example.springbootexam1.domain;
import java.io.Serializable;
public class User implements Serializable {
private Integer userId;
private String userName;
private String password;
private String role;
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", role='" + role + '\'' +
'}';
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
2.dao层,采用面向接口编程
java-com.example.springbootexam1.dao
package com.example.springbootexam1.dao;
import com.example.springbootexam1.domain.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserRegister {
int insert(User user);
}
3.写接口映射文件
resource-mapper-UserRegister.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.example.springbootexam1.dao.UserRegister">
<insert id="insert" parameterType="user">
insert into user values(null,#{userName},#{password},#{role})
</insert>
</mapper>
4.service层
java-com.example.springbootexam1.service
package com.example.springbootexam1.service;
import com.example.springbootexam1.dao.UserRegister;
import com.example.springbootexam1.domain.User;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserRegisterService {
@Resource
UserRegister userRegister;
public int registerService(User user){
return userRegister.insert(user);
}
}
5.controller层
java-com.example.springbootexam1.controller
package com.example.springbootexam1.controller;
import com.example.springbootexam1.domain.User;
import com.example.springbootexam1.service.UserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserRegisterController {
@Autowired
UserRegisterService registerService;
@RequestMapping("/register")
public String register(User user){
if(registerService.registerService(user)>0){
return "success";
}else {
return "false";
}
}
}
三:web模块
src-main-webapp
register.jsp
<%--
Created by IntelliJ IDEA.
User: user
Date: 2020/4/12
Time: 10:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/register" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input name="userName"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>角色:</td>
<td><select name="role">
<option value="doctor">doctor</option>
<option value="nurse">nurse</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="register"></td>
</tr>
</table>
</form>
</body>
</html>
四:总配文件
application-properties
#配置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/hospital
spring.datasource.username=root
spring.datasource.password=itcast
#配置包别名
mybatis.type-aliases-package=com.example.springbootexam1.domain
#配置映射文件位置,配置完成就不需要映射文件和接口的文件名一致
mybatis.mapper-locations=classpath:/mapper/*.xml
五:测试