在一家互联网公司做了整整一年了,这家公司用到的技术还是蛮好的,nodejs+java+mongoDB,java上基本没用啥框架,所以很久没有用ssm框架开发东西了,最近有想着跳槽,准备把之前的东西复习一遍,所以就有了这边搭建ssm的博文,在搭建的时候也遇到了一些问题,一下也会一一写出来并提出解决办法的,好了,开始
commons-logging-1.0.4.jar:用于日志的输出
第一步:安装java虚拟机,配置环境变量,这里我就不多说了,百度一大堆教程
第二步:安装eclipse
第三部:创建web动态项目
第四部:创建包结构,文件结构,目录如下:
com.trybuy.dao:操作数据库的接口
com.trybuy.daoimpl:操作数据库接口实现类
com.trybuy.service:业务接口(此接口继承dao层接口,接口是单继承)
com.trybuy.service.impl:业务实现类
com.trybuy.entity:主要放置实体类的
com.trybut.mapping:主要放置映射实体类映射mybatis的.xml文件
com.trybut.control:放置控制层(这个报主要是做继承springmvc用的,在这里用不到)
com.trybut.test:测试包
resource:放置资源文件的(spring的配置文件,数据源文件,日志配置文件)
lib:放一些依赖jar包,放进来了要add bulitpath一下
很多刚开始学三大框架的人有一个疑问,service层和dao层的代码基本生一模一样,为什么不直接用dao,还要扩展一个service呢,这是因为架构要是数据和业务分离,dao层只管数据方面的事情,不做任何业务方面的事情,所以在dao层里面千万别写业务代码,不然被你项目经理看到会挨骂的,service处理完业务逻辑后调用dao层获取数据或持久化数据
commons-logging-1.0.4.jar:用于日志的输出
druid-1.0.4.jar:阿里巴巴提供的一个数据源接口,用于在spring配置文件配置dataSource的
mybatis-3.2.7.jar:java提供mybatis操作数据库的jar包,包括sqlSessionFactory
mybatis-generator-core-1.3.2.jar:这个包主要是用于根据数据库生成entity,dao,mapping这个三东西的(后面另开一篇博文讲这个)
mybatis-spring-1.2.1.jar:spring整合mybatis的jar包
mysql-connector-java-5.1.18-bin.jar:mysql的数据库驱动
spring-aop-4.1.6.RELEASE.jar:spring提供的aop功能
spring-aspects-4.1.6.RELEASE.jar:
spring-beans-4.1.6.RELEASE.jar:spring最核心的jar包,主要是一些beanFactory,用于根据spring的配置文件生成bean的
spring-context-4.1.6.RELEASE.jar:spring的上下文提供了一些ClassPathXmlApplicationContext,等接口读取spring的配置文件
spring-core-4.1.6.RELEASE.jar:spring的核心包
spring-expression-4.1.6.RELEASE.jar:spring的表达式
spring-jdbc-4.1.6.RELEASE.jar:spring整合jdbc的jar包
spring-test-4.1.6.RELEASE.jar:spring用于测试的包,跟junit有点类似
spring-tx-4.1.6.RELEASE.jar:spring的事务
spring-web-4.1.6.RELEASE.jar:spring用于开发web应用的包
spring-webmvc-4.1.6.RELEASE.jar:spring的一个mvc框架
第六步:介绍完了jar包了,现在开始写配置文件:
这是spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="com.trybuy.entity"></context:component-scan>
<context:component-scan base-package="com.trybuy.sevice.impl"></context:component-scan>
<context:component-scan base-package="com.trybuy.daoimpl"></context:component-scan>
<!--
<context:component-scan base-package="com.trybuy.entity;com.trybuy.sevice.impl;com.trybuy.daoimpl" />
-->
<!-- 引入jdbc配置文件 -->
<!-- <context:property-placeholder location="classpath:/resource/jdbc.properties" />
--> <!-- dataSource 配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/trybuy" />
<property name="username" value="root" />
<property name="password" value="*******" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="false" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat" />
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/trybuy/mapping/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.trybuy.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
spring在启动ioc容器的时候会读取配置文件,就是通过spring-context.jar这个包里面的ClassPathXmlApplicationContext读取配置文件,然后在解析xml,根据配置文件生成容器里面的实体类,就是在spring的ioc容器启动完成了,那么配置到ioc容器里面的类全部已经被实例化了
然后我们来写dao层,实际上不须要写上面写的那个工具可以生成,测试为了以后看的明白还是写一下