论坛首页 入门技术论坛

Spring,Struts2.0,Ibatis框架整合_1

浏览 1668 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-28   最后修改:2010-01-28
    近期在公司参与开发的项目是用Spring+Struts2.0+Ibatis框架整合的,很多底层的东西都被那些架构师们封装好了,如果拆出来拿到其他项目用的话,基本上相当于重新整合。为此,自己整合了一下,也查看了很多人的整合方案,觉得有点乱,希望自己整合的不会那么乱,哈哈,不过有个缺点是对事务的处理还没搞进去。我把数据持久层(*Dao),业务逻辑层(*Service),action层(*Action)等的写法分两种了,一种是全部都写在applicationContext.xml里,另一种是全部分开写,然后再将各自所在的xml引进applicationContext.xml里。

先看总体分包情况,现在所有配置文件都写在WEB-INF下,如果要写在src的某个包下的话要改变一下路径。


以前项目经理用经验跟我们说,开发一般是从后台到前台,所以,我先从后台的代码发起。

建表语句:
drop database if exists zhiming;
create database zhiming;
use zhiming;

create table z_user
(
	id int(11) not null primary key auto_increment,
	name varchar(32) not null,
	sex varchar(32) not null,
	age varchar(32) not null,
	address varchar(100) default null,
	phone varchar(32) not null,
	email varchar(50) default null
);

insert z_user values (1,'musoon','male','23','广州天河珠江新城','13600000000','musoon@126.com');
insert z_user values (2,'li','female','22','广州白云区','13600000000','li@126.com');

select * from z_user;


POJO类,User.java:

/** 
	* File Name:User.java * Version: * Date:2010-1-3 * Copyright Belongs To Musoon  Corporation 2010  
*/
	
package com.vo;

import java.io.Serializable;


/**
 * Project Name:ZhiMing   ** Class Name:User 
 * Author:Musoon        ** Created Time:2010-1-3 下午08:37:31 
 * Changed By:Musoon   ** Changed Time:2010-1-3 下午08:37:31 
 * Changed Memo: 
 * @version 
 * Class Description: 
 */
public class User implements Cloneable, Serializable {
	
	private static final long serialVersionUID = -9103289450003300385L;

	private int id;
	private String name;
	private String sex;
	private String age;
	private String address;
	private String phone;
	private String email;
	
/*	public void clear() {
		id = 0;
		name = null;
		sex = null;
		age = null;
		address = null;
		phone = null;
		email = null;
	}
	
	public User() {
		this.clear();
	}*/
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
}



对应的sqlMap,User_sqlMap.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>
	<typeAlias alias="user" type="com.vo.User"/>
	
	<insert id="saveUser" parameterClass="user">
		<selectKey keyProperty="id" resultClass="int">
			<![CDATA[
				select (max(id)+1) as value
			]]>
		</selectKey>
		<![CDATA[
			insert int z_user(name,sex,age,address,phone,email)
			values(#name#,#sex#,#age#,#address#,#phone#,#email#)
		]]>
	</insert>
	
	<delete id="deleteUser" parameterClass="int">
		<![CDATA[
			delete from z_user where id=#id#
		]]>
	</delete>
	
	<select id="getAllUsers" resultClass="user">
		<![CDATA[
			select * from z_user
		]]>
	</select>
	
	<update id="updateUser" parameterClass="user">
		<![CDATA[
			update z_user set
			name=#name#,
			sex=#sex#,
			age=#age#,
			address=#address#,
			phone=#phone#,
			email=#email#
			where id=#id#
		]]>
	</update>
	
	<select id="findUserById" parameterClass="int" resultClass="user">
		<![CDATA[
			select * from z_user where id=#id#
		]]>
	</select>
	
</sqlMap>




  • 大小: 11.8 KB
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics