spring2.5事务&aop (1)

上一篇 / 下一篇  2008-05-16 23:38:11 / 个人分类:spring

数据库使用MYSQL,创建数据库

create table customer ( id int(11) not null auto-increment,

                                     username varchar(50) not null,

                                    password varchar(50) not null,

                                    unique key(username),

                                    primary key(id)

)

 

之所以将username设置成unique,是方便之后让系统中抛出异常。

然后创建bean,就是所说的模型。

 

生成Customer类的封装文件,Customer.java

import java.io.Serializable;

 

public class Customer implements Serializable{

 

        /**

         *

         */

        private static final long serialVersionUID = 6281397281324007139L;

        

        private Integer id;

        private String username;

        private String passwrod;

        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 getPasswrod() {

                  return passwrod;

        }

        public void setPasswrod(String passwrod) {

                  this.passwrod = passwrod;

        }

        

}

 

如果说存在多张表,并且对应存在多对一的关系,并且使用Set为类型,则需要使用主键的toString()和HashCode()的方法。

 

 

为了方便使用日志文件,配置日志文件:

 

log4j.properties内容为:

 

log4j.rootLogger=INFO,stdout,D  

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

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

log4j.appender.stdout.layout.ConversionPattern=%d%5p(%F:%L)-%m%n

 

 

log4j.appender.D =org.apache.log4j.DailyRollingFileAppender

log4j.appender.D.File =d:/error.log

log4j.appender.D.Append =true ##日志的追加模式

log4j.appender.D.Threshold =ERROR##ERROR以上输出到日志文件中

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

log4j.appender.D.layout.ConversionPattern =%-d{yyyy-MM-ddHH:mm:ss} [%t:%r]-[%p] %m%n

 

日志将INFO以上的级别输出,将INFO以上级别的输出到控制台中,将ERROR以上的级别输出到日志文件中。

 

Logger文件:

publicclassLogger {

   privatestaticLoglog= LogFactory.getLog(Logger.class);

 

   publicvoidentry_info(String message) {

      log.info(message);

   }

 

   publicvoidentry_error(String message) {

      log.error(message);

   }

}

 

 

使用spring2.5theme模式,将数据源配置到applicationContext.xml文件中,需要使用到springaop  tx模式,因此根元素的配置如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<!--

   Application context definition for PetClinic on JDBC.

-->

<beansxmlns="http://www.springframework.org/schema/beans"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:p="http://www.springframework.org/schema/p"

   xmlns:aop="http://www.springframework.org/schema/aop"

   xmlns:context="http://www.springframework.org/schema/context"

   xmlns:jee="http://www.springframework.org/schema/jee"

   xmlns:tx="http://www.springframework.org/schema/tx"

   xsi:schemaLocation="

          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd

          http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

</beans>

 

配置数据源:

<bean id=”dataSource” class=” org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”>

   <property name=”username” value=”%username”/>

   <property name=”password ” value=”%password”/>

        <propertyname="driverClassName"

          value="org.gjt.mm.mysql.Driver">

      </property>

      <propertyname="url"

          value="jdbc:mysql://localhost:3306/test2">

      </property>

</bean>

<beanid="daoTemplate"abstract="true"lazy-init="true">

      <propertyname="dataSource">

          <reflocal="dataSource"/>

      </property>

</bean>

   


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-08-30  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 314
  • 日志数: 11
  • 建立时间: 2007-12-21
  • 更新时间: 2008-05-17

RSS订阅

Open Toolbar