【SSM】環境搭建:整合Spring + SpringMVC + MyBatis + JSON + RESTFUL + 其他(處理音訊檔案上傳)
阿新 • • 發佈:2020-09-09
極環境搭建:Spring + SpringMVC + MyBatis + JSON + RESTFUL風格 + 其他
(1)匯入相關jar以及依賴
略,,,
(2)配置檔案的配置:
① web.xml配置:
<!-- springMVC核心控制器 --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定springMVC配置檔案路徑 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/mvc.xml</param-value> </init-param> <!-- 設定在tomcat啟動建立控制器 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 指定所有的springMVC控制器訪問以.do結尾 --> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 編碼設定,spring自帶的過濾器 --> <filter> <filter-name>encodingFilter</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>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping> <!-- spring工廠初始化監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 標明載入spring工廠的配置檔案路徑 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext.xml</param-value> </context-param>
② mvc.xml
<!-- 掃描註解:控制器層 --> <context:component-scan base-package="com. controller"/> <!—訪問路徑對映註解、響應轉化json替換--> <mvc:annotation-driven> <!-- 轉換json格式 --> <mvc:message-converters> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!—靜態資源處理--> <mvc:default-servlet-handler/> 或者: <mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/img/" mapping="/img/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <!-- 檢視解析器:字首、字尾 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 上傳檔案配置:id固定 --> <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"> <property name="maxUploadSize" value="100000000"></property> </bean>
③ applicationContext.xml/beans.xml/spring.xml配置:
<!-- 掃描註解 --> <context:component-scan base-package="com "> <!—不掃描controller--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan <!-- 引入小配置檔案 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 建立資料來源 --> <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource"> <property name="driverClassName" value="${mysql.driver}"/> <property name="url" value="${mysql.url}"/> <property name="username" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> </bean> <!-- 建立sqlSessionFactory --> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com /mapper/*Mapper.xml"/> <property name="typeAliasesPackage" value="com.entity"/> </bean> <!-- 建立dao --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/> <property name="basePackage" value="com.dao"/> </bean> <!-- 宣告式事務控制 --> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 事務控制註解生效 --> <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
④ 小配置檔案:db.properties
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/cmfz
mysql.username=root
mysql.password=root
…
⑤ DaoMapper.xml
<mapper namespace="com.dao.UserDao">
<select id="getUser" resultType="User">
select * from admin whiere id = #{id}
</select>
</mapper>
⑥ Controller
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
@Qualifier(“userServiceImpl”)
private UserService userService;
@RequestMapping("login")
public String login(){
return "index"; //跳轉到/index.jsp,在配置中配置了字首和字尾
}
@RequestMapping("/getUser/{userId}") //REST風格接受引數和響應JSON物件
public @ResponseBody User getUser(@PathVariable(userId) String id){
return userService.getUser(id)
}
/*檔案上傳時的程式碼:
try {
String realPath = session.getServletContext().getRealPath("/upload/img");
File file = new File(realPath);
if(!file.exists()){
file.mkdirs();
}
String originalFilename = fileName.getOriginalFilename();
String name = new Date().getTime()+"_"+originalFilename;
fileName.transferTo(new File(realPath,name));
map.put("mess", "上傳成功");
} catch (Exception e) {
e.printStackTrace();
map.put("mess", "上傳失敗");
}
//音訊上傳---------------------------------------------
(1)匯入jar:jaudiotagger-2.0.3.jar
(2)程式碼處理:
File audioFile = new File(filePath); //通過音訊路徑讀取
audioFile.getSize();//獲取大小,單位位元組(B),除以1024為KB
AudioFile read = AudioFileIO.read(audioFile);
AudioHeader audioHeader = read.getAudioHeader();
audioHeader.getTrackLength(); //獲取時長,單位為秒(s)
//音訊上傳---------------------------------------------
頁面請求下載時可用window.location
*/
}
⑦ service實現類:
@Service
@Transactional//事務的管理
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Override
public User getUser() {
User user = userDao.getUser();
return user;
}
}