SSM整合

来源:互联网 发布:python官方文档使用 编辑:程序博客网 时间:2024/06/18 11:15

SSM整合

导入jar包:mybatis,spring,springmvc,mysql相关的spring-jar包。

aopalliance.jar

asm-3.3.1.jar

aspectjweaver.jar

cglib-2.2.2.jar

commons-fileupload-1.3.1.jar

commons-io-2.2.jar

commons-logging-1.1.3.jar

druid-1.0.26.jar:超级数据库连接池,连接速度相当的快。稳定!

ClassDriverName---可以不要,可以不写!

gson-1.7.1.jar

jackson-annotations-2.7.0.jar

jackson-core-2.7.0.jar

jackson-databind-2.7.0.jar

javassist-3.17.1-GA.jar

jstl-1.2.jar

log4j-1.2.17.jar

log4j-api-2.0-rc1.jar

log4j-core-2.0-rc1.jar

mybatis-3.2.7.jar

mybatis-spring-1.2.3.jar

mysql-connector-java-5.1.30.jar

slf4j-api-1.7.5.jar

slf4j-log4j12-1.7.5.jar

spring-aop-4.1.6.RELEASE.jar

spring-aspects-4.1.6.RELEASE.jar

spring-beans-4.1.6.RELEASE.jar

spring-context-4.1.6.RELEASE.jar

spring-context-support-4.1.6.RELEASE.jar

spring-core-4.1.6.RELEASE.jar

spring-expression-4.1.6.RELEASE.jar

spring-instrument-4.1.6.RELEASE.jar

spring-instrument-tomcat-4.1.6.RELEASE.jar

spring-jdbc-4.1.6.RELEASE.jar

spring-jms-4.1.6.RELEASE.jar

spring-messaging-4.1.6.RELEASE.jar

spring-orm-4.1.6.RELEASE.jar

spring-oxm-4.1.6.RELEASE.jar

spring-test-4.1.6.RELEASE.jar

spring-tx-4.1.6.RELEASE.jar

spring-web-4.1.6.RELEASE.jar

spring-webmvc-4.1.6.RELEASE.jar

spring-webmvc-portlet-4.1.6.RELEASE.jar

spring-websocket-4.1.6.RELEASE.jar

standard-1.1.2.jar


添加配置文件:web.mxl,springmvc.xml,spring相关的配置文件

Web.xml: 核心---- 第一件事:读取springmvc的核心配置文件

第二件事读取spring相关的配置文件。

Springmvc核心配置文件,专门来处理springmvc的相关配置!


开始搭建项目结构



添加注解:

@Autowire : 默认按照类型注入!

@Service

public class UserServiceImpl implements UserService {

   

    //自动装配 @Autowired默认按照类型注入,@Resource先按照名称注入,如果名称找不到,找type

    @Autowired

    private UserMapperuserMapper;

    public void setUserMapper(UserMapper userMapper) {

       this.userMapper =userMapper;

    }

}


@Controller

public class UserController {

    //  自动装配

    @Autowired

    private UserServiceuserService;

    public void setUserService(UserService userService) {

       this.userService =userService;

    }

   

    @RequestMapping("findAll")

    @ResponseBody

    public List<User> findAll(){

       List<User> list = userService.findAll();

       returnlist;

    }

}


页面测试

<%@ page language="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPEhtmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<scripttype="text/javascript"src="<%=request.getContextPath()%>/js/jquery.min.js"></script>

<title>Insert title here</title>

<scripttype="text/javascript">

    $(function(){

       $.ajax({

           type:"post",

           url:"/springsm/findAll",

           dataType:"json",

           success:function(data){

              for(var i = 0; i<data.length;i++){

                  var c ="<tr><td>"+data[i].id+"</td><td>"+data[i].username+"</td><td>"+data[i].pwd+"</td></tr>"

                  $("#tab").append(c);

              }

           }

       })

    })

</script>

</head>

<body>

    <tableid="tab">

       <tr>

           <th>id</th>

           <th>name</th>

           <th>pwd</th>

       </tr>

    </table>

</body>

</html>


整合的坑:


1      Web.xml 中

1.1     springmvc核心配置文件名

1.2     spring配置文件名

1.3     <context-param>

1.4                 <param-name>contextConfigLocation</param-name>

1.5                 <param-value>classpath:applicationContext-*.xml</param-value>

1.6          </context-param>

1.7     <servlet-mapping>

1.8                 <servlet-name>mvc</servlet-name>

1.9               <url-pattern>/</url-pattern>千万不能配置/*

1.10        </servlet-mapping>


2      扫描mybatis配置文件

2.1     <propertyname="basePackage" value="com.bjsxt.mapper"/>

2.2    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">阿里巴巴,之前我们使用的是spring-mybatis整合jar包的DataSource。


3     使用注解必须扫描@Controller  @Service  @Autowired

3.1     <context:component-scanbase-package="com.bjsxt.controller"/>

3.2     <context:component-scanbase-package="com.bjsxt.service"/>


4    在springmvc中不要添加一些不相干的标签

4.1   例如拦截器等!


5   大家千万要注意大小写,空格,中文字符,配置文件的路径!