项目中一般不会单独使用某一个框架,而是使用几个框架整合。
mybatis+springmvc整合后程序运行顺序:
用户请求界面,然后会根据配置文件向后台发出请求,找到对应的控制器,控制器调用service,service调用dao来与数据库进行交互,dao会寻找对应Mapper,XXXMapper.xml里面写的是sql语句,这时就可以与数据库交互,并将结果一步步返回给用户界面。(说的不专业,大概是这个意思)
解决Spring配置文件xml没有类提示:
https://blog.csdn.net/a_helloword/article/details/80557692
这里记录一下mybatis+springmvc的整合步骤:
一、创建一个web项目。
二、使用myeclipse给该项目添加spring框架,这里使用的是spring4.1。
三、添加mybatis以及mybatis和spring整合的jar包:
四、编写配置文件:
配置文件:
web.xml:
EncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
forceEncoding
true
encoding
utf-8
EncodingFilter
/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springmvc
*.do
applicationContext.xml:
com.mysql.jdbc.Driver
springmvc.xml:
conf.xml:
登录界面:index.jsp
用户登录
${msg }
登录成功界面:main.jsp
欢迎${empty nowuser ? "游客":nowuser.userName },来到我的主页
查看用户列表
查看用户列表分页
LoginCtrl.java:
package com.mfc.ctrl;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.mfc.entity.User;
import com.mfc.service.IUserService;
@Controller
@Scope("prototype")
@RequestMapping("login")
@SessionAttributes("nowuser")
public class LoginCtrl {
@Resource(name="userService")
IUserService userService;
@RequestMapping("userLogin")
public ModelAndView userLogin(ModelAndView mav,User user){
System.out.println("姓名:"+user.getUserName()+" 密码:"+user.getUpass());
User nowuser = userService.login(user);
if(nowuser != null){
//属性名和@SessionAttributes中的属性名一致的话,存入session对象
mav.addObject("nowuser", nowuser);
mav.setViewName("redirect:../main.jsp");
}else{
mav.addObject("msg", "用户名或密码错误!");
mav.setViewName("forward:../index.jsp");
}
return mav;
}
}
service层:
接口:
public interface IUserService {
public User login(User user);
}
实现类:
@Service("userService")
public class UserService implements IUserService {
@Resource(name="userDao")
IUserDao userDao; //面向接口编程
public User login(User user) {
User u = null;
//处理业务逻辑
if(user != null&&user.getUserName() != null&&user.getUpass() != null){
u = userDao.login(user);
}
return u;
}
}
dao层:
接口:
public interface IUserDao {
//用户登录检验
public User login(User user);
}
实现类:
package com.mfc.dao;
import java.util.List;
import javax.annotation.Resource;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import com.mfc.entity.User;
import com.mfc.mapper.UserMapper;
@Repository("userDao")
public class UserDao implements IUserDao {
@Resource(name="sqlSessionTemplate")
SqlSessionTemplate sqlSessionTemplate;
public UserMapper getUserMapper(){
return sqlSessionTemplate.getMapper(UserMapper.class);
}
public User login(User user) {
User u = null;
List list=this.getUserMapper().selectUser(user);
if(list != null&&list.size() > 0){
u = list.get(0);
}
return u;
}
}
mapper:
UserMapper.java:
package com.mfc.mapper;
import java.util.List;
import com.mfc.entity.User;
public interface UserMapper {
public List selectUser(User user);
public void addUser(User user);
}
UserMapper.xml:这个文件里面就是写SQl语句的。
insert into user values
(null,#{userName},#{userAge},#{userAddress},#{upass})
实体类:
package com.mfc.entity;
/*
* 实体类
* */
public class User {
private int id;
private String userName;
private int userAge;
private String userAddress;
private String upass;
//set,get,构造方法省略
}
五、测试可用!
六、源码下载:http://download.csdn.net/detail/fancheng614/9907891