近い将来、同社は新しいプロジェクトを開発し、Struts2+MyBatis+Spring Frameworkを使用します。それで、それを学んだ後、私はそれを私のブログに投稿し、あなたを助けることを望んでいます!
主に、ユーザーの追加、削除、変更、および検索操作を実現します
1.対応するJARパッケージをインポートします
2. Web.xmlの構成主にStruts2とSpringを構成します
web.xmlファイルのコンテンツは次のとおりです。
<?xml version = "1.0" encoding = "utf-8"?> <web-appバージョン= "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instcance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 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> <! - スプリング構成ファイルの読み込み - > <リスナー> <リスナークラス> org.springframework.web.contextloaderlistener </ristener-class> </ristener> <! <param-name> contextconfiglocation </param-name> <param-value> classpath:beans.xml </param-value> </context-param> <! - configure struts2-> <filter> <filter-name> struts2 </filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutspareandexecutefilter </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3.主にデータソース、トランザクション、MyBaitなどの構成を含む、スプリング構成ファイルを構成します。
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 = " xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/a. http://www.springframework.org/schema/aop http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/spring-tx-x.x. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframeworkwwork/コメントで豆を構成 - > <コンテキスト:annotation-config /> <! - スキャンするパッケージを構成 - > <コンテキスト:component-scanベースパッケージ= "com.pdsu.edu"> < /compontion:component-scan> <! - proxy-target-class = "true" proxy-target-class = "true" /> <! - データベース構成ファイルの場所 - > <コンテキスト:プロパティプレイスホルダー場所= "classpath:jdbc.properties" /> <! - dbcpデータソース - > <bean id = "datasource" destroy-method = "close"> <プロパティ= <プロパティ名= "url" value = "$ {jdbc.url}" /> <property name = "username" value = "$ {jdbc.username}" /> <プロパティ名= "パスワード=" $ {jdbc.password} " /> <! - 最低時刻 - value = "$ {jdbc.minidle}"> </property> <! - millisecondsでの最大待機時間 - > <プロパティ名= "maxwait" value = "$ {jdbc.maxwait}"> </property> <! - 最大アクティブな数値 - > <プロパティname = "Maxactive} name = "Intiallsize" value = "$ {jdbc.initialsize}"> </property> </bean> <! - mybitassqlsessionfactorybean-> <bean id = "sqlsessionfactory"> <プロパティ名= "dataSource" ref = "dataSource"/> "configlocet =" configlocet = "fallocation < /bean> <! - sqlsessiontemplate-> <bean id = "sqlsessiontemplate"> <constructor-arg name = "sqlsessionfactory" ref = "sqlsessionfactory" /> < /bean> <! - トランザクション構成 - > <bean id = "="> <propetial manager "> <" datasurce "datasurce" datasurce " </bean> <! - アノテーションアノテーションを使用してトランザクションを構成 - > <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 JDBC.InitialSize = 3 JDBC.MaxWait = 3000
5。mybatisメイン構成ファイルを構成します。
<?xml version = "1.0" encoding = "utf-8"?> <!doctype構成public " - // mybatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-config.dtd" type = "com.pdsu.edu.domain.user"/> </typealiase> <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" <resultmap type = "com.pdsu.edu.domain.user" id = "ueserresult"> <respurent property = "id" column = "id" jdbctype = "integer" javatype = "java.lang.integer" /> <resultプロパティ= "username" columname " id = "userlogin" parametertype = "user" resultmap = "userresult"> select * username =#{username}とpassword =#{password} </select> <selectmap = "userresult"> select * fromユーザー</select> <select> < [ユーザーからid =#{id} </select> <Id = "Insertuser" parametertype = "user"> <![cdata [username、password)values(#{username}、#{password})]]]]]]]]]] username =#{username}、password =#{password}ここで、id =#{id} </update> <delete id = "deleteuser" parametertype = "int">ユーザーからの削除=#{id} </delete> </mapper> 7.ユーザーエンティティの書き方
パブリッククラスユーザーはSerializable {private static final long serialversionuid = -441599028153582814l;プライベート整数ID;プライベート文字列ユーザー名;プライベート文字列パスワード。 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(){パスワードを返します。 } 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());返品結果; } @Override public boolean equals(object obj){if(this == obj)return true; if(obj == null)falseを返します。 if(getClass()!= obj.getClass())falseを返します。ユーザーother =(user)obj; if(id == null){if(other.id!= null)falseを返します。 } else if(!id.equals(other.id))return false; trueを返します。 }} 8。userdaoの書き方
public interface userdao {public abstract void insertuser(user user); public abstract void updateUser(ユーザーユーザー); public abstract void deleteuser(integer userid); public abstract user finduserbyid(integer userid);パブリックアブストラクトリスト<ユーザー> findall(); public abstract userlogin(ユーザーユーザー); } 9。userdaoの実装
@Repository Public Class UserDaoImplを実装しているuserdao {private final string insert_user = "insertuser";プライベート最終文字列update_user = "updateUser";プライベート最終文字列delete_user = "deleteuser";プライベート最終文字列find_user_byid = "finduserbyid";プライベート最終文字列select_all_user = "selectaluser";プライベート最終文字列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 userlogin(user user){return sqlsessiontemplate.selectone(user_login、user); }} 10。Userserviceインターフェイス
パブリックインターフェイスユーザーサービス{//ユーザーパブリックアブストラクトvoid adduser(user user); public abstract void updateUser(ユーザーユーザー); public abstract void deleteuser(integer userid); public abstract user finduserbyid(integer userid);パブリックアブストラクトリスト<user> findalluser();パブリックアブストラクトユーザーログイン(ユーザーユーザー); } 11。ユーザーサービスインターフェイスの実装
@Service @Transactional Public Class userserviceImplを実装している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ソフトウェア財団// dtd struts構成2.1 // en" "http://struts.apache.org/dtds/struts-2.1.dtd" value = "utf-8"/> <! - httpservletrequestのsetcharacterencoding()およびfreemarkerで使用されるデフォルトのエンコードセット、およびvilocityの出力 - > <constant name = "struts.configuration.xmlreload" value = "true"/> < name = "struts.devmode" value = "true"/> <! - 開発モードで詳細なエラー情報を印刷 - > <constant name = "struts.ui.theme" value = "xhtml"/> <package name = "user" namespace = "/user" extends = "struts-default"> <action name = "user_" method = "{1}"> <" type = "redirectaction"> user_queryalluser.action </result> <result name = "input">/index.jsp </result name = "userlist">/userlist.jsp </result name> <result name = "adduser">/useradd.jsp </result> <result name = "uppations.jsp 13。ユーザーアクセス固有の実装
@controller @scope( "Prototype")Public Classユーザーアクションsupport {@autowired private userservice userservice;プライベートユーザーユーザー;プライベートリスト<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"、 "ユーザー名またはパスワードのエラー!");入力を返します。 } public string addui(){return "adduser"; } public string updutui(){user = userservice.finduserbyid(user.getId()); 「updateUser」を返します。 } public string add(){userservice.adduser(user);成功を返す; } public string delete(){userservice.deleteuser(user.getId());成功を返す; } public string update(){userservice.updateuser(user);成功を返す; } public user getUser(){return user; } public void setuser(user user){this.user = user; } public string queryalluser(){userlist = userservice.findalluser(); 「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> user login </title> <meta http-equiv = "pragma" content = ""> chach content = "no-cache"> <meta http-equiv = "compers" content = "0"> <meta http-equiv = "keywords" content = "keyword1、keyword2、keyword3"> <meta http-equiv = "description" content = "this is my page"> <! href = "styles.css"> - > <s:head/> </head> <body> <body> <center> <h1> user login </h1> <s: "user_addui" namespace = "/user">新しいユーザー</s:a> <s:form action = "user_login" namespace = "" post " name = "user.username"> </s:textfield> <s:passwordラベル= "パスワード" name = "user.password"> </s:password> <s:submit value = "login"> </s:submit> </s:form> </center> </body> </html>
15.ページを追加します
<%@ page Language = "Java" Import = "Java.util。*" PageEncoding = "UTF-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> <!doctype public " - // w3c // dtd htmlユーザー</title> <meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "expires" content = "0"> </head> <body> <h1 <h1> < namespace = "/user" method = "post"> <s:textfield label = "username" name = "user.username"> </s:textfield> <s:password label = "password name =" user.password "> </s:パスワード> <
16.ページを変更します
<%@ページ言語= "java" import = "java.util。*" pageencoding = "utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> < <Meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "content =" 0 "> </head> <body> <center> <h1> method = "post"> <s:hidden name = "user.id"> </s:hidden> <s:textfield label = "username" name = "username"> </s:textfield> <s:password label = "password" name "user.password" user.password "> s:password> <s:submit value =" "> </s:submit> </s
17。リストページ
<%@ page Language = "Java" Import = "Java.util。*" PageEncoding = "Utf-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> < <Meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "content =" " namespace = "/user">新しいユーザーを追加</s:a> </h3> <table> <th> user id </th> <th> user name </th> <th>ユーザーパスワード</th> <th>操作</th> </th> <s:iterator value = "userlist"> <td> <td> <s 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> modify </s:s:s namespace = "/user"> <s:param name = "user.id"> $ {id} </s:param> delete </s:a> </td> </s:iterator> </table> </center> </body> </html>上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。