为了账号安全,请及时绑定邮箱和手机立即绑定

java的ssm框架小例子,

标签:
Java
java的ssm框架,举个小例子
在公司自学两个月java,等于看了两个月源代码,就接手了一个web系统的后台,(因为人手不够,赶鸭子上架)。就三个表,业务逻辑不多,所以几天就大体上写完了,就差和前端的调一些接口问题,在这里写一个小demo,回忆一下下。(因为是第一次写后台,所以取数据的所有业余逻辑代码也都写在controller层中,这是不对的,业务逻辑要在serviceImpl中写)
     ##### 至于怎么配置ssm框架,还不会。

controller层:

     @Controller
     @RequestMapping("/accountHomePage")
     public class ActionAccountHomePage {
        Logger logger = Logger.getLogger(ActionAccountHomePage.class);

        @Autowired
        private AccountHomePageServiceImpl accountHomePageService;

        @Autowired 
        private AccountChartsServiceImpl accountChartsService;

        @RequestMapping("/tohomepage")
        public ModelAndView indexpage(HttpServletRequest 
                   request,HttpSession session){

        Login login = ActionFunc.getLogin(request);

        if (!login.isLogined()) {
            return new ModelAndView("login/login");
        }

        if(session.getAttribute("userid")==null){
            session.setAttribute("userid", request.getParameter("userid"));
        }

        return new ModelAndView("welcome/welcome");
    }

     @RequestMapping("/showhomeinfo")
     @ResponseBody
     public StringMap showhomeinfo(HttpServletRequest request){

        StringMap map = new StringMap();
        JSONObject chartcounts = new JSONObject();
        JSONArray chartarray = new JSONArray();
        List<ChartInfo> tcounts = accountChartsService.selectInfoTimeCounts();
        int icount = accountHomePageService.InfoCount();
        int ccount = accountHomePageService.ControlCount();
        int rcount = accountHomePageService.ResultCount();
        List<Info> infos = accountHomePageService.selectAccountInfoTime();

        if(tcounts.size() != 0 || tcounts != null){
            for(ChartInfo cpt : tcounts){
                chartcounts.put("time",cpt.getxData());
                chartcounts.put("数量", cpt.getyData());
                chartarray.add(chartcounts);
            }
        }
        logger.info("容量【"+tcounts.size()+"】:");

        JSONArray jsonArray = new JSONArray();
        JSONObject counts = new JSONObject();
        JSONObject obj = new JSONObject();

        for(Info info : infos){
            obj.put("id",info.getAccountId());
            obj.put("name",info.getAccountName());
            obj.put("e-mail",info.getAccountEmail());
            obj.put("phone", info.getAccountPhone());
            obj.put("site-product", info.getSourceSiteProduct());
            jsonArray.add(obj);
        }

        counts.put("icount", icount);
        counts.put("ccount", ccount);
        counts.put("rcount", rcount);

        map.set("data", jsonArray);
        map.set("charcount", chartarray);
        map.set("counts", counts);
        return map;
    }
    }

Dao接口:

    @Repository
    public interface AccountHomePageDao {

    int InfoCount();

    int ControlCount();

    int ResultCount();

    List<Info> selectAccountInfoTime();

    }

mapper xml 文件(mybatis):

    <?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">
    <!-- 
    namespace:必须与对应的接口全类名一致
    id:必须与对应接口的某个对应的方法名一致   
    -->
    <mapper namespace="xxx.xxx.xxx.dao.account.AccountHomePageDao">

    <select id="InfoCount"  resultType="int">
        select count(*) from account_info where 1=1
    </select>

    <select id="ControlCount" resultType="int">
        select count(control) from account_control_task where 1=1
    </select>

    <select id="ResultCount" resultType="int">
        select count(result) from account_control_task where 1=1
    </select>

    <select id="selectInfoTimeCounts" resultType="String">
        select account_record_time as time,count(account_id) as cCount from account_info where account_record_time >= date(now()) - interval 15 day         
        group by day(account_record_time)
    </select>

    <select id="selectAccountInfoTime" resultMap="resultMap">
        select account_id accountId,account_name accountName,account_email accountEmail,account_phone accountPhone,source_site_product sourceSiteProduct from account_info where 1=1
        order by account_record_time DESC LIMIT 0,8
    </select>

    <resultMap id="resultMap" type="Info">
        <id property="accountId" column="account_id" jdbcType="VARCHAR" />
        <result property="accountName" column="account_name" jdbcType="VARCHAR" />
        <result property="accountEmail" column="account_email" jdbcType="VARCHAR" />
        <result property="accountPhone" column="account_phone" jdbcType="VARCHAR" />
        <result property="sourceSiteProduct" column="source_site_product" jdbcType="VARCHAR" />
    </resultMap>
</mapper>

service 接口:

public interface AccountHomePageService {

    public int InfoCount();
    public int ControlCount();
    public int ResultCount();

    public List<Info> selectAccountInfoTime();
}

serviceImpl 实现类

@Service("AccountHomePageService")
public class AccountHomePageServiceImpl implements AccountHomePageService {

    @Resource
    private AccountHomePageDao accountHomePageDao;

    @Override
    public int InfoCount() {
        // TODO Auto-generated method stub
        return this.accountHomePageDao.InfoCount();
    }

    @Override
    public int ControlCount() {
        // TODO Auto-generated method stub
        return this.accountHomePageDao.ControlCount();
    }

    @Override
    public int ResultCount() {
        // TODO Auto-generated method stub
        return this.accountHomePageDao.ResultCount();
    }

    @Override
    public List<Info> selectAccountInfoTime() {
        // TODO Auto-generated method stub
        return this.accountHomePageDao.selectAccountInfoTime();
    }

}

pojo 实体层: 是根据数据库表建立的

还有 com.cn.xxx.util 放一些通用的方法。上面就用到一些

最后表达一下自己写代码的感受,虽然还不规范,但是自己敢动手写,哈哈哈,还是很有成就感的
点击查看更多内容
21人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消