Spring+Spring+Mybits三大框架集成

1.创建项目

在这里插入图片描述

2.导入相应的jar包,由于我不是用的maven项目,如果是mavn项目百度搜索maven在pom.xml中引入相应的jar包即可

在这里插入图片描述

3.搭建基本架构

在这里插入图片描述

4.创建applicationContext.xml ->spring配置文件以及mybatis配置

以下操作都是在applicationContext.xml中完成!!!
1.约束设置头部
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
2.创建与读取jdbc.properties(与自己的数据库连接相对于)
在这里插入图片描述

<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

3.配置DataSrource

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

4.配置mybatis的sqlsessionfactory

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--扫描别名的包-->
        <property name="typeAliasesPackage" value="cn.itsource.ssm.domain"></property>
        <!--xml的映射-->
        <property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*.xml"></property>
    </bean>

5.配置mapper对象,一劳永奕 [扫描到的接口都进行实现–>]

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.itsource.ssm.mapper"></property>
    </bean>

6.配置事物管理器,增删改需要提交事务,以及支持事务的注解

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

    <!--支持事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager" />

5.创建applicationContext-mvc.xml->springMvc配置

1.设置头部约束

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
	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.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.扫描controller层-->
    <context:component-scan base-package="cn.itsource.ssm.controller"></context:component-scan>

    <!--2.静态资源放行-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

    <!--3.Spring注解支持-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--4.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

6.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
           version="4.0">

    <!--3.监听器 启动Spring-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--2.读取Spring的配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--1.核心控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--读取SpringMvc的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-mvc.xml</param-value>
        </init-param>
        <!--让Spring和服务器一起运行而运行-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <!-- 符合RESTful风格-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--4.处理中文乱码-->
    <filter>  
        <filter-name>characterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
        <filter-mapping>  
            <filter-name>characterEncodingFilter</filter-name>  
            <url-pattern>/*</url-pattern>  
        </filter-mapping>  
</web-app>

7.集成测试

1.doman层创建实体类…省略
2.mapper(dao)层创建接口(增删改查方法)以及mybatis所需要的xml
实体类名+Mapper.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="cn.itsource.ssm.mapper.EmployeeMapper">

    <insert id="save" parameterType="employee">
        insert  into employee(name,age,sex) values(#{name},#{age},#{sex})
    </insert>

    <select id="findAll" resultType="employee">
        select * from  employee
    </select>

    <delete id="delete" parameterType="long">
        delete from employee where id=#{id}
    </delete>

    <update id="update" parameterType="employee">
        update set employee name=#{name},age=#{age},sex=#{sex} where id=#{id}
    </update>
</mapper>

3.service层接口(与mapoer层一样的方法)+实现类(需加上事务注解)
实现类

package cn.itsource.ssm.service.impl;

import cn.itsource.ssm.domain.Employee;
import cn.itsource.ssm.mapper.EmployeeMapper;
import cn.itsource.ssm.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper mapper;

    @Override
    public void save(Employee employee) {
        mapper.save(employee);
    }

    @Override
    //查询不需要事务加上注解
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public List<Employee> findAll() {
        return mapper.findAll();
    }

    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public void update(Employee employee) {
        mapper.update(employee);
    }
}

4.controller层

package cn.itsource.ssm.controller;

import cn.itsource.ssm.domain.Employee;
import cn.itsource.ssm.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/list")
    @ResponseBody
    private List<Employee> list(){
        List<Employee> list = employeeService.findAll();
        return list;
    }

}

配置完以上步骤,启动服务器 输入相应的controller的路径即可测试成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值