SSM( Spring+Struts2+Mybatis )整合框架
Spring:
提供注解和自动装配,就是Spring的两个核心部分:IOC(反向控制)和 AOP(面向切面编程)。
IOC(反向控制)容器,装载bean(也就是我们Java中的类,当然也包括service dao里面的),有了这个机制,我们就不用在每次使用这个类的时候为它初始化。spring的AOP(面向切面编程),用来事务管理等。
Struts2:
Struts2框架mvc 实现低耦合,便于程序的维护,配置文件控制流程的转向 清晰,主要负责具体业务的实现和页面的转向。
Mybatis:
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
这里提一下Mybatis和hibernate的不同之处
1.Mybatis 不是完全的ORM框架 需要我们直接编写sql 可以通过sql注入和XML注解灵活配置要允许的sql。
2.Mybatis 严格控制sql的执行性能 非常适合关系型数据库 无法做到与数据库无关性。
3.Hibernate 对象/关系映射强 数据库无关系强。
接在来直接整一个案例来看看具体操作
工程主要目录结构
一个实体类
public class Goodtable implements Serializable {
private Integer goodId;
private String goodName;
private Double goodPrice;
private Integer goodNum;
private String goodOther;
private Date goodTime;
private String goodImg;
private Integer goodDel;
get、set方法..
构造器..有参、无参
...
}
dao层接口
public interface IGoodDao {
List<Goodtable> queryGoodList(Goodtable goodEnt); //方法一
boolean addGoodTable(Goodtable goodEnt); //方法二
}
servlet层接口
public interface IGoodService {
boolean addGoodTable(Goodtable goodEnt);
}
servlet层接口实现类
public class GoodServiceImpl implements IGoodService{
private IGoodDao goodDao;
public IGoodDao getGoodDao() {
return goodDao;
}
public void setGoodDao(IGoodDao goodDao) {
this.goodDao = goodDao;
}
@Override
public boolean addGoodTable(Goodtable goodEnt) {
return goodDao.addGoodTable(goodEnt);
}
}
mapper 映射文件 Goodtable.xml
<?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="com.ym.dao.IGoodDao">
<!-- 配置启动二级 缓存 -->
<cache eviction="FIFO" flushInterval="600000" size="512" readOnly="true" />
<!-- 写 实体的映射关系 -->
<resultMap type="Goodtable" id="GoodtableList">
<id column="goodId" property="goodId"/>
<result column="goodName" property="goodName"/>
<result column="goodPrice" property="goodPrice"/>
<result column="goodNum" property="goodNum"/>
<result column="goodOther" property="goodOther"/>
<result column="goodTime" property="goodTime"/>
<result column="goodImg" property="goodImg"/>
<result column="goodDel" property="goodDel"/>
</resultMap>
<!-- 写接口中的方法实现 id代指的是 接口中的方法名称 -->
<select id="queryGoodList" resultMap="GoodtableList" parameterType="Goodtable">
select * from goodtable where 1=1
<if test="goodId!=null"> and goodId=#{goodId}</if>
<if test="goodName!=null"> and goodName like #{goodName}</if>
order by goodId desc
</select>
<insert id="addGoodTable" parameterType="Goodtable">
insert into goodtable
<trim prefix="(" suffix=")">
goodId
<if test="goodName!=null">,goodName</if>
<if test="goodPrice!=null">,goodPrice</if>
<if test="goodNum!=null">,goodNum</if>
<if test="goodOther!=null">,goodOther</if>
<if test="goodTime!=null">,goodTime</if>
<if test="goodImg!=null">,goodImg</if>
<if test="goodDel!=null">,goodDel</if>
</trim>
values
<trim prefix="(" suffix=")">
null
<if test="goodName!=null">,#{goodName}</if>
<if test="goodPrice!=null">,#{goodPrice}</if>
<if test="goodNum!=null">,#{goodNum}</if>
<if test="goodOther!=null">,#{goodOther}</if>
<if test="goodTime!=null">,#{goodTime}</if>
<if test="goodImg!=null">,#{goodImg}</if>
<if test="goodDel!=null">,#{goodDel}</if>
</trim>
</insert>
<delete id="deleteGoodtable" parameterType="int">
delete from goodtable where goodId=#{goodId}
</delete>
<update id="updateGoodtable" parameterType="Goodtable">
update goodtable set
<if test="goodName!=null">goodName=#{goodName}</if>
<if test="goodPrice!=null">,goodPrice=#{goodPrice}</if>
<if test="goodNum!=null">,goodNum=#{goodNum}</if>
<if test="goodOther!=null">,goodOther=#{goodOther}</if>
<if test="goodTime!=null">,goodTime=#{goodTime}</if>
<if test="goodImg!=null">,goodImg=#{goodImg}</if>
<if test="goodDel!=null">,goodDel=#{goodDel}</if>
where goodId=#{goodId}
</update>
</mapper>
action层
import java.io.File;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.ym.entity.Goodtable;
import com.ym.service.IGoodService;
public class GoodAction extends ActionSupport{
private IGoodService goodService; //servlet层接口
private Goodtable goodEnt; //实体类
get、set方法....
//方法一
public String addGoodEnt(){
try {
if(goodEnt!=null){
goodService.addGoodTable(goodEnt);
return SUCCESS;
}
} catch (Exception e) {
e.printStackTrace();
}
return ERROR;
}
}
applicationContext-common.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 启动注解 -->
<context:annotation-config/>
<!-- 扫描自定义文件 -->
<context:component-scan base-package="com.ym.*"/>
<!-- 配置DataSource 或者 BasicDataSource 数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/zzdb?
userUnicode=true&&characterEncoding=utf-8
&&serverTimezone=GMT%2B8"/>
<property name="username" value="root" />
<property name="password" value="154355"/>
</bean>
<!-- 创建sessionFactory工厂 -->
<bean id="sessionFactoty" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
<!-- 引入关联的mapper文件 -->
<property name="mapperLocations" value="classpath:com/ym/**/*.xml" />
</bean>
<!-- 配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置传播隔离特性 -->
<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<!-- 传播+事物+隔离 -->
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">readOnly</prop>
</props>
</property>
</bean>
</beans>
applicationContext-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
*********************《dao层》*********************
<!-- 确定作用范围scope -->
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean" scope="prototype">
<!-- 引入接口 -->
<property name="mapperInterface" value="com.ym.dao.IUserDao" />
<property name="sqlSessionFactory" ref="sessionFactoty" />
</bean>
*********************《servlet层》*********************
<bean id="goodService" class="com.ym.service.impl.GoodServiceImpl" scope="prototype">
<property name="goodDao" ref="goodDao"/>
</bean>
*********************《action层》*********************
<bean id="goodAction" class="com.ym.action.GoodAction" >
<property name="goodService" ref="goodService" />
</bean>
//文件上传
<bean id="fileAction" class="com.ym.action.UpFile2" ></bean>
</beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- MyBatis的全局配置文件 -->
<configuration >
<!-- 日志 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<typeAlias type="com.ym.entity.Goodtable" alias="Goodtable"/>
</typeAliases>
</configuration>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="pk" extends="struts-default,json-default" namespace="/">
<action name="good_*" class="goodAction" method="{1}">
<result name="success" >/main.jsp</result>
</action>
</package>
</struts>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext-*.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 初始化配置 -->
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,/config/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
以上就是整个流程中大致内容