spring2.5事务&aop (1)

数据库使用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-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

 

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

 

Logger文件:

public class Logger {

    private static Log log = LogFactory.getLog(Logger.class);

 

    public void entry_info(String message) {

       log.info(message);

    }

 

    public void entry_error(String message) {

       log.error(message);

    }

}

 

 

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

xml version="1.0" encoding="UTF-8"?>

<!--

    Application context definition for PetClinic on JDBC.

--&gt

<beans xmlns="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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 

配置数据源:

   

   

         <property name="driverClassName"

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

       property>

       <property name="url"

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

       property>

<bean id="daoTemplate" abstract="true" lazy-init="true">

       <property name="dataSource">

           <ref local="dataSource" />

       property>

bean>

   

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7528999/viewspace-274338/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7528999/viewspace-274338/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值