使用idea2017搭建SSM框架及测试(5)

@RequestMapping("/showUser.do")
    public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        long userId = Long.parseLong(request.getParameter("id"));
        User user = this.userService.selectUser(userId);
        ObjectMapper mapper = new ObjectMapper();
        response.getWriter().write(mapper.writeValueAsString(user));
        response.getWriter().close();
    }

}

IUserDao

package com.chatRobot.dao;

import com.chatRobot.model.User;

public interface IUserDao {

User selectUser(long id);

}

User

package com.chatRobot.model;

import java.util.Date;

public class User {

private long id;
    private String email;
    private String password;
    private String username;
    private String role;
    private int status;
    private Date regTime;
    private String regIp;

public long getId() {
        return id;
    }

public void setId(int id) {
        this.id = id;
    }

public String getEmail() {
        return email;
    }

public void setEmail(String email) {
        this.email = email;
    }

public String getPassword() {
        return password;
    }

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

public String getUsername() {
        return username;
    }

public void setUsername(String username) {
        this.username = username;
    }

public String getRole() {
        return role;
    }

public void setRole(String role) {
        this.role = role;
    }

public int getStatus() {
        return status;
    }

public void setStatus(int status) {
        this.status = status;
    }

public Date getRegTime() {
        return regTime;
    }

public void setRegTime(Date regTime) {
        this.regTime = regTime;
    }

public String getRegIp() {
        return regIp;
    }

public void setRegIp(String regIp) {
        this.regIp = regIp;
    }

}

IUserService

package com.chatRobot.service;

import com.chatRobot.model.User;

public interface IUserService {

public User selectUser(long userId);

}

UserServiceImpl

package com.chatRobot.service.impl;

import com.chatRobot.dao.IUserDao;
import com.chatRobot.model.User;
import com.chatRobot.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userService")
public class UserServiceImpl implements IUserService {

@Resource
    private IUserDao userDao;

public User selectUser(long userId) {
        return this.userDao.selectUser(userId);
    }

}

UserDao.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">

<!-- 设置为IUserDao接口方法提供sql语句配置 -->
<mapper namespace="com.chatRobot.dao.IUserDao">

<select resultType="User" parameterType="long">
        SELECT * FROM user WHERE id = #{id}
    </select>

</mapper>

然后新建个测试类,来测试mybatis

IUserDaoTest

package com.chatRobot.dao;

import com.chatRobot.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

// 加载spring配置文件
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-mybatis.xml"})
public class IUserDaoTest {

@Autowired
    private IUserDao dao;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/13970.html