激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - SpringMVC整合mybatis實(shí)例代碼

SpringMVC整合mybatis實(shí)例代碼

2020-04-27 12:31小禾點(diǎn)點(diǎn) JAVA教程

MyBatis 的前身就是 iBatis 。是一個(gè)數(shù)據(jù)持久層(ORM)框架。下面通過(guò)本文給大家介紹SpringMVC整合mybatis實(shí)例代碼,感興趣的朋友一起學(xué)習(xí)吧

MyBatis 本是apache的一個(gè)開(kāi)源項(xiàng)目iBatis, 2010年這個(gè)項(xiàng)目由apache software foundation 遷移到了google code,并且改名為MyBatis 。

一、逆向工程生成基礎(chǔ)信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--數(shù)據(jù)庫(kù)連接的信息:驅(qū)動(dòng)類、連接地址、用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3307/mybatis" userId="root"
password="jalja">
</jdbcConnection>
<!-- 默認(rèn)false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer,為 true時(shí)把JDBC DECIMAL 和
NUMERIC 類型解析為java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO類的位置 -->
<javaModelGenerator targetPackage="com.jalja.springmvc_mybatis.model.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
<!-- 從數(shù)據(jù)庫(kù)返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.jalja.springmvc_mybatis.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.jalja.springmvc_mybatis.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定數(shù)據(jù)庫(kù)表 -->
<table tableName="items"></table>
<table tableName="orders"></table>
<table tableName="orderdetail"></table>
<table tableName="user"></table>
</context>
</generatorConfiguration>
public static void main(String[] arhs) throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src.main.resources/generator.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}

二、springMVC與Mybatis整合 各個(gè)配置文件

1.項(xiàng)目結(jié)構(gòu)

2、各個(gè)文件的核心代碼

