ssm項目快速搭建(註解)

dao層配置

dao層配置注意事項:html

  一、Mapper.xml 文件中的 namespace 與 mapper 接口的類路徑相同前端

  二、Mapper.xml 接口方法名和 Mapper.xml 中定義的每一個 statement 的id相同java

  三、Mapper 接口方法的輸入參數類型和 mapper.xml 中定義的每一個sql的 paramenterType的類型相同mysql

  四、Mapper 接口方法的輸出參數類型和 mapper.xml 中頂一個的每一個sql的 resultType 的類型相同git

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層配置

注意事項: 一、使用註解配置bean注入到ioc容器。必定要記得添加組件掃描,不然會報出沒法建立service bean的錯誤 二、事務控制註解使用@Transactional註解配置,能夠做用在類和方法上github


  <?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>


 

表現層配置

注意事項: 一、web.xml中須要配置監聽器加載spring配置文件,配置解決post亂碼,配置springmvc前端控制器DispartcherServlet 二、springmvc配置文件中記得添加組建掃描,不然會報出沒法建立bean錯誤web

    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)
相關文章
相關標籤/搜索