Mybatis+Struts2的结合:实现用户插入和查找

  总结一下今天一个成功的小实验:Mybatis+Struts2的结合:实现用户插入和查找。删除和修改如果以后写了,会继续更新。

  一 准备工作。

   1.新建一个java web项目。

   2.在webContent\lib目录下导入所需要的jar包。

     a.struts2需要的jar包。

struts2
xwork-core.jar
strut2-core.jar
ognl.jar
commoms-lang.jar
freemarker.jar
commons-fileupload.jar

      包的位置:

struts-2.3.24.1-all\struts-2.3.24.1\apps\WEB-INF\lib

      我下载的struts2的版本这个目录下有13个jar包,每个jar包的作用网上都可以查到。正常使用加入这六个就可以了。

    b.mybatis需要的的jar包。

      

asm.jar
cglib.jar
commons-logging-.jar
log4j.jar
mybatis.jar
slf4j-api.jar
slf4j-log4j12.jar

    c.mysql需要的jar包。

mysql-connector-java-5.1.-bin.jar

    到此准备工作就做好了。

  二.工程目录    

    在这里我贴出我的工程目录,便于我后面进行描述。

    Mybatis+Struts2的结合:实现用户插入和查找

    三.写代码。

      对于Java web 工程的搭建,服务器的搭建我就不多说了,因为网上资料很多。(虽然我写的也有很多,但是只是想给自己做一个简单的总结,顺便分享给需要的人) 

      1.webContent\lib\web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MybatisAndStruts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
11 <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

    2.index.jsp

      

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% String path = request.getContextPath();
String BasePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=BasePath %>"></base>
<title>首页</title>
</head>
<body>
<h3 align="center">添加用户</h3>
<form action="user/user!add">
<table align="center">
<tr>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form> <h3 align="center">查询所有用户</h3>
<p align="center"><a href="user/user!get">查询</a></p>
</body>
</html>

    3. user_add_success.jsp

    

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#33CC99">
<h3 align="center">user add success</h3></body>
</html>

    

    4. user_get.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="#33CC99">
<h3 align="center">用户列表</h3></body>
<table align="center">
<tr>
<td>ID</td>
<td>用户名</td>
<td>密码</td>
</tr> <s:iterator value="users" >
<tr>
<td> <s:property value="id" /> </td>
<td> <s:property value="username" /> </td>
<td> <s:property value="password" /> </td>
</tr>
</s:iterator>
</table> </html>

    

    5. struts.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" /> <package name="default" namespace="/user" extends="struts-default">
<action name="user" class="com.hjj.action.UserAction" >
<result name="add">
/user_add_success.jsp
</result> <result name="get">
/user_get.jsp?users=${users}
</result>
</action>
</package>
</struts>

     

    6. conf.xml

 <?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="000000" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/hjj/dao/userMapper.xml"/>
</mappers>
</configuration>

    7. userMapper.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.hjj.dao.userMapper">
<select id="getUser" resultType="com.hjj.model.User">
select * from user;
</select> <insert id="insertUser" parameterType="com.hjj.model.User">
insert into user (username,password) values (#{username},#{password});
</insert>
</mapper>

    

    8 .User.java

 package com.hjj.model;

 public class User {

     private int id;
private String username;
private String password; public User(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public User() {
super();
// TODO Auto-generated constructor stub
} public int getId() {
return id;
}
public void setId(int 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 + ", username=" + username + ", password=" + password + "]";
}
}

    9. UserAction.java

  

 package com.hjj.action;

 import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import com.hjj.dao.MysqlDAO;
import com.hjj.model.*;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class UserAction extends ActionSupport implements ModelDriven<User>{
private User user = null;
List<User> users = new ArrayList<User>(); public List<User> getUsers() {
return users;
} public void setUsers(List<User> users) {
this.users = users;
} public String add() throws IOException{
System.out.println(user);
MysqlDAO dao = new MysqlDAO();
dao.insertUser(user);
return "add";
} public String get() throws IOException{
MysqlDAO dao = new MysqlDAO();
users = dao.getUsers();
return "get";
} public User getModel() {
if(user == null){
user = new User();
}
return user;
} }

    10 . MysqlDAO.java

 package com.hjj.dao;

 import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.hjj.model.User; public class MysqlDAO { private SqlSessionFactory sessionFactory = null;
private SqlSession session = null; public MysqlDAO() throws IOException{
String resource = "conf.xml"; Reader reader = Resources.getResourceAsReader(resource); sessionFactory = new SqlSessionFactoryBuilder().build(reader); }
public List<User> getUsers(){ List<User> users = new ArrayList<User>(); session = sessionFactory.openSession(); String statement = "com.hjj.dao.userMapper.getUser"; users = session.selectList(statement); session.commit(); return users;
} public void insertUser(User user){ session = sessionFactory.openSession(); String statement = "com.hjj.dao.userMapper.insertUser"; int row = session.insert(statement, user);
session.commit();
System.out.println(row);
}
}

  四.运行代码。

  1.地址栏输入:http://localhost:8080/MybatisAndStruts/index.jsp。显示页面如下(页面简单丑陋)。

Mybatis+Struts2的结合:实现用户插入和查找

第一个文本输入框为用户名,第二个为密码。

    2. 提交后页面显示

    Mybatis+Struts2的结合:实现用户插入和查找

3.返回index.jsp。点击查询。显示列表。

    Mybatis+Struts2的结合:实现用户插入和查找

    至此,所有的代码和测试已经写完了。这应该算是很简单的一个mybatis和struts的整合了,就当做入门练习了。至于里面的一些东西,例如ModelDriven,和user/user!add这些东西,学过struts2的人应该都知道。不过为了自己能够更深刻的理解,在明天我也会做出总结。

      

上一篇:ASP.NET缓存全解析4:应用程序数据缓存 转自网络原文作者李天平


下一篇:html5中新增的元素和废除的元素