Spring+Struts+ibatis下配置数据读写分离及事务(四)

接 Spring+Struts+ibatis下配置数据读写分离及事务(三)

10.配置log4j.properties,方便打印ibatis执行信息

# Global logging configuration
log4j.rootLogger=ERROR, stdout,Rlogfile

# SqlMap logging configuration
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG

log4j.logger.org.springframework=ERROR
log4j.logger.org.apache.struts=ERROR
log4j.logger.org.apache.cxf=ERROR

# Console output
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p [%t] - %m%n

#logfile
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.DatePattern=yyyy-MM-dd
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile.File=${webRoot}/logs/myweb.log
log4j.appender.logfile.MaxFileSize=2048KB

#Rlogfile
log4j.appender.Rlogfile=org.apache.log4j.RollingFileAppender
log4j.appender.Rlogfile.layout=org.apache.log4j.PatternLayout
log4j.appender.Rlogfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.Rlogfile.File=${webRoot}/logs/myweb.log
log4j.appender.Rlogfile.MaxFileSize=2048KB
log4j.appender.Rlogfile.MaxBackupIndex=10 

 11.配置Struts对应的action的配置(配置Struts+Json并实现了页面跳转)

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="system" namespace="/" extends="json-default">
        <action name="admin" class="adminAction">
            <result type="json" name="ajaxSuccess">
                <param name="root">result</param>
            </result> 
            <result name="success" type="dispatcher">index.jsp</result>   
       </action>
   </package>
</struts>    

 

 12.配置struts.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.objectFactory" value="spring" /> 
	<constant name="struts.objectFactory"
		value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
    
    <!--导入后台系统Struts配置-->
    <include file="config/struts/bg/struts-system.xml"></include>
    <!--导入前台系统Struts配置-->
</struts>    

 

 13.配置web.xml<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:config/spring.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!--系统启动初始化BEGIN-->
	<listener>
		<listener-class>com.ssi222.wilr.util.listener.SystemInitListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
    <!--系统启动初始化END-->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>struts-default.xml,struts-plugin.xml,/config/struts.xml</param-value>
		</init-param>
	</filter>
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<display-name></display-name>
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 以上配置中还涉及到了一些java文件,如SystemInitListener,这个只是做测试,所以没有做什么操作,各位可以自己根据需求去写,SystemInitListener代码如下:

 

package com.ssi222.wilr.util.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SystemInitListener implements ServletContextListener {
	private static Log log = LogFactory.getLog(SystemInitListener.class);
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {


	}

	@Override
	public void contextInitialized(ServletContextEvent sce){
		this.SystemStarup(sce.getServletContext());
	}
	
	public void SystemStarup(ServletContext servletContext){
		log.info("系统启动初始化...");
		log.info("系统正在初始化服务容器...");
		try {
			new ClassPathXmlApplicationContext(new String[] { "config\\spring.xml" });
		} catch (Exception ex) {
			log.error("初始化服务容器发生错误,请仔细检查您的配置文件!\n" + ex.getMessage());
			ex.printStackTrace();
			System.exit(0);
		}
	}

}

 涉及到的Action如下:

 

public class AdminAction extends ActionSupport {
	private IAdminService adminService;
	private AdminInfo admin;
	private String result;
	public AdminInfo getAdmin() {
		return admin;
	}
	public void setAdmin(AdminInfo admin) {
		this.admin = admin;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	public void setAdminService(IAdminService adminService) {
		this.adminService = adminService;
	}

	public String login() {
		AdminInfo adminInfo=adminService.checkAdminLogin(admin.getAccount());
		Map<String, String> map=new HashMap<String, String>();
		if(adminInfo==null)
			map.put("msg","您输入的用户名不存在");
		else
		{
			if(Utils.validateStrIsEquals(adminInfo.getPassword(), new MD5().getMD5ofStr(admin.getPassword())))
				map.put("msg", "success");
			else
				map.put("msg", "您输入的密码错误");
		}
		result =Utils.getJson(map, "obj");
		return "ajaxSuccess";
	}
	public String index(){
		return "success";
	}
}

  涉及的service层相关代码如下:

 /**

 * @author Wilr
 * @function 基础业务模型接口
 */
public interface IBaseService {

}
/**
 * @author Wilr
 * @function 业务基础模型实现基类
 */
public class BaseServiceImpl implements IBaseService {
	protected IPresentation presentation;

