1、导入ssm框架所需要的jar包。
2、配置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">
<mapper namespace="com.ztkj.pig.mapper.PigMapper">
<resultMap type="com.ztkj.entity.Pig" id="pigMap">
<id property="pigId" column="pig_id"/>
<result property="pigName" column="pig_name"/>
<result property="pigBirth" column="pig_birth"/>
<result property="pigWeight" column="pig_weightm"/>
</resultMap>
<!-- 添加 -->
<insert id="addPig" parameterType="com.ztkj.entity.Pig">
<selectKey keyProperty="pigId" order="BEFORE" resultType="int">
select seq_t_pig.nextval from dual
</selectKey>
insert into t_pig(pig_id,pig_name,pig_birth,pig_weight) values
(#{pigId},#{pigName,jdbcType=VARCHAR},#{pigBirth},#{pigWeight})
</insert>
<!-- 删除 -->
<delete id="delPig" parameterType="int">
delete from t_pig where pig_id=#{pigId};
</delete>
<!-- 修改 -->
<update id="updatePig" parameterType="com.ztkj.entity.Pig">
update t_pig
<set>
<if test="pigName!=null">pig_name=#{pigName},</if>
<if test="pigWeight!=null">pig_weight=#{pigWeight},</if>
<if test="pigBirth!=null">pig_birth=#{pigBirth}</if>
</set>
where pig_id=#{pigId}
</update>
<!-- 查询单条记录 -->
<select id="findPigById" resultMap="pigMap">
select * from t_pig where pig_id=#{pigId}
</select>
<!-- 查询所有的记录 -->
<select id="findAllPig" resultMap="pigMap">
select * from t_pig
</select>
</mapper>
3、整合spring-mvc,配置spring-mvc的配置文件。
例如:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 扫描controller层,自动注入里面所需要的对象 -->>
<context:component-scan base-package="com.ztkj.*.controller"/>
<!--配置ViewResource视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4、配置spring,整合各个框架。
例如:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- spring来扫描service层,自动注入 -->
<context:component-scan base-package="com.ztkj.*.service.impl"/>
<!-- 引入并加载jdbc配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- 此处加*表示只要在classpath(src)目录下都可以包括包里面 -->
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!--配置数据源供SqlSessionFactory使用-->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Oracle数据库的驱动 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!-- 数据库的URL -->
<property name="url" value="${jdbc.url}" />
<!-- 指定数据库的用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 指定数据库的密码 -->
<property name="password" value="${jdbc.pwd}" />
<!-- 指定数据库的最大连接数100 -->
<property name="maxActive" value="20" />
<!-- 指定数据库的最大空闲连接数30 -->
<property name="maxIdle" value="5" />
<!-- 指定数据库的最大等待数300 -->
<property name="maxWait" value="300" />
<!-- 指定数据库的默认自动提交 -->
<property name="defaultAutoCommit" value="true" />
<!-- 指定数据库的连接超时时是否启动自动回收 -->
<property name="removeAbandoned" value="true" />
<!-- 指定数据库的删除数据库连接的超时时长,数据库连接多久不用会被自动回收 -->
<property name="removeAbandonedTimeout" value="60" />
<property name="logAbandoned" value="true" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 设置SqlSessionFactory的数据源,以及映射文件-->
<property name="dataSource" ref="dataSource"/>
<!--让SqlSessionFactory读取mapper.xml映射文件-->
<property name="mapperLocations" value="classpath:com/ztkj/pig/mapper/*.xml"/>
</bean>
<!--自动扫描映射器,扫描dao层,注入到ioc容器中与mapper关联 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--让SqlSessionFactory和mapper关联,用于spring-mvc底层创建dao层接口是实现类-->
<property name="basePackage" value="com/ztkj/pig/mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
</bean>
<!--使用spring的jdbc事务管理器来管理mybatis事务 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务的传播特性
创建事务方法切面 也就是说哪些方法需要加入事务
transaction-manager属性的值是上面我们创建的事务的id
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- propagation required 表示若当前方法没有事务,则创建事务 -->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--
创建事务包类切面
expression是指需要添加事务的正则表达式
第一个*表示方法的返回值
第一个..表示如果按照模块分service的话,每个模块的service都需要添加事务
第二个*表示该包下的所有类
第三个*表示该包类下的所有方法
第二个..表示方法的所有所有参数
-->
<aop:pointcut expression="execution(* com.ztkj.*.service.impl.*.*(..))" id="myCut"/>
<!-- 通知 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="myCut"/>
</aop:config>
</beans>