实现了ssm框架整合的个人心得

第一步:
pom.xml文件:这里面涉及到整合项目需要使用到的所有的jar和相关的依赖,我们在使用的时候需要引入正确的jar,否则很容易出现错误。

第二步:
创建对应的实体类、DAO接口、mapper映射文件、mybatis-config.xml文件:
其中mybatis-config.xml文件中的包扫描我们可以放在spring-dao.xml(spring和mybatis文件中的整合文件)中去。mybatis-config.xml文件中还可以设置其他的属性。

第三步:
配置spring-dao.xml文件:实现对spring和mybatis的整合:
①使用jdbc.properties文件实现连接数据库。


    <context:property-placeholder location="classpath:jdbc.properties" />
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        
        <property name="driverClass" value="${driver}">property>
        <property name="jdbcUrl" value="${url}">property>
        <property name="user" value="${jdbc.username}">property>
        <property name="password" value="${password}">property>
        
        <property name="maxPoolSize" value="30">property>
        <property name="minPoolSize" value="10">property>
        <property name="autoCommitOnClose" value="false">property>
        <property name="checkoutTimeout" value="20000">property>
        
        <property name="acquireRetryAttempts" value="2">property>
    bean>

②sqlSessionFactory的配置,实现的实体类的映射文件的扫描

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        
        <property name="configLocation" value="classpath:mybatis-config.xml">property>
        
        <property name="typeAliasesPackage" value="org.seckill.entity">property>
        
        <property name="mapperLocations" value="classpath:mapper/*.xml">property>
    bean>

③配置扫描Dao接口包,注入到spring中:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
        
        <property name="basePackage" value="org.seckill.dao">property>
    bean>

第四步:
对service接口以及实现类的使用和配置:
①使用:使用@Service标识类,使用@AutoWired实现注入
②配置:

    
    <context:component-scan base-package="org.seckill.service">

    context:component-scan>

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="dataSource">property>
    bean>

    
    <tx:annotation-driven transaction-manager="transactionManager"/>

第五步:
在spring-web.xml中实现springMVC的配置和使用:
①配置:


    <mvc:annotation-driven>mvc:annotation-driven>
    <mvc:default-servlet-handler />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView">property>
        <property name="prefix" value="/WEB-INF/jsp/">property>
        <property name="suffix" value=".jsp">property>
    bean>
    //扫描哪个包
    <context:component-scan base-package="org.seckill.web">context:component-scan>

②使用:
使用@Controller注解标示类,使用@Autowired注入,使用@RequestMapping来表示输入的地址栏:
这里的name为list表示输入http://localhost:8080/项目名/list时调用此方法,

@Controller
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    @RequestMapping(name = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        List list = seckillService.getSeckillList();
        System.out.println(list.toString());
        model.addAttribute("list", list);
        return "list";
    }
}

第六步:
在web.xml中加入DispatcherServlet来处理请求,以及引入spring-dao.xml、spring-service.xml、spring-web.xml文件实现加载注入

<servlet>
        <servlet-name>seckill-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            //这里很重要,必须全部引入
            <param-value>classpath:spring/spring-*.xml</param-value>
        </init-param>

    </servlet>

    <servlet-mapping>
        <servlet-name>seckill-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

你可能感兴趣的:(个人心得)