SSM框架的配置

a:导入jar包

Spring+ SpringMVC + MyBatis + Mybatis-spring整合包 AOP联盟+织入 + c3p0 数据库连接池 + MySQL连接驱动 + jstl

b:创建一个springmvc文件
<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- 1.配置注解扫描位置 -->
    <context:component-scan base-package="main.yzc.backOffice.controller" />
    
    <!-- 2.配置注解处理映射-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

    <!--3.配置适配器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
    
    <!-- 4.配置springmvc视图解析器 视图解析器解析的视频路径为:前缀 + 后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!--文件上传,限制大小 一定要配一个id 名字是固定的 multipartResolver-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="2048000"></property>
    </bean>

</beans>
c:在web.xml添加springmvc配置
<!--    springmvc的配置-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 3.0的springmvc 默认加载WEB-INF下的dispatcher-servlet.xml文件 3.2的springmvc
            加载DispatcherServlet-servlet.xml文件 -->
        <init-param>
            <!-- 修改默认springmvc加载的配置文件路径 -->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
d:先配置一个Controller跑出一个页面
e:通过MyBatis的逆向工程生成JavaBean/Mapper(省略)
f:修改ItemsMapper.java和ItemsMapper.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包名下面的接口名一样-->
<mapper namespace="main.yzc.backOffice.mapper.ItemsMapper">
    <!--    配置缓存 type不写 默认使用的是mybatis自带的缓存技术 ,perpetualCahce-->
    <!--    <cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>-->
    <select id="findAllList" resultType="items">
        select * from items
    </select>
    
    <select id="save" parameterType="main.yzc.backOffice.model.Items">
        insert into items(name,price,detail,createtime) value(#{name},#{price},#{detail},#{createtime})
    </select>
    
    <select id="delete" parameterType="int">
        delete from items where id = #{value}
    </select>
    
    <select id="findById" parameterType="int" resultType="items">
        select * from items where id = #{value}
    </select>
    
    <select id="update" parameterType="main.yzc.backOffice.model.Items">
        update items set name = #{name},price = #{price},detail = #{detail},pic = #{pic} where id = #{id}
    </select>
    
</mapper>
g:定义Service层接口并实现
package main.yzc.backOffice.service.impl;

import main.yzc.backOffice.mapper.ItemsMapper;
import main.yzc.backOffice.model.Items;
import main.yzc.backOffice.model.User;
import main.yzc.backOffice.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ItemsServiceImpl implements ItemsService {

    @Autowired
    private ItemsMapper itemsMapper;

    @Override
    public List<Items> findAll() {
        return itemsMapper.findAllList();
    }

    @Override
    public void saveOrupdate(Items items) {

        if (items.getId() != null){
            itemsMapper.update(items);
        }
        else{
            itemsMapper.save(items);
        }

    }

    @Override
    public void delete(int id) {
        itemsMapper.delete(id);
    }

    @Override
    public Items findById(int id) {
        return itemsMapper.findById(id);
    }
}

h:配置SqlMappingConfig.xml(mybatis)
<?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">
<configuration>
<!--    数据库文件配置-->
<!--    <properties resource="db.properties"/>-->
<!--    <settings>-->
<!--        &lt;!&ndash; 打印查询语句 &ndash;&gt;-->
<!--        <setting name="logImpl" value="STDOUT_LOGGING" />-->
<!--        &lt;!&ndash;        懒加载&ndash;&gt;-->
<!--        <setting name="lazyLoadingEnabled" value="true"/>-->
<!--        &lt;!&ndash;        二级缓存&ndash;&gt;-->
<!--        <setting name="cacheEnabled" value="true"/>-->
<!--    </settings>-->

    <!--别名配置-->
    <typeAliases>
        <package name="main.yzc.backOffice.model"/>
    </typeAliases>

    <!--告诉mybatis加载映射文件-->
    <mappers>
        <!--第一种写映射文件的名字-->
<!--        <mapper resource="main/yzc/backOffice/mapper/UserMapper.xml"></mapper>-->

        <package name="main.yzc.backOffice.mapper"/>
    </mappers>
</configuration>
i:创建spring的applicaiontContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
j:配置dbcp数据源和mybatis的会话工厂
  <!-- 1.加载db配置文件  -->
    <context:property-placeholder location="classpath:db.properties"/>
    
 <!-- 1.配置数据库,dbcp数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
		<property name="password" value="${password}"/>
        <!-- 最大连接  -->
        <property name="maxActive" value="10"/>
        <!--最大空闲数  -->
        <property name="maxIdle" value="5"/>
    </bean>
  
    <!--配置会话工厂-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis.xml"/>
    </bean>

    <!--使用工厂Bean生成userMapper对象-->
<!--    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
<!--        <property name="mapperInterface" value="main.yzc.backOffice.mapper.UserMapper"></property>-->
<!--        <property name="sqlSessionFactory" ref="sessionFactory"></property>-->
<!--    </bean>-->
    
<!--    第三种 批量创建mapper包的bean对象 内部会扫描指定包下的mapper,创建代理对象,名字就是类型,头字母小写-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="main.yzc.backOffice.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
    </bean>
db.properties.xml

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/spring_day02?useUnicode=true&characterEncoding=utf8
user=root
password=123456

k:Web.xml配置spring容器
    <!-- spring的配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
l:在applicationContext.xml添加bean的注解装配
   <!--自动扫描注解-->
    <context:component-scan base-package="main.yzc.backOffice"/>
m:ItemsController
o:applicationContext事务配置
    <!-- 5.配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 6.开启事务注解-->
    <tx:annotation-driven></tx:annotation-driven>
</beans>
p:添加一个保存方法测试事务

测试方法:代码中加入 int a = 10/0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值