SSM整合(自用)

SSM框架整合思路

我们首先准备6个xml文件,分别是applicationContext-dao.xml、applicationContext-service.xml、applicationContext-tx.xml、applicationContext.xml、springmvc.xml,web.xml
下面粗略的介绍下各个xml文件要配置哪些东西:

1. applicationContext-dao.xml
b. 配置数据库连接池
c. 配置SqlSessionFactoryBean并注入连接池和扫描别名
d. 配置扫描mapper接口

2. applicationContext-service.xml
a. 扫描service层

3. applicationContext-tx.xml
a. 配置事务管理器
b. 配置事务的通知,事务的属性,要以什么方法开头、隔离级别、传播行为、是否只读等
c. 织入。将通知和被代理对象结合创建代理的对象的整合过程
d. 切入点。需要拦截哪些方法
e. 切面。切入点和通知的结合
4. applicationContext.xml
a. 将上面三个文件导入到这个主文件中

5. 编写springmvc.xml
a. 扫描controller层
b. 配置处理器映射器和处理器适配器(开启注解驱动)
c. 配置视图解析器并配置前缀和后缀
d. 配置不拦截静态资源

6. web.xml
a. 配置前端控制器 +初始化参数指定springmvc.xml的位置
b. 配置监听器+全局初始化参数指定applicationContext.xml的位置

注意:在SSM框架整合的时候,实际上有两个容器,一个是springmvc中的前端控制器,它用来加载springmvc.xml,实际上是子容器;还有一个是配置监听器和全局化参数来加载applicationContext.xml,这里是父容器。

现在我们正式开始进行SSM框架的整合:

  1. 我们首先创建一个web项目
    在这里插入图片描述
  2. 在WEB-INF下创建lib文件夹存放jar包。主要是spring framework中的一些jar包,spring整合mybatis的jar包。
    在这里插入图片描述
  3. 创建jdbc.properties
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db2024
jdbc.username=root
jdbc.password=admin
jdbc.maxActive=13
jdbc.minIdle=9
  1. 创建applicationContext-dao.xml
<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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!--1.导入外部properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--2.配置数据库连接池-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <property name="minIdle" value="${jdbc.minIdle}"/>
    </bean>
    <!--3.配置sqlSessionFactoryBean -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置别名-->
        <property name="typeAliasesPackage" value="com.gxa.pojo"/>
    </bean>
    <!-- 配置扫描mapper接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.gxa.mapper"/>
    </bean>
</beans>
  1. 创建applicationContext-service.xml
<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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <!--配置扫描service包-->
    <context:component-scan base-package="com.gxa.service"/>
</beans>
  1. 创建applicationContext-tx.xml
<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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <!--1.配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入dataSource-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--2.配置事务的通知-->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" isolation="REPEATABLE_READ" read-only="false"/>
            <tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ" read-only="false"/>
            <tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ" read-only="false"/>
            <tx:method name="find*" propagation="REQUIRED" isolation="REPEATABLE_READ" read-only="true"/>
            <tx:method name="query*" propagation="REQUIRED" isolation="REPEATABLE_READ" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!--3.配置织入-->
    <aop:config>
        <aop:advisor advice-ref="myAdvice" pointcut="execution(* com.gxa.service..*ServiceImp.*(..))"/>
    </aop:config>
</beans>
  1. 创建applicationContext.xml
<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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <import resource="applicationContext-dao.xml"/>
    <import resource="applicationContext-service.xml"/>
    <import resource="applicationContext-tx.xml"/>
</beans>
  1. 创建springmvc.xml
<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/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <!-- 1.扫描Controller-->
    <context:component-scan base-package="com.gxa.controller"/>
<!--    &lt;!&ndash;2.配置处理器映射器 可以通过配置注解驱动&ndash;&gt;-->
<!--    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
<!--    &lt;!&ndash;3.配置处理器适配器 可以通过配置注解驱动&ndash;&gt;-->
<!--    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
    <!--2.配置注解驱动-->
    <mvc:annotation-driven/>
    <!--3.配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置逻辑视图前缀-->
        <property name="prefix" value="/WEB-INF/views/"/>
        <!--配置逻辑视图后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--4.配置不拦截静态资源-->
    <mvc:resources mapping="/layui/**" location="/layui/"/>
</beans>
  1. 在web.xml进行配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--1.配置前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置初始化参数来指定springmvc.xml的位置-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <!--配置servlet在启动项目的时候创建-->
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--缺省路径首先访问匹配路径的servlet,如果没有匹配就走缺省路径的servlet-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--2.配置监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--3.全局初始化参数来指定applicationContext.xml的位置  父容器-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>
</web-app>
  1. 准备pojo进行测试
public class Item {

    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;

    public Integer getId() {
        return id;
    }

    public Item() {
    }

    public Item(Integer id, String name, Float price, String pic, Date createtime, String detail) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.pic = pic;
        this.createtime = createtime;
        this.detail = detail;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    @Override
    public String toString() {
        return "Item{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", pic='" + pic + '\'' +
                ", createtime=" + createtime +
                ", detail='" + detail + '\'' +
                '}';
    }
}
  1. 准备mapper
public interface ItemMapper {
    @Select("select id,name,price,pic,createtime,detail from item where id=#{id}")
    Item findItemById(Integer id);
    @Select("select id,name,price,pic,createtime,detail from item")
    List<Item> findItemAll();
    @Insert("insert into item(name,price,pic,createtime,detail) values(#{name},#{price},#{pic},#{createtime},#{detail})")
    Integer insertItem(Item item);
    @Update("update item set name=#{name},price=#{price},pic=#{pic},detail=#{detail} where id=#{id}")
    Integer update(Item item);
    @Delete("delete from item where id=#{id}")
    Integer deleteById(Integer id);
}

12.进行测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
public class TestItemMapper {
    @Autowired
    private ItemMapper itemMapper;
    @Test
    public void testInsert(){
        Item item=new Item(null,"苹果meate90",9000F,"xxx",new Date(),"xxx");
        itemMapper.insertItem(item);
    }
    @Test
    public void testUpdate(){
        Item item=new Item(1,"大哥大",9000F,"xxx",new Date(),"xxx");
        itemMapper.update(item);
    }
    @Test
    public void testFind(){
        List<Item> itemAll = itemMapper.findItemAll();
        System.out.println(itemAll);
        Item item = itemMapper.findItemById(1);
        System.out.println(item);
    }

    @Test
    public void testDelete(){
        itemMapper.deleteById(8);
    }
}

13.编写service,进行整合测试
接口

public interface ItemService {
    List<Item> findItemAll();
}

实现类

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Override
    public List<Item> findItemAll() {
        return itemMapper.findItemAll();
    }
}

14.编写controller

@Controller
public class ItemController {
    @Autowired
    private ItemService itemService;

    @RequestMapping("/queryItemList")
    public ModelAndView queryItemList(){
        List<Item> list = itemService.findItemAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("list",list);
        modelAndView.setViewName("itemList");
        return modelAndView;
    }
}

15.最后访问页面http://localhost:8080/ssm/queryItemList
在这里插入图片描述

本文章作为自己学习记录,如有错误欢迎指正哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值