SpringMVC+Spring+MyBatis框架

环境配置

1.加入Spring、SpringMVC、数据库驱动等的相关jar文件

2.Spring配置文件

在resource文件夹下增加Spring配置文件

<?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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="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/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"
	>
	<context:component-scan base-package="service,dao"/>
	<import resource="applicationContext-mybatis.xml"/>
</beans>

3.配置web.xml

<?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">
  <!-- 配置环境参数,指定spring配置文件所在目录 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext-*.xml</param-value>
  </context-param>
  <!-- 配置spring的ContextLoaderListener监听器,初始化Spring容器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>
  		org.springframework.web.servlet.DispatcherServlet
  	</servlet-class>
  	<!-- 初始化参数 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc-servlet.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

改造

1.改造DAO层

在Dao类上使用@Repository注解

@Repository
public interface UserDao {
}

2.改造Service层

在Service实现类类名上增加@Service注解
成员变量userDao通过@Resource注解进入注入

@Service("userService")
public class UserServiceImpl implements UserService{

	@Resource(name="userMapper")
	private UserMapper userMapper;
	public UserMapper getUserMapper() {
		return userMapper;
	}
}	

3.改造Controller层

创建Controller层,在类的类名上加入注解@Countroller在类中的方法上加入注解@ResultMapping(@RequestParam String userCode)

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	@RequestMapping("/welcome")
	public String welcome(@RequestParam(value="username",required=false) String username) {
		logger.info("welcome,username:"+username);
		return "index";
	}
}

传参

/*
RequestParam映射参数 ,username接受参数后 通过RequestParam映射参数,
value需和jsp页面值一样没用哦model的addAttribute方法进行包装
*/
public String welcome(@RequestParam(value="username",required=false) String username,Model model) {
		logger.info("welcome,username:"+username);
		model.addAttribute("username", username);
		return "index";
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值