a.web.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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" version="3.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>3000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- post請(qǐng)求亂碼 -->
<filter>
<filter-name>SpringEncodingFilter</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>SpringEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- springMvc前端控制器 -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--
contextConfigLocation加載 springMvc的配置文件(處理器適配器 ,映射器)
如果不配置默認(rèn)加載的是 /WEB-INF/servlet名稱-servlet.xml(springMvc-servlet.xml)
-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<!--
1、*.do :DispatcherServlet 解析所有 *.do 結(jié)尾的訪問(wèn)
2、 / :DispatcherServlet解析所有請(qǐng)求(包括靜態(tài)資源) 這種配置可以實(shí)現(xiàn)restful風(fēng)格的url
3、/*: 這種配置最終要轉(zhuǎn)發(fā)到一個(gè)jsp頁(yè)面
-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- springMvc前端控制器 RestFul
<servlet>
<servlet-name>springMvc_rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc_rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>

b、config/mybatis/applicationContext-mybatis.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?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>
<!--
各個(gè)屬性
properties:
setting(全局配置參數(shù)配置):mybatis運(yùn)行時(shí)可以調(diào)整一些運(yùn)行參數(shù) 例如:開(kāi)啟二級(jí)緩存、開(kāi)啟延遲加載
typeAliases(類型別名): 在mapper.xml中定義parameterType 參數(shù)類型 resultType 返回類型時(shí)
需要指定類型的路徑 不方便開(kāi)發(fā),我們開(kāi)一針對(duì) 這些類型給其指定別名
typeHandler(類型處理器):在mybatis 中是通過(guò)typeHandler 完成 jdbc類型與java類型的轉(zhuǎn)化 ,mybatis 提供的處理器已可滿足 開(kāi)發(fā)需求
objectFactory(對(duì)象工廠):
plugins(插件):
environments(環(huán)境集合屬性對(duì)象):
environment(環(huán)境子屬性對(duì)象):
transactionManager(事務(wù)管理):
dataSource(數(shù)據(jù)源):
mappers(映射器):
-->
<!-- 對(duì)事務(wù)的管理和連接池的配置 -->
<!-- 延遲加載 -->
<settings>
<!-- 打開(kāi)延遲加載 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 積極加載改為消極加載 -->
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 開(kāi)啟二級(jí)緩存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
<!-- 針對(duì)單個(gè)別名定義 -->
<!-- <typeAlias type="com.jalja.myBatis.model.User" alias="user"/> -->
<!--批量別名的定義 mybatis 自動(dòng)掃描包中的類 別名就是類名(首字母大小寫都可以) -->
<package name="com.jalja.springmvc_mybatis.model.pojo"/>
<package name="com.jalja.springmvc_mybatis.model.custom"/>
<package name="com.jalja.springmvc_mybatis.model.vo"/>
</typeAliases>
<!--加載映射文件 -->
<!-- <mappers> <mapper resource="com/jalja/spring_mybatis/mapper/UserMapper.xml"/> -->
<!-- 和spring整合后 可以去掉
<package name="com.jalja.spring_mybatis.mapper"/> </mappers>-->
</configuration>

c、config/spring/applicationContext-dao.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 對(duì)JDBC配置進(jìn)行解密
<bean id="propertyConfigurer" class="cn.com.sinobpo.project.wfjb.utils.EncryptablePropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:resources/config/jdbc.properties</value>
</list>
</property>
</bean> -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc_driverClassName}</value>
</property>
<property name="url">
<value>${jdbc_url}</value>
</property>
<property name="username">
<value>${jdbc_username}</value>
</property>
<property name="password">
<value>${jdbc_password}</value>
</property>
<!-- 連接池最大使用連接數(shù) -->
<property name="maxActive">
<value>20</value>
</property>
<!-- 初始化連接大小 -->
<property name="initialSize">
<value>1</value>
</property>
<!-- 獲取連接最大等待時(shí)間 -->
<property name="maxWait">
<value>60000</value>
</property>
<!-- 連接池最大空閑 -->
<property name="maxIdle">
<value>20</value>
</property>
<!-- 連接池最小空閑 -->
<property name="minIdle">
<value>3</value>
</property>
<!-- 自動(dòng)清除無(wú)用連接 -->
<property name="removeAbandoned">
<value>true</value>
</property>
<!-- 清除無(wú)用連接的等待時(shí)間 -->
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<!-- 連接屬性 -->
<property name="connectionProperties">
<value>clientEncoding=UTF-8</value>
</property>
</bean>
<!-- spring和MyBatis完美整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/applicationContext-mybatis.xml"/>
</bean>
<!--使用 mapper 代理 的方式 mapper掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 掃描包路徑 如果需要掃描多個(gè)包 ,中間使用半角逗號(hào)隔開(kāi) -->
<property name="basePackage" value="com.jalja.springmvc_mybatis.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--聲明式 事務(wù)管理 使用jdbc的事務(wù)管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事務(wù)通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事務(wù)的切點(diǎn),并把事務(wù)切點(diǎn)和事務(wù)屬性不關(guān)聯(lián)起來(lái)AOP -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.jalja.springmvc_mybatis.service.impl.*.*(..))"/>
</aop:config>
</beans>

d、config/spring/applicationContext-service.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<bean id="itemsService" class="com.jalja.springmvc_mybatis.service.impl.ItemsServiceImpl"></bean>
</beans>

e、config/spring/springmvc.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<!--注解 處理器 映射器 -->
<!-- 映射器 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping springmvc3.1以前-->
<!-- 映射器 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping springmvc3.1以后 -->
<!-- 適配器 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter springmvc3.1以前-->
<!-- 適配器 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter springmvc3.1以后 -->
<!--配置映射器和 適配器
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
<!-- 開(kāi)啟注解 映射器和 適配器 這種方式默認(rèn)加載了很多參數(shù)綁定的方法 例如 json轉(zhuǎn)換解析器-->
<mvc:annotation-driven/>
<!-- 配置 Handler
<bean class="com.jalja.springmvc_mybatis.controller.UserController"/>-->
<!-- 注解 配置 基于組建掃描的方式 -->
<context:component-scan base-package="com.jalja.springmvc_mybatis.controller" />
<!-- 配置自定義參數(shù)解析器 -->
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<!-- 日期類型轉(zhuǎn)換 -->
<bean class="com.jalja.springmvc_mybatis.converter.CustomDateConverter"></bean>
</list>
</property>
</bean>
<!-- 全局異常處理器 -->
<bean class="com.jalja.springmvc_mybatis.exception.CustomExceptionResolver"/>
<!-- 文件上傳 -->
<!-- 支持上傳文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 文件大小 5M -->
<property name="maxUploadSize" value="5242880"/>
</bean>
<!-- 使用 restFul 風(fēng)格 編程 照成 的 靜態(tài)資源 訪問(wèn) 問(wèn)題 -->
<!-- <mvc:resources mapping="/js/**" location="/resources/js/"/> -->
<!-- springMVC攔截器的配置 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.jalja.springmvc_mybatis.interceptor.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<!-- 視圖映射 jsp解析 默認(rèn)使用jstl-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 默認(rèn)使用 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

f、config/jdbc.properties

?
1
2
3
4
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8
jdbc_username=root
jdbc_password=111111

g、config/log4j.properties

?
1
2
3
4
5
6
#在開(kāi)發(fā)環(huán)境下的日志級(jí)別 要設(shè)置成debug,生成環(huán)境設(shè)置成info 或error
log4j.rootLogger=debug, stdout
log4j.logger.org.apache.ibatis=debug
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

h、com/jalja/springmvc_mybatis/controller/ItemsController.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.jalja.springmvc_mybatis.controller;
import java.io.File;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.jalja.springmvc_mybatis.exception.CustomException;
import com.jalja.springmvc_mybatis.model.custom.ItemsCustom;
import com.jalja.springmvc_mybatis.service.ItemsService;
/**
* 商品
* @author PC003
*conterver參數(shù)轉(zhuǎn)換器 springMvc提供了很多參數(shù)轉(zhuǎn)換器
*/
@Controller
@RequestMapping("/items") //窄化請(qǐng)求映射
public class ItemsController {
@Autowired ItemsService itemsService;
@RequestMapping(value="/findItemsList")
public String findItemsList(Model model) throws Exception{
List<ItemsCustom> itemsList=itemsService.findItemsList(null);
System.out.println(itemsList);
model.addAttribute("itemsList", itemsList);
return "itemsList";
}
@RequestMapping(value="/editItems", method={RequestMethod.POST,RequestMethod.GET}) //限制Http請(qǐng)求方式
//@RequestParam 將請(qǐng)求參數(shù) 與 形式參數(shù)進(jìn)行綁定 required:指定屬性必須傳入值 defaultValue:設(shè)置默認(rèn)值
public String editItems(Model model, @RequestParam(value="id",required=true,defaultValue="0") Integer itemsId) throws Exception{
ItemsCustom itemsCustom=itemsService.findItemsById(itemsId);
if(itemsCustom==null){
throw new CustomException("商品不存在");
}
model.addAttribute("itemsCustom", itemsCustom);
return "editItems";
}
@RequestMapping(value="/updateItems")
public String editItemsSubmit(Integer id,ItemsCustom itemsCustom,MultipartFile itemsPic) throws Exception{
String uploadFileName=itemsPic.getOriginalFilename();//獲取上傳的文件名
if(itemsPic!=null && uploadFileName!=null && !uploadFileName.equals("")){
String imagesPath="E:\\develop\\upload\\images\\";
String newFileName=UUID.randomUUID()+uploadFileName.substring(uploadFileName.lastIndexOf("."),uploadFileName.length());
File newFile=new File(imagesPath+newFileName);
itemsPic.transferTo(newFile);//將內(nèi)存中的數(shù)據(jù)寫入磁盤
itemsCustom.setPic(newFileName);
}
itemsService.updateItemsById(id, itemsCustom);
return "redirect:findItemsList.do"; //重定向
}
//JSON的使用 @ResponseBody:將對(duì)像轉(zhuǎn)json輸出 @RequestBody:將請(qǐng)求參數(shù)轉(zhuǎn) java對(duì)象
@RequestMapping(value="/jsonRequest")
public @ResponseBody ItemsCustom jsonRequest(@RequestBody ItemsCustom itemsCustom) throws Exception{
return itemsCustom;
}
//RestFul 風(fēng)格 編程 /restFulRequest/{id}:表示將這個(gè)位置的參數(shù)傳到 @PathVariable 指定的名稱中
@RequestMapping(value="/restFulRequest/{id}")
public @ResponseBody ItemsCustom restFulRequest(@PathVariable("id") Integer id) throws Exception{
ItemsCustom itemsCustom=itemsService.findItemsById(id);
return itemsCustom;
}
}

