开始工作
先按照上图建立好相应的controller,mapper等文件。
接着进行一个配置
首先是application.properties
server.port=8080#启动端口
#加载Mybatis配置文件
mybatis.mapper-locations = classpath:mapper/*.xml
#数据源必填项
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/studentmanage?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
spring.datasource.username = root
spring.datasource.password = 123456
接着是spring启动主函数java文件
/*扫描mapper,防止找不到dao层文件,就写上*/
@MapperScan(basePackages = {"com.example.learn.Dao"})
@SpringBootApplication
public class LearnApplication {
public static void main(String[] args) {
SpringApplication.run(LearnApplication.class, args);
}
}
根据用户表,在entity建立实体类User.java,编译器都有相应的操作可以一键生成getter,setter,toString,只需定义好变量即可。
XM返佣https://www.fx61.com/brokerlist/xm.html
public class User {
private String username;
private String password;
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;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
接着是Dao层
import com.example.learn.Entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
@Mapper
@Component(value = "userDao")//这些都为配置注释
public interface UserDao {
//登录
public User login(@Param("username") String username,@Param("password") String password);
}
dao层写完,在写dao层配套的sql语句(mybatis功能)
mapper包里面建立.xml文件
select * from user_info where username=#{username} and password=#{password}
弄完这些接着到service层,包括impl接口实现
首先是service接口
import com.example.learn.Entity.User;
public interface UserService {
public User login(String username, String password);
}
接着是Impl下的实现类
import com.example.learn.Dao.UserDao;
import com.example.learn.Entity.User;
import com.example.learn.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("UserService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User login(String username, String password) {
return userDao.login(username,password);
}
}
最后就是Controller
import com.example.learn.Entity.User;
import com.example.learn.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/login")//可以换成@RequestMapping,action那里使用的命名
public String login(HttpServletRequest request, User u){
String username=request.getParameter("username");
String password=request.getParameter("password");
u=userService.login(username,password);
if(u!=null){
return "success";//到success的动态网页
}
return "redirect:/login.html";//这里是定向到login.html静态网页
}
}
测试工作
先在表中插入测试数据用户和密码
再建立两个html来测试功能
static/login.html
templates/success
成功
|
|