数据库使用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文件中,需要使用到spring的aop tx 模式,因此根元素的配置如下:
xml version="1.0" encoding="UTF-8"?>
<!--
Application context definition for PetClinic on JDBC.
-->
<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/