004_SpringMVC分层配置文件

一. Dao层

1. 添加spring和mybatis整合包

2. Mybatis必须的配置文件SqlMapConfig.xml, 空文件即可。

3. applicationContext-dao.xml

3.1. 数据库连接池

3.2. 配置SqlSessionFactory对象, 在spring和mybatis整合包下。

3.3. 配置mapper文件扫描器。

二. Service层

1. applicationContext-service.xml包扫描器, 扫描@service注解的类。

2. applicationContext-trans.xml配置事务。

三. Controller层

1. springmvc核心配置文件springmvc.xml

1.1. 配置包扫描器, 扫描@Controller注解的类。

1.2. 配置注解驱动。

1.3. 配置视图解析器。

四. web.xml配置

1. 配置上下文加载监听器来加载spring配置文件

2. 配置前端控制器

. SpringMVC与Mybatis整合例子

1. 新建一个名为SpringMVCMybatis的Web工程, 同时拷入相关jar包

2. 新建一个名为User.java的实体类

package com.lywgames.domain;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	private Integer id;
	private String username;
	private String password;
	private Date registertime;
	
	public User() {}
	
	public User(String username, String password, Date registertime) {
		this.username = username;
		this.password = password;
		this.registertime = registertime;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getRegistertime() {
		return registertime;
	}

	public void setRegistertime(Date registertime) {
		this.registertime = registertime;
	}

}

3. 新建一个名为UserMapper.java的数据库操作接口

package com.lywgames.dao;

import java.util.List;
import com.lywgames.domain.User;

public interface UserMapper {
	public List<User> selectUser();
	
	public void insertUser(User u);
}

4. 新建一个名为UserMapper.xml数据库映射文件

5. 新建一个名为UserService.java的业务层接口

package com.lywgames.service;

import java.util.List;
import com.lywgames.domain.User;

public interface UserService {
	List<User> selectUser();
}

6. 新建一个名为UserServiceImpl.java的业务层实现类

package com.lywgames.service.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.lywgames.dao.UserMapper;
import com.lywgames.domain.User;
import com.lywgames.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Resource
	private UserMapper userMapper;
	
	@Override
	public List<User> selectUser() {
		// 测试事务
		userMapper.insertUser(new User("rew", "fds4", new Date()));
		int result = 1 / 0;
		userMapper.insertUser(new User("fsda", "fds4", new Date()));
		
		return userMapper.selectUser();
	}

}

7. 新建一个名为UserAction.java的处理器类 

package com.lywgames.web.action;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.lywgames.domain.User;
import com.lywgames.service.UserService;

@Controller
public class UserAction {
	@Resource
	private UserService userService;
	
	@RequestMapping("userList")
	public ModelAndView getItemList(String name, HttpServletRequest req) {
		ModelAndView modelAndView = new ModelAndView();
		List<User> users = userService.selectUser();
		modelAndView.addObject("userList", users);
		modelAndView.setViewName("userList"); 
		return modelAndView;
	}
}

8. 在config(Source Folder)目录下新建jdbc.properties数据库连接属性文件

9. 在config(Source Folder)目录下新建SqlMapConfig.xml配置文件

10. 在config(Source Folder)/spring目录下新建applicationContext-dao.xml配置文件

11. 在config(Source Folder)/spring目录下新建applicationContext-service.xml配置文件

12. 在config(Source Folder)/spring目录下新建applicationContext-trans.xml配置文件

13. 在config(Source Folder)/spring目录下新建springmvc.xml配置文件

14. 修改web.xml

15. 编写index.jsp

16. 编写userList.jsp

17. 查看user表

18. 运行项目

19. 点击获取用户列表, 发生了除0异常, 数据库数据并没有增加, 因为我们添加了事务

20. 注释掉除0异常, 重新访问首页

21. 点击获取用户列表, 到达用户列表页面, 同时查看数据库多了2个用户

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值