	public void setPresentation(IPresentation presentation) {
		this.presentation = presentation;
	}

}
/**
 * @author Wilr
 * @function 管理员业务层接口
 */
public interface IAdminService {
	  /**
	   * @author Wilr
	   * @param loginAccount 登录账号(account/email)
	   * @function 检查用户登录
	   * @return 符合条件的用户
	   */
	  public AdminInfo checkAdminLogin(String account);
}
/**
 * @author Wilr
 * @function 管理员业务层接口实现
 */
public class AdminServiceImpl extends BaseServiceImpl implements IAdminService {
    private static final String NAMESPACE="System"; //对应Ibatis中配置的命名空间
	@Override
	public AdminInfo checkAdminLogin(String account) {
		return (AdminInfo)presentation.queryForObject(NAMESPACE+".loadLoginAdmin",account);
	}

}

   jsp页面采用AJAX提交并跳转页面的javascript代码清单如下:

function errorShow(msg,tagart){
			   $('#username').removeClass('username').addClass('errorusername');
			   $('#msgTip>div').text(msg);
			   $('#msgTip').show();
			   $(tagart).focus();
			}
			$(document).ready(function(){
			          $('#account').focus();
				 	  $('#login').bind("click",function(){
				 	      var $account=$('#account').val();
				 	      var $password=$('#password').val();
				 	     if($account.length<=0){
				 	        errorShow('请输入用户名','#account');
				 	        return false;
				 	     }
				 	     if($password.length<=0){
				 	      errorShow('请输入密码','#password');
				 	      return false;
				 	     }
				 	     $.post("admin!login",{"admin.account":$account,"admin.password":$password},function(data){
				 	        var $result=$.parseJSON(data).msg;
				 	        if($result=="success")
				 	            window.location.href = "admin!index";    
				 	         else
				 	           errorShow($result,'#account');
				 	     });
				 	 });
			});
 

   涉及到的javabean类我就不列举清单了,涉及的数据持久层请看我的博客:

 

http://wilr.iteye.com/admin/blogs/1189330(浅谈Spring+Struts+Ibatis项目结构下的持久层优化)

整个整合配置过程到此全部结束,有写的不好的地方大家多给点意见和多多包涵,我在继续优化,也方便小弟近一步提高,

小弟在此先行谢过。

 

 

StrutsSpringiBatis集成实战教程 本文还有配套的精品资源,点击获取 简介:本教程将引导开发者深入理解并掌握StrutsSpringiBatis三大Java开源框架的集成应用,覆盖MVC架构、依赖注入、事务管理以及数据访问层的高效实现。文档详细描述了从环境准备到运行测试的全过程,助力开发者构建企业级Java Web应用。 1. Struts框架及其MVC模式实现 1.1 Struts框架概... 阅读详情

相关推荐

Spring+Struts+ibatis配置数据读写分离事务()

NULL 博文链接:https://wilr.iteye.com/blog/1190524

MyBatis的读写分离与负载均衡

1.背景介绍 在现代互联网应用中,数据库性能和可用性是非常重要的。为了提高数据库性能和可用性,我们通常需要采用读写分离和负载均衡等技术。本文将详细介绍MyBatis的读写分离与负载均衡技术,并提供一些最佳实践和实际应用场景。 1. 背景介绍 MyBatis是一个流行的Java数据库访问框架,它可以用于简化数据库操作,提高开发效率。在实际应用中,MyBatis的性能和可用性是非常重要的。为了提...

AI天才研究院 881

spring+ibatis实现读写分离(分享我的开源项目)

先吐槽下博客园,每天都推荐水帖不说,正经的分享技术的博客还他妈的不让上首页,我在那里投入了那么多汗水,什么垃圾东西。spring+ibatis实现读写分离 特点 无缝结合spring+ibatis,对于程序员来说,是透明的 除了修改配置信息之外,程序的代码不需要修改任何东西 支持spring的容器事务 规则:基于spring配置的容器事务 读写事务到主库 只读事务到从库 如果没有配置事务,更

tangyanbo1110的专栏 2078

采用aop进行数据读写分离

采用aop进行数据读写分离 转自: https://mp.weixin.qq.com/s__biz=MzA5NzgzODI5NA==&mid=2454036042&idx=2&sn=723a3855a586237938f256f7267703f4&chksm=872b8bf3b05c02e52b56b9f2bd46b6e1a6656fba41942b6fd9a0...

lwy572039941的博客 1100

struts+spring+ibatis+velocity整合配置

这几天学了下struts+spring+ibatis+velocity的框架组合。弄了个Blog的小程序,基本上算就简的了,略记之。高级的东东还在学习中。。。1、web.xml的配置xml version="1.0" encoding="UTF-8"?>web-app xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="h

hgs_renyao的专栏 3837

Spring+Struts+ibatis配置数据读写分离事务()

Spring+Struts+ibatis配置数据读写分离事务()  6.配置spring-transaction.xml(事务管理)   &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="htt...

wilr笔记空间 236

SSIstrutsspringibatis三者的整合过程

经过半个月的学习,在理解了strutsspringibatis的理论及流程后,总结一下SSI对该三者的整合过程 1.业务产生(比如查找) 2.查找struts.xml文件找到处理添加业务的Action类(如selectAction)(此时为struts框架) 3.根据selectAction中的execute方法,通知相应的DAO类(如userDAO)进行查找操作(进入ibatis框架)

===小虫=== 1802

struts2 + spring + ibatis

一、导入struts2支持     1、导入struts2所需要的包:          commons-fileupload-1.2.1.jar          commons-logging-1.0.4.jar          freemarker-2.3.15.jar          ognl-2.7.3.jar          struts2-core-2.1.8.1.jar   

Fruits Boy 1041

ibatis+spring+struts 环境配置

ibatis+spring+struts环境配置 步骤: 一、新建工程,添加ibatisspringstruts的jars; 二、数据库设计. 三、POJO类. 、pojo类配置文件编写 五、ibatis配置文件编写【主配置文件只剩下sqlMap了】 六、dao接口和接口实现 七、service接口和接口实现 八、struts action实现及struts-config.xml

morning2008的专栏 393

Spring+Struts+ibatis配置数据读写分离事务()

Spring+Struts+ibatis配置数据读写分离事务() : 5.配置配置spring-datasource.xml(可以根据需求自己定义名字,或者写在spring配置文件中) &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/sch...

wilr笔记空间 202

spring+struts+ibatis

<br />原来的系统里面只采用了struts的框架,并且没有使用struts的校验功能,为方便开发,修改框架为spring+struts+ibatis组合<br />1、添加需要的jar文件<br />2、添加spring配置文件applicationContext.xml,内容如下<br /><?xml version="1.0" encoding="gb2312"?><br /><beans xmlns="http://www.springframework.org/schema/beans"<br

Jack 534

Struts2+Spring+Ibatis集成合并

上一篇博客讲述了Struts2+Spring的集成合并,主要是利用了一个中间jar包,这篇博客在加上Ibatis持久层框架,三个框架进行合并。其中Struts2和Spring部分和前边的一样,主要是讲解SpringIbatis之间的合并,这里也涉及到Spring的AOP编程思想,声明式事务的使用。            一,看一下分工吧:                   Struts

学会改变自己——才能突破 2万+

搭建springstruts、jpa(hibernate)ibatis框架详解

搭建springstruts、jpa(hibernate)ibatis框架详解1.本文选用spring3.06发行版,首先下载spring3.0.6版的jar包; 2.下载struts2.3.24版本,struts2的jar可以在struts2官网下载完整的source demo,里面包含了struts2整合spring的demo; 3.下载ibatis2.3.4的jar; 4.sprin

斑斑的博客 833

struts+spring+ibatis示例(附:源代码)

