ssm框架搭建详细(spring4+struts2+mybatis3)


    以前工作中,项目用的是ssm(spring,struts,mbatis)框架,但是自己没有搭建过,最近花了一段时间来整理学习了一下,把最近的收获分给大家,主要是ssm的搭建。


1.首先我们要准备好自己框架所需要的jar包

    1.1 struts2所需jar包:包括整合spring的jar包

ssm框架搭建详细(spring4+struts2+mybatis3)_第1张图片

   1.2 spring所需的jar包:我这里是把spring的所有核心jar包都考了进来

  ssm框架搭建详细(spring4+struts2+mybatis3)_第2张图片

  1.3  mybatis所需要的jar包和其他,链接数据库,log4j,的jar包

  ssm框架搭建详细(spring4+struts2+mybatis3)_第3张图片

上面是所需要的jar包,由于是后来才写的博文,有可能还有不全,忘大家多提意见!


2.配置xml配置文件

2.1 mybatis的配置文件内容:mybatis-config.xml




	
		
	
	
	
		
	



增删改查的操作,userinfoMapping.xml





	
	
		
		
		
		
		
		
		
	

	
	
		id,username,password,sex,birthday,insertDate,message
	

	
	
		insert into
		userinfo(id,username,password,sex,birthday,message,insertDate)
		values(#{id},#{username},#{password},#{sex},#{birthday,jdbcType=DATE},#{message,jdbcType=CLOB},sysdate)
	
	
	
	
	
	
	
		delete from userinfo where id = #{id}
	
	
	
	
	
	
		update userinfo set
		name=#{name},sex=#{sex},birthday=#{birthday},message=#{message}
		where
		id=#{id}
	

	
	
		update userinfo set
		
			
				username=#{username},
			
			
				sex=#{sex},
			
			
				birthday=#{birthday},
			
			
				message=#{message},
			
		
		where id=#{id}
	




2.2 struts配置文件的内容:struts.xml






	
		
		
		
		

		
	
	
    


2.3 spring的配置文件的内容,有详细的注解:applicationContext.xml

!--  -->

	
	
	
	

	
	
	

	
	
		
		
		
		

		
	

	

	
		
	

	

	
		
		
		
		
	

	
	
		
			
			
			
			
		
	

	


	
	
		
			
	
	 


pringle.xml的内容




	
		
			
		
	

	
	
	
	


2.4数据库连接的文件:jdbc.properties,这里要根据我们需要的数据库进行不同的修改,我是使用 的Oracle

db.driver=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@localhost:1521:orcl
db.username=user1
db.password=user1

2.5 log4j的文件:自己根据情况自己配置

llog4j.rootLogger = debug, stdout, R  

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern = %p  %d{yyyy-MM-dd HH:mm:ss} -- %m -- %l%n

###file logger###
log4j.appender.R = org.apache.log4j.RollingFileAppender
log4j.appender.R.File = d:/logs/lylg.log
log4j.appender.R.MaxFileSize = 1Mb
log4j.appender.R.MaxBackupIndex = 100
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern =%p  %d{yyyy-MM-dd HH\:mm\:ss} -- %m -- %l%n

log4j.logger.org.springframework=debug,console
log4j.logger.org.apache.ibatis = ERROR
log4j.logger.com.ibatis=ERROR
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=ERROR
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=ERROR
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=ERROR
log4j.logger.java.sql.Connection = ERROR
log4j.logger.java.sql.Statement = ERROR
log4j.logger.java.sql.PreparedStatement = ERROR
log4j.logger.java.sql.ResultSet = ERROR


3. 在数据库中创建我们相应的表:Userinfo

create table USERINFO
(
  ID         VARCHAR2(32) not null,
  USERNAME   VARCHAR2(20),
  PASSWORD   VARCHAR2(20),
  SEX        VARCHAR2(2),
  INSERTDATE DATE,
  MESSAGE    CLOB,
  PIC        BLOB,
  BIRTHDAY   DATE
)

-- Add comments to the columns 
comment on column USERINFO.ID
  is 'id';
comment on column USERINFO.USERNAME
  is '用户名';
comment on column USERINFO.PASSWORD
  is '密码';
comment on column USERINFO.SEX
  is '性别';
comment on column USERINFO.INSERTDATE
  is '入库时间';
comment on column USERINFO.MESSAGE
  is '长数据';
comment on column USERINFO.PIC
  is '头像';
comment on column USERINFO.BIRTHDAY
  is '生日';


4.编写我们的类

4.1先创建我们的dao接口,这里是利用alldao的思路实现的,所以又一个all到类

public interface IUserinfoDao {

	public int insertUserinfo(Userinfo userinfo);
	public List selectPage(PageModel page);
	public long userCount(Map pageMap);
	public void deleteById(String id);
	public Userinfo queryById(String id);
	public int updateById(Userinfo userinfo);
	public int updateBySelective(Userinfo userinfo);
	
}
@Repository
public class AllDao {

	@Autowired
	private IUserinfoDao userinfodao;

	public IUserinfoDao getUserinfodao() {
		return userinfodao;
	}

	public void setUserinfodao(IUserinfoDao userinfodao) {
		this.userinfodao = userinfodao;
	}

}


4.2 service类,这里也是利用了all的思想,所以也有allservice的类

@Service
public class UserinfoService {

	@Autowired
	private AllDao alldao;

	public int insertUserinfo(Userinfo user) {
		return this.alldao.getUserinfodao().insertUserinfo(user);

	}

	public long userCount(Map pageMap) {
		return this.alldao.getUserinfodao().userCount(pageMap);
	};

	public List selectPage(PageModel page) {

		return this.alldao.getUserinfodao().selectPage(page);
	}

	public void deleteById(String id) {

		 this.alldao.getUserinfodao().deleteById(id);
	}
	public Userinfo queryById(String id){
		return this.alldao.getUserinfodao().queryById(id);
	};
	public int updateById(Userinfo userinfo){
		return this.alldao.getUserinfodao().updateById(userinfo);
	};
	public int updateBySelective(Userinfo userinfo){
		return this.alldao.getUserinfodao().updateBySelective(userinfo);
	};
}


@Service
public class AllService {

	@Autowired
	private UserinfoService userinfoservice;

	public UserinfoService getUserinfoservice() {
		return userinfoservice;
	}

	public void setUserinfoservice(UserinfoService userinfoservice) {
		this.userinfoservice = userinfoservice;
	}

}


4.3 下面就是我们的实现功能,对应的controller,这里有一个base,是用来声明service的,公共使用,所有的类都要继承它。

public class BaseController extends ActionSupport {

	private AllService allservice;

	public AllService getAllservice() {
		return allservice;
	}

	public void setAllservice(AllService allservice) {
		this.allservice = allservice;
	}

}

功能的实现类:我这里的代码包括了分页的功能,也是自己写的,有需要的可以看看,要原码的在联系我。

@Transactional
/**
 * 操作增删改查
 * @author Administrator
 *
 */
public class Insert extends BaseController {

	cJson cjson = new cJson();
	private String jsonString;

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	// 分页model
	public PageModel page;

	public String insert() {
		try {
			JSONObject object = JSONObject.fromObject(jsonString);
			String name = object.getString("name");
			String password = object.getString("password");
			String sex = object.getString("sex");
			String id = UUID.randomUUID().toString().replaceAll("-", "");
			Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse(object
					.getString("birthday"));

			String message = object.getString("message");

			Userinfo user = new Userinfo(id, name, password, sex, birthday,
					new Date(), message);
			int ses = this.getAllservice().getUserinfoservice()
					.insertUserinfo(user);
			if (ses == 1) {
				cjson.write("true");
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String selectById() {

		return null;
	}

	public String selectPage() {
		try {
			JSONObject object = JSONObject.fromObject(jsonString);
			int gotoPage = object.getInt("gotoPage");
			int pageNum = object.getInt("pageNum");
			Map pageMap = new HashMap();
			pageMap.put("sex", "男");
			int userCount = (int) this.getAllservice().getUserinfoservice()
					.userCount(pageMap);

			page = new PageModel(userCount, pageNum, pageMap);
			page.setGotoPage(gotoPage);

			List userinfoList = this.getAllservice()
					.getUserinfoservice().selectPage(page);
			page.setList(userinfoList);
			cjson.write(page);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据id删除
	 * 
	 * @return
	 */
	public String deleteById() {

		try {
			this.getAllservice().getUserinfoservice().deleteById(jsonString);
			cjson.write("true");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据id查询
	 * 
	 * @return
	 */
	public String queryById() {
		try {
			Userinfo userinfo = this.getAllservice().getUserinfoservice()
					.queryById(jsonString);
			cjson.write(userinfo);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String update() {
		try {
			JSONObject object = JSONObject.fromObject(jsonString);
			String id = object.getString("userId");
			String name = object.getString("name");
			String password = object.getString("password");
			String sex = object.getString("sex");
			String bir = object.getString("birthday");
			Date birthday = null;
			if (bir != null && !bir.equals("")) {
				birthday = new SimpleDateFormat("yyyy-MM-dd").parse(bir);

			}

			String message = object.getString("message");

			Userinfo userinfo = new Userinfo(id, name, password, sex, birthday,
					new Date(), message);
			this.getAllservice().getUserinfoservice()
					.updateBySelective(userinfo);
			cjson.write("true");
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

对其中不懂的地方解释:cjson.write()是自己写的,其实是用的json传参,具体类:

public class cJson {
	static HttpServletResponse response;
	static PrintWriter out;

	public void write(String jsonString) throws IOException {

		String returnJSONObject = jsonString;
		//System.out.println(returnJSONObject);
		response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/htm;");
		out = response.getWriter();

		out.print(returnJSONObject);
		out.flush();
		out.close();
		
	}

	public void write(Object jsonObj) throws IOException {

		String returnJSONObject = JSONArray.fromObject(jsonObj).toString();
		//System.out.println(returnJSONObject);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/htm;");

		PrintWriter out = response.getWriter();

		out.print(returnJSONObject);
		out.flush();
		out.close();
	}
}


5.下面是页面的功能

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>






My JSP 'index.jsp' starting page












	
username: password: sex: 男 
birthday: CLOB:
文件:
编号 姓名 性别 操作
name 修改删除
首页  上一页  下一页  末尾 |   / 1跳转 | 每页

如有需要下载代码的,可加群:399846037 里面有上传,希望互相学习。






你可能感兴趣的:(java,ssm,spring,struts2,mybatis)