Struts2.0+ibatis2.3完美整合实例如何实现呢?让我们开始具体的步骤:
Struts2.0+ibatis2.3完美整合实例一、我们先把数据库的表映射出来,即Users.java
- package org.login.vo;
- /**
- * Users Entity Bean
- * @author jinlong0
- */
- public class Users {
- 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;
- }
- public Users() {
- }
- public Users(Integer id, String userName, String password) {
- super();
- this.id = id;
- this.userName = userName;
- this.password = password;
- }
- }
Struts2.0+ibatis2.3完美整合实例二、接下来我们把映射的XML写出来,即Users.xml
- ﹤?xml version="1.0" encoding="UTF-8"?﹥
- ﹤!DOCTYPE sqlMap
- PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-2.dtd"﹥
- ﹤sqlMap namespace="User"﹥
- ﹤typeAlias alias="user" type="org.login.vo.Users"/﹥
- ﹤resultMap class="user" id="get-all-user"﹥
- ﹤result property="id" column="id"/﹥
- ﹤result property="userName" column="username"/﹥
- ﹤result property="password" column="password"/﹥
- ﹤/resultMap﹥
- ﹤select id="checkUser" parameterClass="user" resultClass="user"﹥
- ﹤![CDATA[
- SELECT id, username, password FROM users WHERE username = #userName# and password = #password#
- ]]﹥
- ﹤/select﹥
- ﹤select id="getUserById" parameterClass="java.lang.Integer" resultClass="user"﹥
- ﹤![CDATA[
- SELECT id, username, password FROM users WHERE id = #id#
- ]]﹥
- ﹤/select﹥
- ﹤select id="getAllUser" resultMap="get-all-user"﹥
- ﹤![CDATA[
- SELECT id, username, password FROM users
- ]]﹥
- ﹤/select﹥
- ﹤insert id="insertUser" parameterClass="user"﹥
- ﹤![CDATA[
- INSERT INTO users (username, password) VALUES (#userName#, #password#)
- ]]﹥
- ﹤/insert﹥
- ﹤update id="updateUser" parameterClass="user"﹥
- ﹤![CDATA[
- UPDATE users SET username=#userName#, password=#password# WHERE id=#id#
- ]]﹥
- ﹤/update﹥
- ﹤delete id="deleteUser" parameterClass="java.lang.Integer"﹥
- ﹤![CDATA[
- DELETE FROM users WHERE id = #id#
- ]]﹥
- ﹤/delete﹥
- ﹤/sqlMap﹥
在这里我们要特别注意的是与Hibernate的xml的区别,在这个映射的xml里有了所有的数据操作的SQL语句,这也是ibatis的一个特点,它的半自动化,让开发人员能随心所欲的,想必学过ibatis的都很清楚了,在这里我就不详细说明了,呵呵!
Struts2.0+ibatis2.3完美整合实例三、接下来我们就写DAO层的接口及实现
接口IUserDAO.java
- package org.login.dao;
- import java.util.List;
- import org.login.vo.Users;
- public interface IUserDAO {
- List﹤Users﹥ getAllUser();
- Users getUserById(Integer id);
- boolean isLogin(Users user);
- boolean insertUser(Users user);
- boolean updateUser(Users user);
- boolean deleteUser(Integer id);
- }
- 实现类:UserDAO.java
- package org.login.dao.Impl;
- import java.sql.SQLException;
- import java.util.List;
- import org.login.dao.IUserDAO;
- import org.login.vo.Users;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Service;
- import com.ibatis.sqlmap.client.SqlMapClient;
- @SuppressWarnings("unchecked")
- @Service("userDAO")
- public class UserDAO implements IUserDAO {
- @Autowired(required=false)
- @Qualifier("sqlClient")
- private SqlMapClient client = null;
- public SqlMapClient getClient() {
- return client;
- }
- public void setClient(SqlMapClient client) {
- this.client = client;
- }
- @Override
- public boolean deleteUser(Integer id) {
- try {
- client.delete("deleteUser", id);
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- @Override
- public List﹤Users﹥ getAllUser() {
- List﹤Users﹥ list = null;
- try {
- list = client.queryForList("getAllUser");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return list;
- }
- @Override
- public boolean insertUser(Users user) {
- try {
- client.insert("insertUser", user);
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- @Override
- public boolean updateUser(Users user) {
- try {
- client.update("updateUser", user);
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- @Override
- public boolean isLogin(Users user) {
- try {
- Users u = (Users) client.queryForObject("checkUser", user);
- if (u != null) {
- return true;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- @Override
- public Users getUserById(Integer id) {
- Users u = null;
- try {
- u = (Users) client.queryForObject("getUserById", id);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return u;
- }
- }
在这里,接口和我们前面的Hibernate的例子类同,只是实现类区别有点大了。
特别应该注意的是Spring2.5中类的注入方式,是用标注方式注入的,即:
@Service("userDAO")
@Autowired(required=false)
@Qualifier("sqlClient")
这是我们特别应该注意的,这是标注,详细说明见Spring2.5的开发手册。
这篇就写到这里吧,下篇我们将说明服务层及Struts层的实现,如果有兴趣研究的朋友可以先下载示例代码先看下,具体的下载地址如下:
演示示例下载地址:Struts2.0+Spring2.5.1+ibatis2.3地址:http://download.csdn.net/source/331581
Struts2.0+ibatis2.3完美整合实例之映射实现就向你介绍到这里,希望对你有所帮助。
【编辑推荐】