《struts2+spring3+mybatis整合》由会员分享,可在线阅读,更多相关《struts2+spring3+mybatis整合(9页珍藏版)》请在金锄头文库上搜索。
1、-src|-com.ssm|-action|-LoginAction.java|-entity|-User.java|-iface|-IUserDao.java|-impl|-UserDao.java|-seriface|-IUserServices.java|-impl|-UserServices.java|-sqlmap|-User.xmlapplicationContext.xmldatabase.Propertieslog4j.PropertiesmyBatis-config.xmlstruts.Propertiesstruts.xmlok 直接上代码了:Action 类: Login
2、Action.javapackage com.ssm.action;import java.util.ArrayList;import java.util.List;import com.opensymphony.xwork2.Action;import com.ssm.entity.User;import com.ssm.seriface.IUserService;public class LoginAction implements Actionprivate User user;private IUserService userServices;public void setUserSe
3、rvices(IUserService userServices)this.userServices = userServices;public User getUser()return user;public void setUser(User user)this.user = user;public String execute() throws ExceptionString aaa= getUser().getId()+;List userList= new ArrayList();userList= userServices.selectUserById(getUser().getI
4、d();if(userList.size()0)return Action.SUCCESS;elsereturn Action.ERROR;实体类User.javapackage com.ssm.entity;import java.io.Serializable;import java.util.Date;public class User implements Serializableprivate int id;private String name;private String password;private Date birthday;public User()public Use
5、r(String name, String password, Date birthday)this.name= name;this.password= password;this.birthday= birthday;public int getId()return id;public void setId(int id)this.id = id;public String getName()return name;public void setName(String name)this.name = name;public String getPassword()return passwo
6、rd;public void setPassword(String password)this.password = password;public Date getBirthday()return birthday;public void setBirthday(Date birthday)this.birthday = birthday;IUserDao 接口类IUserDao.javapackage com.ssm.iface;import java.util.List;public interface IUserDaopublic boolean userVaild(String na
7、me, String password);public List selectUserById(int id);UserDaoImpl 实现接口类 :UserDaoImpl.javapackage com.ssm.iface.impl;import java.util.List;import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;import com.ssm.iface.IUserDao;/import com.ssb.baseutil.SqlMapClientDaoSupport;/import com.s
8、sb.iface.IUserDao;/import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;public class UserDaoImpl extends SqlMapClientDaoSupport implements IUserDaopublic List selectUserById(int id)/ TODO Auto-generated method stubreturn getSqlMapClientTemplate().queryForList(selectUserById, id);publ
9、ic boolean userVaild(String name, String password)/ TODO Auto-generated method stubreturn false;/*public boolean userVaild(String name, String password)return false;public List selectUserById(String id)return getSqlMapClientTemplate().queryForList(getUserById, id);*/业务类IUserServices.javapackage com.
10、ssm.seriface;import java.util.List;public interface IUserServicepublic boolean userVaild(String name, String password);public List selectUserById(int id);UserServicesImpl.javapackage com.ssm.seriface.impl;import java.util.List;import com.ssm.iface.IUserDao;import com.ssm.seriface.IUserService;public
11、 class UserServiceImpl implements IUserServiceprivate IUserDao serviceUserDao= null;public IUserDao getServiceUserDao()return serviceUserDao;public void setServiceUserDao(IUserDao serviceUserDao)this.serviceUserDao = serviceUserDao;public List selectUserById(int id)/ TODO Auto-generated method stubr
12、eturn serviceUserDao.selectUserById(id);public boolean userVaild(String name, String password)/ TODO Auto-generated method stubreturn serviceUserDao.userVaild(name, password);mybatis 实体配置文件User.xml sprng 配置文件applicationContext.xml 数据库连接属性配置文件(其实可以不用这么复杂)database.Propertiesjdbc.driverClassName=com.my
13、sql.jdbc.Driverjdbc.url=jdbc:mysql:/127.0.0.1:3306/ddtest?characterEncoding=utf8jdbc.username=rootjdbc.password=123cpool.checkoutTimeout=5000cpool.minPoolSize=20cpool.maxPoolSize=50cpool.maxIdleTime=7200cpool.maxIdleTimeExcessConnections=1800cpool.acquireIncrement=10log4j 随便到 log4j 里 copy 一个吧mybatis-config.xmlstruts 配置文件struts.Properties#整合 spring 的struts.objectFactory=springstruts.objectFactory.spring.useClassCache = truestruts.locale=zh_CNstruts.i18n.encoding=GBKstruts 的 Action 配置文件struts.xmlsuccess.jsperror.jspsuccess.jsperror.jsp