Spring+SpringMVC+Mybatis+Maven搭建項目
1.前言
這篇文章一步步總結了搭建一個基於Maven的Spring+SpringMVC+Mybatis框架的完整過程,同時加入了基礎工具類,包括讀取配置文件,監聽器和定時器等。文件可以免積分下載:http://download.csdn.net/download/jintao_ma/9661038
2.建立一個web項目
2.1 maven創建web項目
顯然,由於SpringMVC的存在,表明這篇講述的是一個Web項目,使用maven的maven-archetype-webapp這個模板,搭建一個項目,在命令行下執行:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=myWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
然後新建的myWebApp項目中,只有java/main目錄,我們新建下面三個目錄纔是一個完整的maven web項目:
java/resources
test/main
test/resources
接着導入eclipse,file->import:
2.2 設置web項目的版本和jdk版本
新導入的項目的web版本默認是2.3,jdk版本是1.5;我們需要重新設置一下,設置爲最新的;
修改項目目錄.settings下的org.eclipse.wst.common.project.facet.core.xml,修改後如下:
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.8"/>
<installed facet="jst.web" version="3.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
同時修改項目中的web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
改爲
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
</web-app>
2.3 maven設置
2.3.1 項目run as->maven clean報錯:
-Dmaven.multiModuleProjectDirectory system property is not set
可以設一個環境變量M2_HOME指向你的maven安裝目錄M2_HOME=D:\Apps\apache-maven-3.3.1,然後在Window->Preference->Java->Installed JREs->Edit,在Default VM arguments中設置-Dmaven.multiModuleProjectDirectory=$M2_HOME
2.3.2 maven update project
經過前面的幾項設置,項目的web版本已經是3.0,jdk也是1.8了,也可以進行maven clean和install;但是每次在進行maven->project時,項目還會變爲 web 2.3和jdk 1.5
我們還需要在pom中增加下面的配置:
<build>
<finalName>myWebApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
然後就可以maven-clean,maven-install;然後maven update project;就沒有任何的錯誤和警告了。
3.加入Spring,SpringMVC和Mybatis
3.1 spring.xml
在介紹配置文件之前,先在pom.xml中引入相關的jar包,下面把這篇所有需要的依賴(包括後面所用到的定時器jar包)全部貼上來,pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>myWebApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>myWebApp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>[4.3.0.RELEASE,)</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>[4.10,)</version>
<scope>test</scope>
</dependency>
<!-- 添加數據庫驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>[5.1.39,)</version>
</dependency>
<!-- 添加Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>[3.4.0,)</version>
</dependency>
<!-- 添加mybatis-spring的依賴 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>[1.3.0,)</version>
</dependency>
<!-- 添加Spring的依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>[1.0,)</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>[3.2.4,)</version>
</dependency>
<!-- 引入jstl標籤庫 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 引入jstl標籤庫 -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- 配置log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>[1.2.17,)</version>
</dependency>
<!-- 配置slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>[1.7.21,)</version>
</dependency>
<!-- 配置slf4j-log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>[1.7.21,)</version>
</dependency>
<!-- 配置commons-io-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>[2.5,)</version>
</dependency>
<!--配置quartz定時器 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>[2.2.3,)</version>
</dependency>
<!--配置quartz所依賴的jar -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>[3.2.1,)</version>
</dependency>
<!-- 配置commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>[1.2,)</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>[1.3.2,)</version>
</dependency>
</dependencies>
<build>
<finalName>myWebApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
<version>[1,1)</version>這樣的寫法意思是獲取大於等於1.1版本的最新的jar包,這樣以後就不用手動升級了,每次maven->update project就可以了。
接着在配置spring.xml之前,建議先看上一篇文章的總結:http://blog.csdn.net/Jintao_Ma/article/details/52853865,spring.xml的作用就是把spring和mybatis進行集成,這篇文章的spring配置和上一篇稍有不同,首先掃描的包發生了變化
<!-- 掃描註解,Spring的配置文件不掃描@Controller註解,因爲controller層由springMvc配置文件掃描解析 -->
<context:component-scan base-package="com.mycompany">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
這裏面去掉了controller層的掃描,controller層由spring mvc來進行掃描;然後是aop配置中增加了“..”
<!-- 添加規則事務 -->
<aop:config proxy-target-class="true">
<!-- 第一個*代表所有的返回值類型 -->
<!-- 第一個..代表中間隔着任意多個包層次 -->
<!-- 第二個*代表所有的類 -->
<!-- 第三個*代表類所有方法 最後一個..代表所有的參數。 -->
<aop:pointcut expression="execution(* com.mycompany..service.*.*(..))" id="txPoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
因爲service層在不同的包中,我們希望只對service進行事務,com.mycompany..service代表com.mycompany項目所有目錄下的名爲service文件夾
3.2 web.xml
先配置spring.xml文件的讀取:
<!--contextConfigLocation在 ContextLoaderListener類中的默認值是 /WEB-INF/applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,
classpath:springQuartz.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
配置context-param,裏面讀取spring.xml(先忽略springQuartz.xml,後面提到), 然後監聽器listener在項目初始化的時候就能夠讀取spring.xml中的配置,順便說一下不同節點在web.xml中的執行順序,context-param -> listener -> filter -> servlet
再配置自己的servlet:
<servlet>
<servlet-name>myWebApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 默認加載/WEb-INF/<servletName-servlet>.xml的Spring配置文件,可以通過contextConfigLocation手動指定 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myWebApp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在分析之前,建議先看之前總結的另一篇文章:http://blog.csdn.net/Jintao_Ma/article/details/52141953,之後就可以明白上面的配置了。這時候可能會有一個疑問,這個tomcat自帶的名爲'jsp'的servlet, 與mvc中的視圖解析器有什麼關係嗎?後面進行解釋
3.3 springMvc.xml
先看一下視圖解析器的配置,然後分析它與tomcat自帶'jsp'關係
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
當我們在地址欄中輸入一個地址,首先它會去找servlet,由於與‘jsp'這個servlet不匹配,所以會進入我們自己配置的默認servlet,然後進入controller層次,接着controller返回的時候,會經過試圖解析器的解析,完成之後再由‘jsp’這個servlet來找到對應的jsp。
對資源文件的處理,這篇文章用的是<default-servlet-handler/>,之後還要配置<mvc annotation-driven/>,原因看這篇文章http://www.cnblogs.com/hujingwei/p/5349983.html,配置如下:
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
同時還可以配置<mvc:view-controller>,這樣url請求可以不經過controller層次,直接由試圖解析器進行解析,如下:
<!-- 加載系統配置文件,然後再mvc:view-controller中的path中引入 -->
<context:property-placeholder location="classpath:system.properties"/>
<mvc:view-controller path="${adminPath}" view-name="index"/>
另外需要注意一點,我們在mvc中掃描包時候,只掃描controller層的,這和spring.xml的配置正好是互補的,(或者說不要掃描service層)。 爲什麼mvc不能掃描service層呢,上一篇文章http://blog.csdn.net/Jintao_Ma/article/details/52853865提到了。
在springMvc.xml中還可以加入多文件上傳,這樣一來,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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 加載系統配置文件,然後再mvc:view-controller中的path中引入 -->
<context:property-placeholder location="classpath:system.properties"/>
<!-- 配置自動掃描的包,只掃描@controller註解的包 -->
<context:component-scan base-package="com.mycompany" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:view-controller path="${adminPath}" view-name="index"/>
<mvc:annotation-driven/>
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler/>
<!-- 支持多文件上傳 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默認編碼 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 內存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>
4.其他配置
4.1 讀取配置文件
全局配置配置文件有兩個,一個是jdbc.properties, 另一個是system.properties, 後者可以放入一下系統的配置信息:
adminPath=/main
delTempFile=E:/Majintao/temp
配置文件加載類,PropertiesLoader.java:
package com.mycompany.mvc.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
/**
* @author Jintao_Ma
* @time 2016年7月21日23:19:39
* @description:
* Properties文件載入工具類. 可載入多個properties文件, 相同的屬性在最後載入的文件中的值將會
* 覆蓋之前的值,但以System的Property優先.項目的基礎工具類,可被上層使用
*/
public class PropertiesLoader {
private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
private static ResourceLoader resourceLoader = new DefaultResourceLoader();
private final Properties properties;
public PropertiesLoader(String[] resourcesPaths) {
properties = loadProperties(resourcesPaths);
}
public Properties getProperties() {
return properties;
}
/**
* 取出Property,但以System的Property優先.
*/
private String getValue(String key) {
String systemProperty = System.getProperty(key);
if (systemProperty != null) {
return systemProperty;
}
return properties.getProperty(key);
}
/**
* 取出String類型的Property,但以System的Property優先,如果都為Null則拋出異常.
*/
public String getProperty(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return value;
}
/**
* 取出String類型的Property,但以System的Property優先.如果都為Null則返回Default值.
*/
public String getProperty(String key, String defaultValue) {
String value = getValue(key);
return value != null ? value : defaultValue;
}
/**
* 取出Integer類型的Property,但以System的Property優先.如果都為Null或內容錯誤則拋出異常.
*/
public Integer getInteger(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Integer.valueOf(value);
}
/**
* 取出Integer類型的Property,但以System的Property優先.如果都為Null則返回Default值,如果內容錯誤則拋出異常
*/
public Integer getInteger(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Integer.valueOf(value) : defaultValue;
}
/**
* 取出Double類型的Property,但以System的Property優先.如果都為Null或內容錯誤則拋出異常.
*/
public Double getDouble(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Double.valueOf(value);
}
/**
* 取出Double類型的Property,但以System的Property優先.如果都為Null則返回Default值,如果內容錯誤則拋出異常
*/
public Double getDouble(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Double.valueOf(value) : defaultValue;
}
/**
* 取出Boolean類型的Property,但以System的Property優先.如果都為Null拋出異常,如果內容不是true/false則返回false.
*/
public Boolean getBoolean(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Boolean.valueOf(value);
}
/**
* 取出Boolean類型的Property,但以System的Property優先.如果都為Null則返回Default值,如果內容不爲true/false則返回false.
*/
public Boolean getBoolean(String key, boolean defaultValue) {
String value = getValue(key);
return value != null ? Boolean.valueOf(value) : defaultValue;
}
/**
* 載入多個文件, 文件路徑使用Spring Resource格式.
*/
private Properties loadProperties(String[] resourcesPaths) {
Properties props = new Properties();
for (String location : resourcesPaths) {
InputStream is = null;
try {
Resource resource = resourceLoader.getResource(location);
is = resource.getInputStream();
props.load(is);
} catch (IOException ex) {
logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
} finally {
IOUtils.closeQuietly(is);
}
}
return props;
}
}
配置文件工具類SystemConfig.java:
package com.mycompany.mvc.config;
import java.util.Properties;
import com.mycompany.mvc.utils.PropertiesLoader;
/**
*@author Jintao_Ma
*@time 2016年7月23日14:26:12
*@description:
* 獲取配置文件信息
*/
public class SystemConfig {
private static Properties properties;
private static String[] resourcesPaths = {"classpath:system.properties"};
static{
properties = new PropertiesLoader(resourcesPaths).getProperties();
}
public static String getProperty(String key){
String systemProperty = System.getProperty(key);
if (systemProperty != null) {
return systemProperty;
}
return properties.getProperty(key);
}
}
然後就可以像SystemConfig.getProperty()的形式使用配置文件中的屬性了。
4.2 設置監聽器
監聽器可以做很多事情,比如我們可以在系統加載的時候獲取系統的絕對路徑,然後放入System中,SystemListener.java:
package com.mycompany.mvc.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SystemListener implements ServletContextListener{
private Logger logger = LoggerFactory.getLogger(SystemListener.class);
/**
* 獲取系統絕對路徑
*/
@Override
public void contextInitialized(ServletContextEvent servletContext) {
logger.debug("servletContext init start...");
System.setProperty("realPath", servletContext.getServletContext().getRealPath("/"));
logger.debug("servletContext init end...");
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
}
4.3 設置定時器
新建springQuartz.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="com.mycompany.mvc.quartz"></context:component-scan>
<!-- 任務生成器,指定其對應的對象和其中的方法 -->
<bean id="fileDeleteDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="fileDelete" />
<property name="targetMethod" value="fileDelete_delFile" />
</bean>
<!-- 觸發器生成器,指定任務和執行的時間 -->
<bean id="fileDeleteTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="fileDeleteDetail" />
<property name="cronExpression">
<value>0 0/5* * * ?</value><!-- 每隔五分鐘跑一次,刪除指定目錄下的文件 -->
</property>
</bean>
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="fileDeleteTrigger" />
</list>
</property>
</bean>
</beans>
接着再web.xml中讀取,這就是前面提到的:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,
classpath:springQuartz.xml
</param-value>
</context-param>
然後定時器就設置好了,它所指定的類,和要執行的方法是下面這個,FileDelete.java:
package com.mycompany.mvc.quartz;
import java.io.File;
import org.springframework.stereotype.Component;
import com.mycompany.mvc.config.SystemConfig;
/**
* @author Jintao_Ma
* @time 2016年7月25日21:45:35
* @description:
* 文件刪除類,可刪除多個文件夾下的文件
*/
@Component("fileDelete")
public class FileDelete {
private String folder = SystemConfig.getProperty("delTempFile");
private String[] folders={folder};
private static void delFiles(String[] folders) {
for (int i = 0; i < folders.length; i++) {
File f = new File(folders[i]);
File[] files = null;
if (f.exists()) {
files = f.listFiles();
for (int j = 0; j < files.length; j++) {
files[j].delete();
}
}
}
}
public void fileDelete_delFile(){
delFiles(folders);
}
}
就是刪除E:/Majintao/temp下面所有的文件夾,每5分鐘查詢一次。 (這個temp是我的臨時文件夾 ^_^)
5.總結
一個基本的基於maven的spring+mvc+mybatis框架就搭建好了,需要增加的東西還有很多,後面可以根據需要慢慢的往其中增加其他的一些東西,以後會繼續總結~ 下面是本篇講述項目的的基本結構:
PS:上一篇文章提到了事務的配置應該在service層,而且應拋出異常讓controller層try...catch處理。還應該注意一點是,由於controller層沒有事務,所以一個方法裏應該只有一個service層的方法,也就是說一個service層的方法,應該完成一個完整的業務。