SpringBoot 整合SSM(四):编写后台文件

写后台文件,这里是做一个登录的功能,依旧是一个功能涉及到三个框架。


1.bean

User.java

package com.ssm_login.bean;

// 登录的实体类
public class User {
	private Integer id;
	private String username;
	private String password;

	public User() {
		super();
		// TODO 自动生成的构造函数存根
	}

	public User(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}

	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 getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}

}

2.dao

UserDao.java

package com.ssm_login.dao;


import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.ssm_login.bean.User;

//登录的Dao
@Mapper
public interface UserDao {
	User IsUser(User user);
	String findRole(int id);
	int addUser(User user);
	int addRole(@Param("id")int id, @Param("role")String role);
	int getUserId(User user);
	int getUserIdByName(String name);
}

这里注意要有@Mapper,表示要扫描我

3.service

接口:

package com.ssm_login.service;

import com.ssm_login.bean.User;

// 登录的操作类
public interface LoginService {
	boolean Login(User user);
	boolean Register(User user, String role) throws Exception;
	int getUserId(User user);
	String Role(int i);
}

实现类:

package com.ssm_login.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ssm_login.bean.User;
import com.ssm_login.dao.UserDao;

// 实现
@Service
public class LoginServiceImp implements LoginService {
	@Autowired
	private UserDao userDao;
	
	@Override
	public boolean Login(User user) {
		// TODO 自动生成的方法存根
		if(userDao.IsUser(user) != null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	@Override
	public String Role(int i) {
		// TODO 自动生成的方法存根
		return userDao.findRole(i);
	}

	@Override
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public boolean Register(User user, String role) throws Exception {
		// TODO 自动生成的方法存根
		if(userDao.getUserIdByName(user.getUsername()) > 0)
		{
			// 已存在
			return false;
		}
		if(userDao.addUser(user) == 1)
		{
			int i =userDao.addRole(user.getId(), role);
			System.out.println("addRole:"+i);
			if(i == 1)
			{
				return true;
			}
			else
			{
				throw new Exception();
			}
		}
		else
		{
			return false;
		}
	}
	
	@Override
	public int getUserId(User user)
	{
		return userDao.getUserId(user);
	}

}






可以看到这里面已经有事务了。。。事务也没变。

4.controller

LoginController.java

package com.ssm_login.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ssm_login.bean.User;
import com.ssm_login.exception.NoLoggingException;
import com.ssm_login.service.LoginServiceImp;

// 2018年5月16日10:29:13 登录类
@Controller
public class LoginController 
{
	@Autowired
	private LoginServiceImp ls;
	
	@RequestMapping("/")
	public String index(Model m)
	{
		m.addAttribute("msg", "欢迎登录!");
		return "/index.html";
	}
	
	@RequestMapping("/login")
	public String login(User user, Model m, HttpSession session, RedirectAttributes ra, HttpServletRequest request)
	{
		System.out.println(ls.Login(user));
		if(ls.Login(user))
		{
			ra.addFlashAttribute("msg", "欢迎!");
			session.setAttribute("user", user.getUsername());
			session.setAttribute("role", ls.Role(ls.getUserId(user)));
			return "redirect:/welcome";
		}
		else
		{
			m.addAttribute("msg", "欢迎登录![账号密码错误,登录失败]");
			m.addAttribute("username", user.getUsername());
			m.addAttribute("password", user.getPassword());
			return "/index.html";
		}
	}
	
	@RequestMapping("/register")
	public String register(User user, String role, Model m, HttpSession session, RedirectAttributes ra, HttpServletRequest request) throws Exception
	{
		System.out.println(role);
		if(ls.Register(user, role))
		{
			session.setAttribute("user", user.getUsername());
			session.setAttribute("role", ls.Role(ls.getUserId(user)));
			ra.addFlashAttribute("msg", "欢迎!");
			return "redirect:/welcome";
		}
		else
		{
			m.addAttribute("msg", "欢迎登录![注册失败!]");
			m.addAttribute("username", user.getUsername());
			m.addAttribute("password", user.getPassword());
			return "/index.html";
		}
	}
	
	@RequestMapping("/welcome")
	public String welcome(Model m ,@ModelAttribute("msg")String message)
	{
		m.addAttribute("msg", message);
		return "/welcome.html";
	}
	@RequestMapping("/selectAllStudent")
	public String selectAllStudent()
	{
		// 
		return "/student_list.html";
	}
	
	@ExceptionHandler(value=Exception.class)
	public String err(Model m)
	{
		m.addAttribute("msg", "写入权限失败!");
		return "/err.html";
	}
	
	@ExceptionHandler(NoLoggingException.class)
	public String ex(Exception ex, Model m)
	{
		System.out.println(ex.getMessage());
		m.addAttribute("msg", ex.getMessage());
		return "/err.html";
	}
}

这里可以看到异常捕获也是有了

5.Mapper

UserDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.ssm_login.dao.UserDao">
 	<select id="IsUser" parameterType="User" resultType="User">
 		select * from SystemInfo_user where username=#{username} and password=#{password}
 	</select>
	<select id="findRole" resultType="String">
		select role from SystemInfo_role where id=#{id}
	</select>
	<insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
		insert into SystemInfo_user(username, password) values(#{username}, #{password})
	</insert>
	<insert id="addRole">
		insert into SystemInfo_role(id, role) values(#{id}, #{role})
	</insert>
	<select id="getUserId" resultType="int" parameterType="User">
		select id from SystemInfo_user where username=#{username} and password=#{password}
	</select>
 </mapper>

这里有一个坑。

Mapper的配置文件要放在resources下面,必须,包括静态资源,都要在这个大目录下,因为默认只有有限几个文件夹是默认配置了的,其他没有,具体可以在application.properties的


可以参阅。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值