廢話很少說,直接上配置代碼css
1:applicationContext.xmlmysql
<context:annotation-config />
<context:component-scan base-package="com.spring.demo"/> <!-- 自動掃描全部註解該路徑 -->
<!-- 自動掃描組件,須要把controller去掉,不然影響事務管理 -->
<context:component-scan base-package="com.spring.demo">
<context:exclude-filter type="regex" expression="com.spring.demo.controller.*"/>
</context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/qzy?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="1000" />
<property name="poolPreparedStatements" value="true" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事務 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config expose-proxy="true">
<aop:pointcut id="txPointcut" expression="execution(public * com.spring.demo.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
</aop:config>web
2:dispatcher.xmlspring
<!-- 啓動掃描全部的controller -->
<context:component-scan base-package="com.spring.demo.controller"/>
<mvc:annotation-driven/>sql
<!-- 解決亂碼問題 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
</bean> express
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size100MB -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
</bean> apache
<mvc:resources mapping="/images/**" location="/image/" cache-period="31556926"/>
<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>
<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>json
3 controllermvc
@Controller
@RequestMapping("/province")
public class ProvinceController {app
private static Logger log = Logger.getLogger(ProvinceController.class);
@Autowired
private ProvinceService provinceService;
@RequestMapping(value = "", method = RequestMethod.GET ,produces="application/json;charset=UTF-8")
@ResponseBody
public Map<String,Object> getProvice() {
Map<String,Object> map = new HashMap<String, Object>();
try {
return this.provinceService.list();
} catch (SQLException e) {
e.printStackTrace();
return map;
}
}
@RequestMapping(value = "/update", method = RequestMethod.POST ,produces="application/json;charset=UTF-8")
@ResponseBody
public Map<String,Object> updateProvince(@RequestBody String str,@RequestHeader HttpHeaders headers,HttpServletRequest request) {
Map<String,Object> map = new HashMap<String,Object>();
try {
JSONObject json1 = new JSONObject(str);
String name = json1.optString("name");
int id = json1.optInt("id");
System.out.println("name:"+name);
boolean bl = this.provinceService.updateProvinceName(id, name);
System.out.println(bl);
map.put("code", 0);
map.put("msg", "success");
return map;
} catch (Exception e) {
e.printStackTrace();
return map;
}
}
}
4:service
@Service("provinceService")
public class ProvinceServiceImpl implements ProvinceService {
@Autowired
private ProvinceDao provinceDao;
@Override
public Map<String, Object> list() throws SQLException{
return this.provinceDao.list();
}
/* (非 Javadoc)
* <p>Title: updateProvinceName</p>
* <p>Description: </p>
* @param provinceId
* @return
* @throws SQLException
* @see com.spring.demo.service.ProvinceService#updateProvinceName(int)
*/
@Override
@Transactional(propagation=Propagation.REQUIRED)
public boolean updateProvinceName(int provinceId,String provinceName){
for (int id : new int[] { 2, 3 }) {
this.provinceDao.updateProvinceName(provinceId, provinceName);
int j = 1 / 0;
}
return false;
}
}