ssm项目-需求:对用户表记录数进行查询

1-创建项目的目录结构
2-在pojo中创建实体类User

package cn.smbms.pojo;

import java.util.Date;

//用户实体类 方法:setter和getter方法
public class User {

    private Integer id;//主键id
    private String userCode;//用户编码
    private  String userName;//用户名称
    private String userPassword;//用户密码
    private String gender;//用户性别
    private Date  birthday;//出生日期
    private String phone;//电话
    private String address;//地址
    private Integer userRole;//用户角色
    private Integer createdBy;//创建人
    private Date creationDate;//创建时间
    private Integer modifyBy;//修改人
    private Date modifyDate;//修改时间

    public Integer getId() {
        return id;
    }

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

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getUserRole() {
        return userRole;
    }

    public void setUserRole(Integer userRole) {
        this.userRole = userRole;
    }

    public Integer getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Integer getModifyBy() {
        return modifyBy;
    }

    public void setModifyBy(Integer modifyBy) {
        this.modifyBy = modifyBy;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }
}

3-在dao.user包中创建UserMapper.java和UserMapper.xml
dao包:负责和数据进行交互的代码和操作
UserMapper.java

package cn.smbms.dao.user;

public interface UserMapper {

    //需求是:统计用户表中的记录数
    // select count(1) as count from smbms_user ;
    public int count();
}

UserMapper.xml
https://mybatis.org/mybatis-3/getting-started.html
sql映射文件

<?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="cn.smbms.dao.user.UserMapper">
    <!--id 填写接口中的方法名  resultType 填写 返回类型-->
    <select id="count" resultType="int">
        select count(1) as count from smbms_user
    </select>
</mapper>

4-在mybatis-config.xml文件中添加sql映射文件的引入

 <!--引入sql映射文件-->
    <mappers>
        <mapper resource="cn/smbms/dao/user/UserMapper.xml"/>
    </mappers>

5-测试类

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class UserMapperTest {

    //创建日志对象
    Logger logger=Logger.getLogger(UserMapperTest.class);

    //测试方法
    @Test
    public void test(){
        SqlSession sqlSession=null;
        try {
            //1 获得mybatis-config.xml文件的输入流
            InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
            //2 创建SqlSessionFactory对象,读取上述流
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
            //3  创建SqlSession对象(Connection)
            sqlSession=sqlSessionFactory.openSession();
            //4 通过sqlSession对象对mapper文件对应的接口进行方法的读取 全路径+方法名
            int count = sqlSession.selectOne("cn.smbms.dao.user.UserMapper.count");
            logger.debug("UserMapperTest test count:"+count);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
           sqlSession.close();
        }

    }

}
展开阅读全文

SSM框架中如何实时统计在线人?

12-19
已经尝试过implements HttpSessionListener,然后在web.xml中配置监听器发现并不管用;但是当把spring本身的监听器注释掉后之前配置的计监听器就起作用了,但是项目进不去了. ``` //spring的监听器 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ``` ``` //统计在线人的监听器 <listener> <listener-class>web.CountListener</listener-class> </listener> ``` ``` //代码 package web; import javax.servlet.ServletContext; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; @WebListener public class CountListener implements HttpSessionListener{ /** * session对象创建之后,容器会调用 * 此方法。 * 注: * HttpSessionEvent是事件对象。 */ public void sessionCreated( HttpSessionEvent arg0) { System.out.println( "sessionCreated方法"); HttpSession session = arg0.getSession(); ServletContext sctx = session.getServletContext(); //通过Servlet上下文获得绑订的人 Integer count = (Integer)sctx.getAttribute( "count"); if(count == null){ //第一个用户 count = 1; }else{ //不是第一个用户 count ++; } sctx.setAttribute("count", count); } /** * session对象销毁之后,容器会调用此方法。 */ public void sessionDestroyed( HttpSessionEvent arg0) { System.out.println( "sessionDestroyed方法"); ServletContext sctx = arg0.getSession().getServletContext(); Integer count = (Integer)sctx.getAttribute( "count"); count --; sctx.setAttribute("count", count); } } ``` **最后总结:** 1.SSM框架中到底如何实时统计在线人? 2.两个监听器同时写在web.xml里貌似只会执行spring的,不会执行web.CountListener,这个怎么办?
©️2020 CSDN 皮肤主题: 游动-白 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值