博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring中整合ssm框架注解版
阅读量:7200 次
发布时间:2019-06-29

本文共 5803 字,大约阅读时间需要 19 分钟。

和xml版差不多,只不过创建对象的方式是由spring自动扫描包名,然后命名空间多一行context代码在application.xml中,然后将每个对象通过注解创建和注入:

直接上代码:

1.userDao

package cn.mr.li.dao;import java.util.List;import cn.mr.li.entity.User;public interface UserDao {    List
getUser();}

2.userDaoImpl

package cn.mr.li.dao.impl;import java.util.List;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.support.SqlSessionDaoSupport;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import cn.mr.li.dao.UserDao;import cn.mr.li.entity.User;/** * 因为直接调用的都是Service接口类型,不直接注入impl实现类,所以不用写@Repository("userDao")@Service("userService")这样的 * @author Administrator * */@Repository()public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {        @Autowired    @Override    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {        super.setSqlSessionFactory(sqlSessionFactory);    }        @Override    public List
getUser() { return this.getSqlSession().selectList("cn.mr.li.entity.user.mapper.getAll"); }}

3.userService

package cn.mr.li.service;import java.util.List;import cn.mr.li.entity.User;public interface UserService {    List
getAll();}

4.userServiceImpl

package cn.mr.li.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import cn.mr.li.dao.UserDao;import cn.mr.li.entity.User;import cn.mr.li.service.UserService;@Service()public class UserServiceImpl implements UserService {    @Autowired    private UserDao userDao;        @Override    public List
getAll() { return userDao.getUser(); } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; }}

5.user对象

package cn.mr.li.entity;public class User {    private int id;        private String name;        private int age;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public User(int id, String name, int age) {        super();        this.id = id;        this.name = name;        this.age = age;    }        public User() {    }}

6.user.mapper.xml

7.userAction

package cn.mr.li.action;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import cn.mr.li.entity.User;import cn.mr.li.service.UserService;/** *@Scope注解的意思是为了配置让每个被访问的Action都不是单例的,否则本次访问到下次在访问,上次的数据就会还存在。 * @author Administrator * */@Controller()@Scope("prototype")public class UserAction {    private List
list; @Autowired private UserService userService; /** * 这里必须返回success,因为访问的时候回判断状态,如果不是success让数据不予扎实,页面就会404 * @return */ public String list(){ list = userService.getAll(); return "success"; } public List
getList() { return list; } }

8.applicationContext.xml

9.mybatis.cfg.xml

s

10.struts.xml

/list.jsp

11.web.xml

contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp

12.list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    
编号 姓名 密码
${bean.id } ${bean.name } ${bean.age }

我的访问路径:http://localhost:8080/spring001/list.action

项目目录结构:

 

转载于:https://www.cnblogs.com/li-yan-long/p/10672428.html

你可能感兴趣的文章
Java压缩技术的学习
查看>>
Java获取客户端真实IP地址的两种方法
查看>>
MVC3中,在control里面三种Html代码输出形式
查看>>
修杰楷_百度百科
查看>>
第七周 Word文档修订
查看>>
LINQ 图解 LINQ学习第三篇
查看>>
微信公众平台开发(七) 聊天机器人功能开发
查看>>
jQuery源码分析系列
查看>>
UVA 10817 Headmaster's Headache(DP +状态压缩)
查看>>
百度地图纠偏处理
查看>>
Winform开发--控件
查看>>
HDUOJ---(4708)Rotation Lock Puzzle
查看>>
TFS2010中文版安装
查看>>
【Android】Handler详解
查看>>
太有才了!创新的街头涂鸦手绘欣赏【中篇】
查看>>
ZOJ 2624 Popo's Lamps(DP 记忆化搜索)
查看>>
ant中copy操作学习心得(转)
查看>>
aspx中的表单验证 jquery.validate.js 的使用 以及 jquery.validate相关扩展验证(Jquery表单提交验证插件)...
查看>>
$.data(elem, key, val) 和 elem.data(key, val)
查看>>
NAND Flash Bad Block Table
查看>>