zoukankan      html  css  js  c++  java
  • ssm项目快速搭建(注解)

    dao层配置

    dao层配置注意事项:

      1、Mapper.xml 文件中的 namespace 与 mapper 接口的类路径相同

      2、Mapper.xml 接口方法名和 Mapper.xml 中定义的每个 statement 的id相同

      3、Mapper 接口方法的输入参数类型和 mapper.xml 中定义的每个sql的 paramenterType的类型相同

      4、Mapper 接口方法的输出参数类型和 mapper.xml 中顶一个的每个sql的 resultType 的类型相同

    1. SqlMapConfig.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    <plugins>
    <!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageHelper">
    <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库-->
    <property name="dialect" value="mysql"/>
    </plugin>
    </plugins>
    </configuration>
    2. db.properties
      连接mysql数据库所需要的信息
        jdbc.driver=com.mysql.jdbc.Driver
        jdbc.url=jdbc:mysql://localhost:3306/pinyougoudb?characterEncoding=utf-8
        jdbc.username=root
        jdbc.password=pig
    3. applicationContext-dao.xml
      <!-- 数据库连接池 -->
       <!-- 加载配置文件 -->
       <context:property-placeholder location="classpath:properties/db.properties"/>
       <!-- 数据库连接池 -->
       <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
             destroy-method="close">
           <property name="url" value="jdbc:mysql://localhost:3306/taobao?characterEncoding=utf-8"/>
           <property name="username" value="root"/>
           <property name="password" value="pig"/>
           <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
           <property name="maxActive" value="10"/>
           <property name="minIdle" value="5"/>
       </bean>

       <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
           <!-- 数据库连接池 -->
           <property name="dataSource" ref="dataSource"/>
           <!-- 加载mybatis的全局配置文件 -->
           <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
       </bean>
       <!--配置mapper映射路径-->
       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
           <property name="basePackage" value="com.taobao.mapper"/>
       </bean>

     


     

    service层配置

    注意事项: 1、使用注解配置bean注入到ioc容器。一定要记得添加组件扫描,否则会报出无法创建service bean的错误 2、事务控制注解使用@Transactional注解配置,可以作用在类和方法上


      <?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:context="http://www.springframework.org/schema/context"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          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/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

       <!--组件扫描-->
       <context:component-scan base-package="com.taobao.service"/>
       <!-- 事务管理器 -->
       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
           <property name="dataSource" ref="dataSource" />
       </bean>

       <!-- 开启事务控制的注解支持 -->
       <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>


     

    表现层配置

    注意事项: 1、web.xml中需要配置监听器加载spring配置文件,配置解决post乱码,配置springmvc前端控制器DispartcherServlet 2、springmvc配置文件中记得添加组建扫描,否则会报出无法创建bean错误

        applicationContext.xml中导入dao和service的配置
       <!--加载service中spring配置文件-->
       <import resource="classpath*:spring/applicationContext-dao.xml"/>


       <!--加载service中spring配置文件-->
       <import resource="classpath*:spring/applicationContext-tx.xml"/>

    springmvc.xml中配置下面一句就行。
    1. controller使用@ResponseBody声明在方法上,或者直接在类上声明@RestController(相当于@Controller和@ResponseBody的结合)
    2. 前端给后台的数据是springmvc自动封装到参数中的。如果需要json数据,可以声明@RequestBody。后台给前端的数据是JSON格式

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xmlns:context="http://www.springframework.org/schema/context"
          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/mvc
          http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd">

       <!--组件扫描-->
       <context:component-scan base-package="com.taobao.controller"/>

       <!--配置视图解析器-->
       <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="prefix" value="/pages/"/>
           <property name="suffix" value=".html"/>
       </bean>


       <!--释放静态资源-->
       <mvc:default-servlet-handler/>

       <!--处理器映射器、处理器适配器-->
       <!---配置JSON解析器-->
       <mvc:annotation-driven>
     <mvc:message-converters register-defaults="true">
       <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
         <property name="supportedMediaTypes" value="application/json"/>
         <property name="features">
           <array>
             <value>WriteMapNullValue</value>
             <value>WriteDateUseDateFormat</value>
           </array>
         </property>
       </bean>
     </mvc:message-converters>
    </mvc:annotation-driven>

    </beans>
    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"
            version="2.5">

       <!--spring security过滤器-->
       <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:spring/spring-security.xml</param-value>
       </context-param>
       <listener>
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>
       <filter>
           <filter-name>springSecurityFilterChain</filter-name>
           <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
       </filter>
       <filter-mapping>
           <filter-name>springSecurityFilterChain</filter-name>
           <url-pattern>/*</url-pattern>
       </filter-mapping>


         <!-- 加载spring容器 -->
       <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath*:spring/applicationContext*.xml</param-value>
       </context-param>
       <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>

       <!-- 解决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>
           <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>

     <!---配置前端控制器加载springMVC核心配置文件-->
       <servlet>
           <servlet-name>springmvc</servlet-name>
           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
           <init-param>
               <param-name>contextConfigLocation</param-name>
               <param-value>classpath:spring/springmvc.xml</param-value>
           </init-param>
       </servlet>

       <servlet-mapping>
           <servlet-name>springmvc</servlet-name>
           <url-pattern>*.do</url-pattern>
       </servlet-mapping>

    </web-app>
     
    注解说明:

    @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法。通俗来说,被Controller标记的类就是一个控制器,这个类中的方法,就是相应的动作。
    @RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。比如图一中,跳转到登录页面的路径就是localhost:8080/xxx-war/user/toLogin
    service采用@service注解


    例:@Service("userService")注解是告诉Spring,当Spring要创建UserServiceImpl的的实例时,bean的名字必须叫做"userService",这样当Action需要使用UserServiceImpl的的实例时,就可以由Spring创建好的"userService",然后注入给Action。
    dao层使用@repository注解

    @Controller
    @ResponseBody 添加此注解表示返回类型是JSON格式
    @RestController 相当于@Controller@ResponseBody的结合
    1@RequestBody 作用于方法的参数上,传入的参数类型必须是String。因为传入的数据类型是JSON字符差串数据

    2、如果作用了@ResponseBody注解或者直接在类上使用了@RestController注解则返回类型是JSON格式。(不走视图解析器)
    如果没有注明返回的是JSON数据,则返回数据会走视图解析器


    java中的注解大全@controller@service@repository
    引用前辈的连接(https://www.cnblogs.com/CrisZjie180228/p/8745603.html)

     

     

  • 相关阅读:
    递增一个指针
    ubuntu 系统 sudo apt-get update遇到问题sub-process returned an error code
    熟悉HDFS过程中遇到的问题
    大二暑假第八周进度报告
    大二暑假第七周进度报告
    oracle“ORA-00904”错误:标识符无效
    大学暑假第六周进度报告
    大二暑假第五周进度报告
    使用Navicat for Oracle新建表空间、用户及权限赋予
    大学暑假第四周进度报告
  • 原文地址:https://www.cnblogs.com/xuan-cz/p/10138449.html
Copyright © 2011-2022 走看看