准备工作:
下载整合所需的jar包 点击此处下载
使用MyBatis Generator生成dao接口、映射文件和实体类 如何生成
搭建过程:
先来看一下项目的 目录结构
1.配置dispatcherServlet-servlet.xml,将此文件放于WEB-INF下
/schema/beans /schema/beans/spring-beans-3.0.xsd
/schema/context /schema/context/spring-context-3.0.xsd">
2.配置web.xml
/xml/ns/javaee/web-app_3_0.xsd">
login.jsp
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
1
dispatcherServlet
/
default
*.css
default
*.gif
default
*.jpg
default
*.js
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
3.配置applicationContext.xml,并将自动生成的dao实现类交给Spring管理
/schema/context
/schema/context/spring-context-3.1.xsd
/schema/aop
/schema/aop/spring-aop-3.1.xsd">
4.将service实现类交给Spring管理
@Service("userService")public class UserServiceImpl implementsIUserService {
@Autowired// 此处使用ByType自动装配 dao实现类privateUserEntityMapper userDao;
@OverridepublicUserEntity login(UserEntity user) {returnuserDao.getByNameAndPwd(user);
}@Override
@Transactional// 使用该注解实现事务管理public voiddelById(String id) {
userDao.delByid(id);
}}
5.创建Controller
@Controller
@RequestMapping(value="/emp")public classEmpController {
@Autowired// 使用ByType装配privateIEmpService empService;@RequestMapping(value="/queryEmps")publicString queryEmps(HttpServletRequest request){
List list =empService.queryAll();request.getSession().setAttribute("list",list );return "index.jsp";
}
6.现在就可以使用 http://localhost:8080/ProjectName/emp/queryEmps访问到上述Controller