struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成简单demo
程序员文章站
2024-03-12 15:33:32
近期公司要开发新的项目,要用struts2+mybatis+spring框架,所以学习了下,来自己的博客发表下,希望能给大家带来帮助!
主要实现用户的增删改查操作
1、...
近期公司要开发新的项目,要用struts2+mybatis+spring框架,所以学习了下,来自己的博客发表下,希望能给大家带来帮助!
主要实现用户的增删改查操作
1、导入相应的jar包
2、配置web.xml主要是配置struts2和spring
web.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 加载spring的配置文件 --> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- 配置spring配置文件加载的位置 --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!-- 配置struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3、配置spring配置文件,主要包括配置数据源、事务、mybaits等
beans.xml配置文件详细如下:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 采用注释的方式配置bean --> <context:annotation-config /> <!-- 配置要扫描的包 --> <context:component-scan base-package="com.pdsu.edu"></context:component-scan> <!--proxy-target-class="true"强制使用cglib代理 如果为false则spring会自动选择--> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 数据库配置文件位置 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置dbcp数据源 --> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <property name="driverclassname" value="${jdbc.driverclassname}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 队列中的最小等待数 --> <property name="minidle" value="${jdbc.minidle}"></property> <!-- 队列中的最大等待数 --> <property name="maxidle" value="${jdbc.maxidle}"></property> <!-- 最长等待时间,单位毫秒 --> <property name="maxwait" value="${jdbc.maxwait}"></property> <!-- 最大活跃数 --> <property name="maxactive" value="${jdbc.maxactive}"></property> <property name="initialsize" value="${jdbc.initialsize}"></property> </bean> <!-- 配置mybitassqlsessionfactorybean --> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> <property name="configlocation" value="classpath:mybatis.xml"></property> </bean> <!-- 配置sqlsessiontemplate --> <bean id="sqlsessiontemplate" class="org.mybatis.spring.sqlsessiontemplate"> <constructor-arg name="sqlsessionfactory" ref="sqlsessionfactory" /> </bean> <!-- 事务配置 --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource" /> </bean> <!-- 使用annotation注解方式配置事务 --> <tx:annotation-driven transaction-manager="transactionmanager"/> </beans>
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主配置文件:
<?xml version="1.0" encoding="utf-8"?> <!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typealiases> <typealias alias="user" type="com.pdsu.edu.domain.user"/> </typealiases> <mappers> <mapper resource="com/pdsu/edu/domain/sqlmappers/user.xml" /> </mappers> </configuration>
6、配置user.xml文件
<?xml version="1.0" encoding="utf-8" ?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.pdsu.edu.domain.user"> <resultmap type="com.pdsu.edu.domain.user" id="userresult"> <result property="id" column="id" jdbctype="integer" javatype="java.lang.integer" /> <result property="username" column="username" /> <result property="password" column="password" /> </resultmap> <select id="userlogin" parametertype="user" resultmap="userresult"> select * from user where username=#{username} and password=#{password} </select> <select id="selectalluser" resultmap="userresult"> select * from user </select> <select id="finduserbyid" parametertype="int" resultmap="userresult"> select * from user where id=#{id} </select> <insert id="insertuser" parametertype="user"> <![cdata[ insert into user(username,password) values(#{username},#{password}) ]]> </insert> <update id="updateuser" parametertype="user"> update user set username=#{username},password=#{password} where id=#{id} </update> <delete id="deleteuser" parametertype="int"> delete from user where id=#{id} </delete> </mapper>
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<user> 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<user> 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<user> 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<user> findalluser() { return userdao.findall(); } public user login(user user) { return userdao.userlogin(user); } }
12、配置struts2
<?xml version="1.0" encoding="utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.i18n.encoding" value="utf-8"/> <!-- 指定默认编码集 ,作用于httpservletrequest的setcharacterencoding()和freemarker,vilocity的输出 --> <constant name="struts.configuration.xmlreload" value="true"/> <!-- 当struts配置文件修改时是否自动加载 --> <constant name="struts.devmode" value="true"/> <!-- 开发模式下打印详细的错误信息 --> <constant name="struts.ui.theme" value="xhtml"/> <package name="user" namespace="/user" extends="struts-default"> <action name="user_*" class="useraction" method="{1}"> <result name="success" type="redirectaction">user_queryalluser.action</result> <result name="input">/index.jsp</result> <result name="userlist">/userlist.jsp</result> <result name="adduser">/useradd.jsp</result> <result name="updateuser">/userupdate.jsp</result> </action> </package> </struts>
13、useraction具体实现
@controller @scope("prototype") public class useraction extends actionsupport { @autowired private userservice userservice; private user user; private list<user> 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<user> getuserlist() { return userlist; } public void setuserlist(list<user> 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+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>用户登录</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <s:head/> </head> <body> <center> <h1>用户登录</h1> <s:a action="user_addui" namespace="/user">添加新用户</s:a> <s:form action="user_login" namespace="/user" method="post"> <s:textfield label="用户名" name="user.username"></s:textfield> <s:password label="密码" name="user.password"></s:password> <s:submit value="登录"></s:submit> </s:form> </center> </body> </html>
15、添加页面
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title>添加新用户</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <center> <h1>添加新用户</h1> <s:form action="user_add" namespace="/user" method="post"> <s:textfield label="用户名" name="user.username"></s:textfield> <s:password label="密码" name="user.password"></s:password> <s:submit value="提交"></s:submit> </s:form> </center> </body> </html>
16、修改页面
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title>修改用户</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <center> <h1>修改用户</h1> <s:form action="user_update" namespace="/user" method="post"> <s:hidden name="user.id"></s:hidden> <s:textfield label="用户名" name="user.username"></s:textfield> <s:password label="密码" name="user.password"></s:password> <s:submit value="提交"></s:submit> </s:form> </center> </body> </html>
17、列表页面
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <title>用户列表</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <center> <h2>用户列表</h2> <h3><s:a action="user_addui" namespace="/user">添加新用户</s:a> </h3> <table width="90%" border="1"> <tr> <th>用户id</th> <th>用户名称</th> <th>用户密码</th> <th>操作</th> </tr> <s:iterator value="userlist"> <tr> <td><s:property value="id"/> </td> <td><s:property value="username"/> </td> <td><s:property value="password"/> </td> <td><s:a action="user_updateui" namespace="/user"><s:param name="user.id">${id}</s:param>修改</s:a> <s:a action="user_delete" namespace="/user"><s:param name="user.id">${id}</s:param>删除</s:a></td> </tr> </s:iterator> </table> </center> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 基于Java web服务器简单实现一个Servlet容器
下一篇: mysql - 刷题记录
推荐阅读
-
struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成简单demo
-
struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成简单demo
-
Spring Boot整合logback一个简单的日志集成架构
-
Spring mvc整合tiles框架的简单入门教程(maven)
-
CodeIgniter框架实现的整合Smarty引擎DEMO示例
-
CodeIgniter框架实现的整合Smarty引擎DEMO示例
-
Spring mvc整合tiles框架的简单入门教程(maven)
-
laravel5.6 框架邮件队列database驱动简单demo示例
-
Spring项目整合成SpringBoot的简单登录拦截Demo
-
【简单到离谱而又十分实用的测试工具】SSM框架轻松整合Swagger