首页 新闻 搜索 专区 学院

Spring、MyBatis的问题(改MyBatis配置文件的问题)

0
[已关闭问题] 关闭于 2020-07-14 10:24

GitHub上的详细代码
登录时数据库返回是空,看不出来是mapper的问题还是其它的

package com.oracle.curd.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.oracle.curd.bean.User;
import com.oracle.curd.service.UserService;

@Controller
public class UserController {
	// User
	@Autowired
	private UserService userService;

	/**
	 * UserService实例
	 */
	// 用户登录操作
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(String usercode, String password, Model model, HttpSession session, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String verifycode = request.getParameter("verifycode");
		session = request.getSession();
		String CHECKCODE_SERVER = (String) session.getAttribute("CHECKCODE_SERVER");
		session.removeAttribute("CHECKCODE_SERVER"); // 一次性的验证码使用
		if (!CHECKCODE_SERVER.equalsIgnoreCase(verifycode)) {
			model.addAttribute("msg", "验证码输入错误");
			return "login";
		}
		System.out.println(usercode + password);
		// 调用Dao层方法
		User user = userService.findUser(usercode, password);
		if (user != null) {
			// 从dao层那边查询的数据加入Session
			session.setAttribute("USER_SESSION", user);
			// 跳转到主页面
			// return "customer";
			return "redirect:customer";
		}
		model.addAttribute("msg", "密码或账号输入错误");
		// 错误返回到登录页面
		return "login";
	}

	// 用户退出操作
	@RequestMapping(value = "enduser")
	public String enduser(HttpSession session) {
		// 清除Session
		session.invalidate();
		// 重定向到login
		return "redirect:login";
	}

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String toLogin() {
		return "login";
	}

	// 用户注册操作
	@RequestMapping(value = "createuser", produces = "text/plain;charset=utf-8")
	public String add(String usercode, String username, String userpassword, String userage, String usersex,
			Model model, HttpSession session) {
		System.out.println(username);
		model.addAttribute("msg", "注册成功");
		userService.addUser(usercode, username, userpassword, userage, usersex);
		return "login";
	}

}
package com.oracle.curd.dao;

import org.apache.ibatis.annotations.Param;

import com.oracle.curd.bean.User;
import com.oracle.curd.bean.Record;

public interface UserDao {
	// 用户登录注册层
	public User findUser(@Param("usercode") String usercode, @Param("password") String password);// 登录方法@Param是为属性取另一个名字,方便数据库语法中赋值

	public void addUser(@Param("usercode") String usercode, @Param("username") String username,
			@Param("userpassword") String password, @Param("userage") String userage, @Param("usersex") String usersex);// 注册方法
	// 添加登录日志信息

	public void addRecord(Record r);
}
package com.oracle.curd.service;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.oracle.curd.bean.User;
import com.oracle.curd.dao.UserDao;
import com.oracle.curd.util.MyBatisUtil;
import com.oracle.curd.bean.Record;
/**
 * 业务层接口实体方法
 */
@Service
public class UserService {
	// 注入UserDao
	@Autowired
	private UserDao userDao;

	// 登录查找用户
	public User findUser(String usercode, String password) {
		User user = this.userDao.findUser(usercode, password);
		return user;
	}

	// 注册新用户
	public void addUser(String usercode, String username, String userpassword, String userage, String usersex) {
		userDao.addUser(usercode, username, userpassword, userage, usersex);

	}

	public void addRecordService(Record r) {
		SqlSession sqlSession = MyBatisUtil.getSession();
		userDao = sqlSession.getMapper(UserDao.class);

		userDao.addRecord(r);
		sqlSession.commit();
		sqlSession.close();
	}
}
<?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.oracle.curd.dao.UserDao">
	<!-- 查询登录用户 -->
	<select id="findUser" parameterType="User" resultType="User">
	    select * from sys_user where user_code = #{usercode} and user_password = #{password} and user_state = '1'
    </select>
	<!-- 注册新用户 -->
	<insert id="addUser" parameterType="String">
    insert into sys_user(
	                     user_code,
	                     user_name,
	                     user_password,
	                     user_state,
	                     user_age,
	                     user_sex
	             )
	             values(
	                    #{usercode},
	                    #{username},
	                    #{userpassword},
	                    "1",
	                    #{userage},
	                    #{usersex}
	            )
    </insert>
	<!-- 登录日志 -->
	<select id="addRecord" parameterType="record">
	    insert into record(event,event_time,event_ip)
	    value(#{event},#{event_time},#{event_ip})
	</select>
</mapper>

UserController User user = userService.findUser(usercode, password);判断是这里出现了问题,但是看不出来

《这是记忆中的一本书!》的主页 《这是记忆中的一本书!》 | 菜鸟二级 | 园豆:222
提问于:2020-07-14 05:06
< >
分享
所有回答(1)
0

源代码本身应该问题不大,最主要的是MyBatis的配置文件/SSM/src/main/resources/mybaties.xml使用了驼峰式命mapUnderscoreToCamelCase导致数据库连接时数据表属性去掉了下划线_,与bean对应不上了

《这是记忆中的一本书!》 | 园豆:222 (菜鸟二级) | 2020-07-14 10:24

详细代码参照上文GitHub链接

清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册