struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成简单demo

网友投稿 189 2023-06-30


struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成简单demo

近期公司要开发新的项目,要用struts2+mybatis+spring框架,所以学习了下,来自己的博客发表下,希望能给大家带来帮助!

主要实现用户的增删改查操作

2、配置web.xml主要是配置struts2和spring

web.xml文件内容如下:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

index.jsp

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:beans.xml

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

index.jsp

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:beans.xml

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

3、配置spring配置文件,主要包括配置数据源、事务、mybaits等

beans.xml配置文件详细如下:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd">

4.JDBC配置文件详细

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/operationLog

jdbc.username=root

jdbc.password=

jdbc.maxActive = 2

jdbc.maxIdle =5

jdbc.minIdle=1

jdbc.initialSize =3

jdbc.maxWait =3000

5、配置mybatis主配置文件:

6、配置user.xml文件

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from user

where

username=#{username} and password=#{password}

select * from user

select *

from user where id=#{id}

<![CDATA[

insert into

user(username,password) values(#{username},#{password})

]]>

update user set

username=#{username},password=#{password} where id=#{id}

delete from user where

id=#{id}

7、User实体的写法

public class User implements Serializable {

private static final long serialVersionUID = -4415990281535582814L;

private Integer id;

private String username;

private String password;

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;

}

@Override

public String toString() {

return "User [id=" + id + ", password=" + password + ", username=" + username + "]";

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((id == null) ? 0 : id.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

User other = (User) obj;

if (id == null) {

if (other.id != null)

return false;

} else if (!id.equals(other.id))

return false;

return true;

}

}

8、UserDao的写法

public interface UserDao {

public abstract void insertUser(User user);

public abstract void updateUser(User user);

public abstract void deleteUser(Integer userId);

public abstract User findUserByid(Integer userId);

public abstract List findAll();

public abstract User userLogin(User user);

}

9、UserDao的实现

@Repository

public class UserDaoImpl implements UserDao {

private final String INSERT_USER = "insertUser";

private final String UPDATE_USER = "updateUser";

private final String DELETE_USER = "deleteUser";

private final String FIND_USER_BYID = "findUserById";

private final String SELECT_ALL_USER = "selectAllUser";

private final String USER_LOGIN = "userLogin";

@Autowired

private SqlSessionTemplate sqlSessionTemplate;

public void insertUser(User user) {

sqlSessionTemplate.insert(INSERT_USER, user);

}

public void updateUser(User user) {

sqlSessionTemplate.update(UPDATE_USER, user);

}

public void deleteUser(Integer userId) {

sqlSessionTemplate.delete(DELETE_USER, userId);

}

public User findUserByid(Integer userId) {

return sqlSessionTemplate.selectOne(FIND_USER_BYID, userId);

}

public List findAll() {

return sqlSessionTemplate.selectList(SELECT_ALL_USER);

}

public User userLogin(User user) {

return sqlSessionTemplate.selectOne(USER_LOGIN, user);

}

}

10、UserService接口

public interface UserService {

// 添加用户

public abstract void addUser(User user);

public abstract void updateUser(User user);

public abstract void deleteUser(Integer userId);

public abstract User findUserById(Integer userId);

public abstract List findAllUser();

public abstract User login(User user);

}

11、UserService接口的实现

@Service

@Transactional

public class UserServiceImpl implements UserService {

@Autowired

private UserDao userDao;

// 添加用户

public void addUser(User user) {

userDao.insertUser(user);

}

// 更新用户

public void updateUser(User user) {

userDao.updateUser(user);

}

public void deleteUser(Integer userId) {

userDao.deleteUser(userId);

}

public User findUserById(Integer userId) {

return userDao.findUserByid(userId);

}

public List findAllUser() {

return userDao.findAll();

}

public User login(User user) {

return userDao.userLogin(user);

}

}

12、配置Struts2

user_queryAllUser.action

/index.jsp&LTNMvmxlt;/result>

/userList.jsp

/userAdd.jsp

/userUpdate.jsp

13、UserAction具体实现

@Controller

@Scope("prototype")

public class UserAction extends ActionSupport {

@Autowired

private UserService userService;

private User user;

private List userList;

public String execute() throws Exception {

return null;

}

public String login() {

if (user != null) {

User user2 = userService.login(user);

if (user2 != null) {

return SUCCESS;

}

}

this.addFieldError("user.username", "用户名或密码错误!");

return INPUT;

}

public String addUI() {

return "addUser";

}

public String updateUI() {

user = userService.findUserById(user.getId());

return "updateUser";

}

public String add() {

userService.addUser(user);

return SUCCESS;

}

public String delete() {

userService.deleteUser(user.getId());

return SUCCESS;

}

public String update() {

userService.updateUser(user);

return SUCCESS;

}

public User getUser() {

return user;

}

public void setUser(User user) {

this.user = user;

}

public String queryAllUser() {

userList = userService.findAllUser();

return "userList";

}

public List getUserList() {

return userList;

}

public void setUserList(List userList) {

this.userList = userList;

}

}

14、登录页面的实现

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

添加新用户

15、添加页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

16、修改页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

17、列表页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

${id}删除


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 日期转换详解及实例代码
下一篇:java webApp异步上传图片实现代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~