以上所述是小編給大家介紹的SpringMVC整合mybatis實(shí)例代碼,希望對(duì)大家有所幫助,如果大家想了解更多資訊敬請(qǐng)關(guān)注服務(wù)器之家網(wǎng)站!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久99精品久久久久久秒播放器 | 亚洲精品欧美二区三区中文字幕 | 特级黄色一级毛片 | 黄色片免费看看 | 精品久久久久久久久久久久 | 欧美成人一级片 | 精品一区二区久久久久久按摩 | 国产亚洲精品久久久久久网站 | 欧美日韩成人一区二区 | 国产午夜精品一区二区三区在线观看 | 奇米影视奇米色777欧美 | 日韩精品一二区 | 免费视频xxxx| 国产日韩一区二区三区在线观看 | 免费中文视频 | 亚洲国产精品一区二区三区 | 色av综合在线 | 99成人精品视频 | 国产九色视频在线观看 | 一区二区免费 | 精品国产91久久久久久久 | 一级成人在线 | 国产精品视频专区 | 国产一区毛片 | 91精品国产91热久久久做人人 | 91福利影视| 国产在线免费 | 免费看一级毛片欧美 | h视频在线免费观看 | 国产一级aa大片毛片 | 七首小情歌泰剧在线播放 | 亚洲影视综合网 | 亚洲第一页中文字幕 | 日本成人一区二区 | 久久精品99久久久久久2456 | 国产无区一区二区三麻豆 | 免费黄色成人 | 精品国产乱码久久久久久久 | 色屁屁xxxxⅹ在线视频 | 国产成人强伦免费视频网站 | 牛牛a级毛片在线播放 |