spring,springmvc与mybatis整合

先从持久层出发

一.DAO层(新建一个source folder:config,在其下新建一个文件夹:spring,其下的配置文件如图):

  1. SqlMapConfig.xml,空文件即可。需要文件头。
  2. applicationContext-**.xml里边开头的统一配置:

 

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 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-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/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  1. applicationContext-dao.xml。
    1. 加载配置文件,数据库连接池

 

 <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
 
     <!-- 数据库连接池 -->
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
         destroy-method="close">
         <property name="driverClassName" value="${jdbc.driver}" />
         <property name="url" value="${jdbc.url}" />
         <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
         <!-- 连接池的最大数据库连接数 -->
         <property name="maxActive" value="10" />
         <!-- 最大空闲数 -->
         <property name="maxIdle" value="5" />
     </bean>
    1. SqlSessionFactory对象,需要spring和mybatis整合包下的(配置mybatis核心配置文件,别名包扫描扫描pojo包)。

 

 <!-- sqlsessionfactory的配置 -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 加载mybatis核心配置文件 -->
       <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
        <!-- 别名包扫描 -->
        <property name="typeAliasesPackage" value="com.itheima.springmvc.pojo"></property>
     </bean>
    1. 配置mapper文件扫描器。

 

 <!-- 动态代理Dao开发,第一种方式,包扫描器(推荐使用) -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
               <!-- basePackage:配置映射包装扫描,多个包时用","或";"分隔 -->
    <property name="basePackage" value="com.itheima.springmvc.mapper" />
    </bean>

二service层:

a.applicationContext-service.xml包扫描器,扫描@service注解的类。

 

<!-- @service包扫描器 -->
   <context:component-scan base-package="com.itheima.springmvc.service"/>


 b.applicationContext-trans.xml配置事务。

1)事务管理器

 

 <!-- 事务管理器 -->
     <bean id="transactionManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <!-- 数据源 -->
         <property name="dataSource" ref="dataSource" />
     </bean>

2).通知:

 

 <!-- 通知 -->
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
         <tx:attributes>
             <!-- 传播行为 -->
             <tx:method name="save*" propagation="REQUIRED" />
             <tx:method name="insert*" propagation="REQUIRED" />
             <tx:method name="delete*" propagation="REQUIRED" />
             <tx:method name="update*" propagation="REQUIRED" />
             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
             <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
             <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
         </tx:attributes>
     </tx:advice>

3).配置切面(有了该切面,只要给了该service的注解,就可利用该service类下的所有属性和方法)

 

 <!-- 切面 -->
    <aop:config>
         <aop:advisor advice-ref="txAdvice"
             pointcut="execution(* com.itheima.springmvc.service.*.*(..))" />
     </aop:config>

三controller层(springmvc.xml)

a.包扫描器,扫描@Controller注解的类。

 

 <!-- 配置@Controller处理器,包扫描器 -->
     <context:component-scan base-package="com.itheima.springmvc.controller" />

b.配置注解驱动(注解驱动配置,代替映射器与适配器的单独配置,同时支持json响应(推荐使用) )。

 

 <!-- 注解驱动配置,代替映射器与适配器的单独配置,同时支持json响应(推荐使用) -->
    <mvc:annotation-driven />

c.视图解析器:

 

 <!-- 配置视图解析器 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <!-- 配置视图响应的前缀 -->
         <property name="prefix" value="/WEB-INF/jsp/" />
         <!-- 配置视图响应的后缀 -->
        <property name="suffix" value=".jsp" />
     </bean>

web.xml编写:

在里边添加如下:

 

 <!-- 配置spring -->
     <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:spring/applicationContext-*.xml</param-value>
     </context-param>

     <!-- 配置监听器加载spring -->
     <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
 
     <!-- 配置过滤器,解决post的乱码问题 -->
     <filter>
         <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>encoding</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
 
     <!-- 配置SpringMVC -->
     <servlet>
         <servlet-name>boot-crm</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:spring/springmvc.xml</param-value>
         </init-param>
         <!-- 配置springmvc什么时候启动,参数必须为整数 -->
         <!-- 如果为0或者大于0,则springMVC随着容器启动而启动 -->
         <!-- 如果小于0,则在第一次请求进来的时候启动 -->
         <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
         <servlet-name>boot-crm</servlet-name>
         <!-- 所有的请求都进入springMVC
            /*代表拦截所有的请求包括jsp,/代表拦截所有的请求排除jsp
          -->
         <url-pattern>*.action</url-pattern>
     </servlet-mapping>

四.然后分别建立controller包,mapper映射包(里面包含SQL语句的id定义的xml文件,以及SQL语句操作方法的定义的java文件),pojo包用于存放对象,service包(用于定义操作数据库的方法(利用mapper里定义的sql语句)),service.impl的service实现类的包(用于实现service包下的方法)以及utils工具包

注意事项:

1.controller类开头要利用@controller进行标注,例如:

@Controller
public class ItemController {  }

2.类里面要利用@autowired对service进行自动配注,例如:

@Autowired
    private ItemService itemservice;

3.方法的前面要加上拦截器注解@RequestMapping("itemList")来绑定地址,例如:

@RequestMapping("itemList")
    public ModelAndView itemList() { }

4.service实现类开头要加上@service来标注是service,例如:

@Service
public class ItemServiceimpl implements ItemService {  }

5.类里面要加上mapper注解@Autowired,例如:

@Autowired
    private ItemMapper itemMapper;
加上注解是为了方便直接使用其中的属性和方法。

猜你喜欢

转载自blog.csdn.net/qq_37706228/article/details/81363254