springmvc纯注解配置文件配置例子

springmvc纯注解配置文件配置例子

springmvc纯注解配置文件配置例子

描述

applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task" 
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/task    http://www.springframework.org/schema/task/spring-task-3.0.xsd"
       default-autowire="byName">
	<description>Spring公共配置</description>
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
	<context:component-scan base-package="project">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	<bean name="springApplicationContextHolder" class="project.util.SpringApplicationContextHolder"></bean>
	<!-- 数据源配置 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<!-- 需要修改的配置地方 mysql -->
		<property name="url" value="jdbc:mysql://localhost:3306/javawebxx_blog?characterEncoding=UTF-8" />
		<property name="username" value="root" />
		<property name="password" value="root" />
		
		<property name="filters" value="stat" />
		<property name="maxActive" value="80" />
		<property name="initialSize" value="1" />
		<property name="maxWait" value="60000" />
		<property name="minIdle" value="1" />
		<property name="timeBetweenEvictionRunsMillis" value="3000" />
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="SELECT 'x' FROM DUAL" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
	</bean>
	<!-- jdbc事务管理器 -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource">  
            <ref local="dataSource" />  
        </property>  
    </bean>  
    <!--事务模板 -->  
    <bean id="transactionTemplate"  
        class="org.springframework.transaction.support.TransactionTemplate">  
        <property name="transactionManager">  
            <ref local="transactionManager" />  
        </property>  
        <!--ISOLATION_DEFAULT 表示由使用的数据库决定  -->  
        <property name="isolationLevelName" value="ISOLATION_DEFAULT"/>  
        <property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/>  
        <!-- <property name="timeout" value="30"/> -->  
    </bean>  
    <!-- db操作类 -->
    <bean id="db"  
        class="project.util.DBHelper">  
        <property name="jdbcTemplate">  
            <ref local="jdbcTemplate" />  
        </property>
        <property name="transactionTemplate">  
            <ref local="transactionTemplate" />  
        </property>
    </bean>
</beans>

 

一个controller配置例子:


/**
 * @author https://www.javawebxx.com    javaweb学习网
 * 备注:>ssm框架ssh框架springmvc框架搭建下载javaweb学习网
 */
package project.controller.admin;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import project.controller.MyController;

/**
*分类
* @author Administratorxxxx
* @date2019-01-08
*/
@Controller("fenleiController")
@RequestMapping(value = "/admin/fenlei")
public class FenleiController extends MyController {
	

/**
* 查询frame
*/
	@RequestMapping(value = "/frame")
	public String frame(Model model, HttpServletRequest request,String flag)throws Exception {
		return "/admin/fenlei/frame";
	}
	
/**
* 查询列表
*/
	@RequestMapping(value = "/list")
	public String list(Model model, HttpServletRequest request,String flag,String flName)throws Exception {
		//select date_format(insertDate, '%Y-%m-%d %H:%i:%s')
		//CONVERT(varchar, insertDate, 120 )
		//to_char(insertDate,'yyyy-mm-dd,hh24:mi:ss') 

		String sql="select a.* from t_fenlei a where 1=1 ";


	if(flName!=null&&!"".equals(flName)){
			sql+=" and flName like '%"+flName+"%'";
		}
		sql+=" order by id desc";
		List list = db.queryListForPage(sql, request);
		request.setAttribute("list", list);
		return "/admin/fenlei/list";
	}
	
/**
* 编辑保存(包含修改和添加)
*/
	@RequestMapping(value = "/editSave")
	public ResponseEntity editSave(Model model,HttpServletRequest request,Long id,String flag
		,String flName) throws Exception{
		int result = 0;
		if(id!=null){
			String sql="update t_fenlei set flName=? where id=?";
			result = db.update(sql, new Object[]{flName,id});
		}else{
			String sql="insert into t_fenlei(flName) values(?)";
			result = db.update(sql, new Object[]{flName});
		}
		if(result==1){
			return renderData(true,"操作成功",null);
		}else{
			return renderData(false,"操作失败",null);
		}
	}
	
/**
* 删除信息
*/
	@RequestMapping(value = "/editDelete")
	public ResponseEntity editDelete(Model model,HttpServletRequest request,Long id,String flag) throws Exception {
		
		String sql="delete from t_fenlei where id=?";
		int result = db.update(sql, new Object[]{id});
		if(result==1){
			return renderData(true,"操作成功",null);
		}else{
			return renderData(false,"操作失败",null);
		}
		
	}
	
/**
* 跳转到编辑页面
*/
	@RequestMapping(value = "/edit")
	public String edit(Model model, HttpServletRequest request,Long id,String flag)throws Exception {
		if(id!=null){
			//修改
			String sql="select * from t_fenlei where id=?";
			Map map = db.queryForMap(sql,new Object[]{id});
			model.addAttribute("map", map);
		}String sql="";

		return "/admin/fenlei/edit";
	}
}

 



留言