taotao第二天:ssm整合+dubbo初步

来源:互联网 发布:吃鸡手游 知乎 编辑:程序博客网 时间:2024/06/04 01:25

1 导入数据库文件,生成数据库表如下


2 使用mybatis官方提供的逆向工程工具generateSqlMapperCustom配置生成实体类pojo和mapper映射文件和java文件


3 因为taotao-manager为提供服务的聚合工程,内部含有taotao-manager-dao,taotao-manager-pojo,taotao-manager-service,taotao-manager-interface四大模块,除过taotao-manager-service打成war包,其余最终都会打成jar包放在taotao-manager-service打成的war包的WEB-INF目录下的lib目录中,因此mybatis的配置文件和spring的配置文件均配置在taotao-manager-service工程的src/main/resources目录下

首先配置mybatis的全局配置文件:在classpath路径下创建mybatis文件夹,以及SqlMapperConfig.xml



然后同理在classpath下创建spring文件夹,以及spring整合dao层的配置文件applicationContext-dao.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.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- spring和mybatis整合 --><!-- 加载properties配置文件 --><context:property-placeholder location="classpath:properties/*.properties"/><!-- 配置数据库数据源Druid --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">    <!-- 基本属性 url、user、password -->      <property name="url" value="${jdbc.url}" />      <property name="username" value="${jdbc.user}" />      <property name="password" value="${jdbc.password}" />      <!-- 配置初始化大小、最小、最大 -->      <property name="initialSize" value="5" />      <property name="minIdle" value="5" />       <property name="maxActive" value="20" />      <!-- 配置获取连接等待超时的时间 -->      <property name="maxWait" value="60000" />      <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->      <property name="timeBetweenEvictionRunsMillis" value="60000" />      <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->      <property name="minEvictableIdleTimeMillis" value="300000" />      <property name="validationQuery" value="SELECT 'x'" />      <property name="testWhileIdle" value="true" />      <property name="testOnBorrow" value="false" />      <property name="testOnReturn" value="false" />      <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->      <property name="poolPreparedStatements" value="true" />      <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />      <!-- 配置监控统计拦截的filters -->      <property name="filters" value="stat" /> </bean><!-- 配置spring管理sqlSessionFactory,采用spring和mybatis整合包中的类 --><bean id="sqlSessionFatory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><!-- 配置mybatis全局配置文件位置 --><property name="configLocation" value="classpath:mybatis/SqlMapperConfig.xml"></property></bean><!-- 配置对mapper文件的扫描 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.taotao.mapper"></property></bean></beans>


创建spring整合service层的配置文件applicationContext-service如下所示,同时在taotao-manager-interface和taotao-manager-service目录下分别建立包com.taotao.service和com.taotao.service.impl,
<?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.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 配置扫描加载service类创建对象加入spring容器 --><context:component-scan base-package="com.taotao.service"></context:component-scan></beans>

创建spring事务配置文件applicationContext-tx.xml,配置如下

<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.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 配置spring事务管理器 --><bean id="transationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 数据源 --><property name="dataSource" ref="dataSource"></property></bean><!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 配置以哪些开头的方法需要事务 --><tx:method name="add*" propagation="REQUIRED"/><tx:method name="insert*" propagation="REQUIRED"/><tx:method name="create*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><!-- 只读事务提示数据库驱动程序和数据库系统,这个事务并不包含更改数据的操作,那么JDBC驱动程序和数据库就有可能根据这种情况对该事务进行一些特定的优化,比方说不安排相应的数据库锁--><tx:method name="find*" propagation="SUPPORTS" read-only="true"/><tx:method name="select*" propagation="SUPPORTS" read-only="true"/><tx:method name="get*" propagation="SUPPORTS" read-only="true"/></tx:attributes></tx:advice><!-- 配置aop切面 --><!-- 任意返回值的com.taotao.service下面的任意类的带任意参数的任意方法 --><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.taotao.service.*.*(..))" /></aop:config></beans>
配置taotao-manager-service的web.xml,配置父spring容器,为contextLoadListener监听加载
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>taotao-manager-service</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>    <!-- 配置加载spring父容器 -->  <context-param>  <param-name>contextCofigLocation</param-name>  <param-value>classpath:spring/applicationContext-*.xml</param-value>  </context-param>    <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  </web-app>
这里之所以说是父容器,是因为是contextLoadListener加载的,dispatcherServlet运行的时候也会加载一个spring容器,这是spring子容器,下面为父子容器的关系


子容器可以访问父容器的对象即controller可以依赖注入service使用,而service不可以依赖注入controller进行使用,父容器中的service是有事务的,即便子容器中配了如果未声明,则使用的是子容器中并未有事务的service

4 配置taotao-web表现层的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:p="http://www.springframework.org/schema/p"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.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!-- 配置扫描controller类实例化 --><context:component-scan base-package="com.taotao.controller"></context:component-scan><!-- 配置springmvc注解驱动,替代配置处理器映射器和处理器适配器 --><mvc:annotation-driven></mvc:annotation-driven><!-- 配置视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"></property><property name="suffix" value=".jsp"></property></bean></beans>

配置文件,首先在resources下创建spring文件夹,然后创建springmvc.xml文件,内容如下

<?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:p="http://www.springframework.org/schema/p"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.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!-- 配置扫描controller类实例化 --><context:component-scan base-package="com.taotao.controller"></context:component-scan><!-- 配置springmvc注解驱动,替代配置处理器映射器和处理器适配器 --><mvc:annotation-driven></mvc:annotation-driven><!-- 配置视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"></property><property name="suffix" value=".jsp"></property></bean></beans>
在main/java/下创建com.taotao.controller包

配置taotao-web的web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>taotao-web</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <!-- 配置前端控制器 -->  <servlet>  <servlet-name>taotao-web</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>  <!-- 配置这个即在tomcat容器启动的时候就加载spring子容器不配置的话第一请求的时候才加载 -->  <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>taotao-web</servlet-name>  <url-pattern>/</url-pattern>  </servlet-mapping>    <!-- 配置post请求乱码过滤 -->  <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></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>  </web-app>



原创粉丝点击