 下面这篇文章介绍的是struts+spring+ibatis示例,笔者将从他们的配置直到部署都尽可能的介绍清楚:1  首先创建数据库(以oracle为例),数据库脚本如下:CREATE TABLE EMPLOYEE(EMPLOYEEID INTEGER NOT NULL PRIMARY KEY,FIRSTNAME VARCHAR(256),LASTNAME VARCHAR(256),

xiaojunhu的专栏 4231

Spring2.5、Struts2、Ibatis开发框架搭建

一、框架下载 1.1  Struts2框架 Struts2框架发展于WebWork,现在捐献给了Apache开源组织,最新版本的Struts2框架可以从位于Apache官方网站的Struts2项目中获取,Struts2框架的项目主页地址为:http://struts.apache.org/,下载页面地址为:http://struts.apache.org/download.cgi ,打开下载

xiaotom5的专栏 938

(Struts2,Spring,Ibatis)初步整合理解

先来点文字性的描述: MVC对于我们来说,已经不陌生了,它起源于20世纪80年代针对smalltalk语言的一种软件设计模式,现在已被广泛应用。近年来,随着java的盛行,MVC的低耦合性、高重用性、可维护性、软件工程的可管理性等诸多优点使其在java平台中很受欢迎,其间,也诞生了许多优秀的MVC框架,如专注于控制层的Struts、WebWork, Struts2, JSF等框架,专注于

smithdoudou88的专栏 840

Strtus2、SpringiBatis配置

MVC 对于我们来说,已经不陌生了,它起源于20世纪80年代针对smalltalk语言的一种软件设计模式,现在已被广泛应用。近年来,随着java的盛行,MVC的低耦合性、高重用性、可维护性、软件工程的可管理性等诸多优点使其在java平台中很受欢迎,其间,也诞生了许多优秀的MVC框架,如专注于控制层的Struts、 WebWork, Struts2, JSF等框架,专注于业务逻辑方面的Sp...

abc12345670899的博客 137
上一篇: Spring+Struts+ibatis下配置数据读写分离及事务(三)
下一篇: IBM入职4道面试题(附答案)
wang153281
博客等级 码龄18年 0粉丝 11原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值