标签:简化 document 识别 开始 ber 业务逻辑 javaweb was for
package org.javaresearch.log4j;
import org.apache.log4j.*;
public class TestLog4J {   
static Logger log = Logger.getLogger(TestLog4J.class.getName());  
 public static void main(String args[]) {	    // logging的各种方法	  
  log.debug("Start of main()");  
 }
}
关于Log4J比较全面的配置### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{2}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ###
log4j.rootLogger=info, stdout ,file
Log4JTestpackage cn.test.log4j;
import org.apache.log4j.Logger;
import org.junit.Test;
/**
 * 日志记录的类:
 *
 */
public class Log4JTest {
	
	private Logger logger = Logger.getLogger(Log4JTest.class);
	
	@Test
	public void demo1(){
		logger.fatal("致命错误");
		logger.error("普通错误");
		logger.warn("警告信息");
		logger.info("普通信息");
		logger.debug("调试信息");
		logger.trace("堆栈信息");
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<!-- 配置类 和数据表 对应关系 -->
	<class name="cn.itcast.domain.Customer" table="customer" select-before-update="true">
		 <!-- 配置哪个属性 关联数据表主键 -->
		 <id name="id" column="id" type="integer">
		 	<!-- 主键生成策略 -->
		 	<generator class="identity"></generator>
		 </id>
		 <!-- 普通属性 -->
		 <property name="name" column="name" type="string"></property>
		 <!-- 如果属性名和列名相同 可以省略 column -->
		 <property name="age" type="integer" ></property>
		 <!-- 类型也可以使用默认生成规则,省略type -->
		 <property name="city"></property>
		 <property name="info"></property>
	</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<!-- 会话连接工厂,建立数据库连接需要SessionFactory -->
	<session-factory>
		<!-- JDBC连接基本参数 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///hibernatetest</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123</property>
		<!-- 配置数据库方言,便于生成一些与数据库相关SQL方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
		<!-- 可以根据需要自动创建数据表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 将SQL语句 输出到控制台 -->
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		
		<!-- JavaEE6 使用BeanValidator校验,需要设置校验模式-->
<!-- 		<property name="javax.persistence.validation.mode">none</property> -->
		
		<mapping resource="cn/itcast/domain/Customer.hbm.xml"></mapping>
	</session-factory>
</hibernate-configuration>
package cn.itcast.hibernate3.demo1;
/**
 * 实体类对象
 *
 */
public final class Customer {
	private Integer id;
	//private String id;
	private String name;
	private int age;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Customer [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
}
HibernateTest1package cn.itcast.hibernate3.demo1;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;
/**
 * Hibernate入门案例的测试:
 *
 *
 */
public class HibernateTest1 {
	
	@Test
	// 查询所有记录:SQL
	public void demo7(){
		// 1.加载核心配置文件
		Configuration configuration = new Configuration().configure();
		// 2.构建Session工厂
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		// 3.通过工厂创建Session
		Session session = sessionFactory.openSession();
		// 4.开启事务
		Transaction tx = session.beginTransaction();
		
		// 5.操作
		// 查询所有:SQL
		/*SQLQuery query = session.createSQLQuery("select * from customer");
		List<Object[]> list = query.list();
		
		for (Object[] objs : list) {
			System.out.println(Arrays.toString(objs));
		}*/
		SQLQuery query = session.createSQLQuery("select * from customer");
		query.addEntity(Customer.class);
		List<Customer> list = query.list();
		for (Customer customer : list) {
			System.out.println(customer);
		}
		
		// 6.事务提交
		tx.commit();
		// 7.释放资源
		session.close();
		sessionFactory.close();
		
	}
	
	@Test
	// 查询所有:QBC
	public void demo6(){
		// 1.加载核心配置文件
		Configuration configuration = new Configuration().configure();
		// 2.构建Session工厂
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		// 3.通过工厂创建Session
		Session session = sessionFactory.openSession();
		// 4.开启事务
		Transaction tx = session.beginTransaction();
		
		// 5.操作:
		// 查询所有 :QBC.
		/*Criteria criteria = session.createCriteria(Customer.class);
		List<Customer> list = criteria.list();*/
		Criteria criteria = session.createCriteria(Customer.class);
		criteria.add(Restrictions.eq("name", "凤姐"));
		List<Customer> list = criteria.list();
		
		for (Customer customer : list) {
			System.out.println(customer);
		}
		
		// 6.事务提交
		tx.commit();
		// 7.释放资源
		session.close();
		sessionFactory.close();
	}
	
	@Test
	// 查询所有:HQL.
	// HQL:Hibernate Query Language.Hibernate查询语言.面向对象的查询.
	public void demo5(){
		// 1.加载核心配置文件
		Configuration configuration = new Configuration().configure();
		// 手动编码加载映射文件:
		// configuration.addResource("cn/itcast/hibernate3/demo1/Customer.hbm.xml");
		// configuration.addClass(Customer.class);
		// 2.构建Session工厂
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		// 3.通过工厂创建Session
		Session session = sessionFactory.openSession();
		// 4.开启事务
		Transaction tx = session.beginTransaction();	
		
		// 5.操作
		// 1.查询所有的客户
		/*Query query = session.createQuery("from Customer");
		List<Customer> list = query.list();*/
		// 2.按名称查询
		/*Query query = session.createQuery("from Customer where name = ?");
		query.setParameter(0, "苍老师");*/
		Query query = session.createQuery("from Customer where name = :aaa");
		query.setParameter("aaa", "苍老师");
		List<Customer> list = query.list();
		
		for (Customer customer : list) {
			System.out.println(customer);
		}
		
		// 6.事务提交
		tx.commit();
		// 7.释放资源
		session.close();
		sessionFactory.close();
	}
	
	@Test
	// 删除记录
	public void demo4(){
		// 1.加载核心配置文件
		Configuration configuration = new Configuration().configure();
		// 2.构建Session工厂
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		// 3.通过工厂创建Session
		Session session = sessionFactory.openSession();
		// 4.开启事务
		Transaction tx = session.beginTransaction();	
		
		// 5.操作
		// 删除记录有两种方式:
		// 5.1手动创建对象的方式
		/*Customer customer = new Customer();
		customer.setId(2);
		
		session.delete(customer);*/
		
		// 5.2先查询在删除的方式
		Customer customer = (Customer)session.get(Customer.class, 1);
		session.delete(customer);
		
		// 6.事务提交
		tx.commit();
		// 7.释放资源
		session.close();
		sessionFactory.close();
	}
	
	@Test
	// 修改记录
	public void demo3(){
		// 1.加载核心配置文件
		Configuration configuration = new Configuration().configure();
		// 2.构建Session工厂
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		// 3.通过工厂创建Session
		Session session = sessionFactory.openSession();
		// 4.开启事务
		Transaction tx = session.beginTransaction();	
		
		// 5.操作:
		// 修改记录:两种方式可以进行修改.
		// 5.1手动创建对象的方式
		/*Customer customer = new Customer();
		customer.setId(2);
		customer.setName("苍老师");
		
		session.update(customer);*/
		
		// 5.2先查询在修改的方式
		Customer customer = (Customer) session.get(Customer.class, 1);
		customer.setName("凤姐");
		
		session.update(customer);
		
		// 6.事务提交
		tx.commit();
		// 7.释放资源
		session.close();
		sessionFactory.close();
	}
	
	@Test
	// 按id进行查询
	// (*****面试题)get和load方法区别
	public void demo2(){
		// 1.加载核心配置文件
		Configuration configuration = new Configuration().configure();
		// 2.构建Session工厂
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		// 3.通过工厂创建Session
		Session session = sessionFactory.openSession();
		// 4.开启事务
		Transaction tx = session.beginTransaction();
		// 5.操作
		// 根据id进行查询:
		// get方法进行查询
		Customer customer = (Customer) session.get(Customer.class, 100); // 马上发生一条SQL进行查询
		
		System.out.println(customer);
		
		// load方法进行查询
		//Customer customer = (Customer) session.load(Customer.class, 100); // 没有发送SQL
		
		//System.out.println(customer);// 发送SQL.
		
		// 6.事务提交
		tx.commit();
		// 7.释放资源
		session.close();
		sessionFactory.close();
		
	}
	
	@Test
	// 保存记录
	public void demo1(){
		// 1.Hiberante框架加载核心配置文件(有数据库连接信息)
		Configuration configuration = new Configuration().configure();
		// 2.创建一个SessionFactory.(获得Session--相当连接对象)
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		// 3.获得Session对象.
		Session session = sessionFactory.openSession();
		// 4.默认的情况下,事务是不自动提交.
		Transaction tx = session.beginTransaction();
		// 5.业务逻辑操作
		
		// 向数据库中插入一条记录:
		Customer customer = new Customer();
		customer.setName("苍老师");
		customer.setAge(38);
		
		session.save(customer);
		
		// 6.事务提交
		tx.commit();
		// 7.释放资源
		session.close();
		sessionFactory.close();
	}
}
Customer.hbm.xml<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入约束 -->
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
<hibernate-mapping>
	<!-- 建立类与表的映射 -->
	<!-- class标签:用于映射类与表的关系 name :类的全路径  table:表名称 -->
	<class name="cn.itcast.hibernate3.demo1.Customer" table="customer">
		<!-- 建立类中属性与表中的字段映射 -->
		<!-- 唯一标识 -->
		<!-- 使用id的标签 配置唯一属性 -->
		<!-- 在<id>标签中配置一个主键的生成策略. -->
		<id name="id" column="id">
			<generator class="assigned"/>
		</id>
		
		<!-- 普通属性 -->
		<!-- property标签:映射类中的普通属性 name:类中的属性名称, column:表中字段名称 -->
		<!-- 
			type:三种写法
				* Java类型		:java.lang.String
				* Hibernate类型	:string
				* SQL类型		:不能直接使用type属性,需要子标签<column>
					* <column name="name" sql-type="varchar(20)"/>
		 -->
		<property name="name" column="name" type="string" length="20"/>
		<property name="age" column="age"/>
		
		
	</class>
	
</hibernate-mapping>
hibernate.cfg.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
	<!-- 必须去配置的属性 -->
	<!-- 配置数据库连接的基本信息: -->
	<property name="hibernate.connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="hibernate.connection.url">
		jdbc:mysql:///hibernate3_day01
	</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">123</property>
	<!-- Hibernate的方言 -->
	<!-- 生成底层SQL不同的 -->
	<property name="hibernate.dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
	<!-- 可选的属性 -->
	<!-- 显示SQL -->
	<property name="hibernate.show_sql">true</property>
	<!-- 格式化SQL -->
	<property name="hibernate.format_sql">true</property>
	
	<property name="hibernate.connection.autocommit">false</property>
	<!-- hbm:映射 to DDL: create drop alter -->
	<property name="hibernate.hbm2ddl.auto">update</property>
	<!-- C3P0连接池设定-->
	<!-- 使用c3po连接池  配置连接池提供的供应商-->
	<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider                                                                                                                                                     </property>
	
	<!--在连接池中可用的数据库连接的最少数目 -->
	<property name="c3p0.min_size">5</property>
	<!--在连接池中所有数据库连接的最大数目  -->
	<property name="c3p0.max_size">20</property>
	<!--设定数据库连接的过期时间,以秒为单位,
	如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
	<property name="c3p0.timeout">120</property>
	 <!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
	<property name="c3p0.idle_test_period">3000</property>
	<!-- 通知Hibernate加载那些映射文件 -->
	<mapping resource="cn/itcast/hibernate3/demo1/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>log4j.properties### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{2}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ###
log4j.rootLogger=info, stdout ,file
package cn.itcast.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
 * Hibernate抽取工具类
 * 
 *
 */
public class HibernateUtils {
	private static Configuration configuration;
	private static SessionFactory sessionFactory;
	
	static{
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
	}
	
	public static Session openSession(){
		return sessionFactory.openSession();
	}
	
	public static void main(String[] args) {
		openSession();
	}
}package cn.itcast.hibernate3.demo1;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;
import cn.itcast.utils.HibernateUtils;
/**
 * 抽取了Hibernate的工具类的使用
 *
 */
public class HibernateTest2 {
	@Test
	// 保存数据
	public void demo1(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer customer = new Customer();
		customer.setName("芙蓉");
		customer.setAge(26);
		
		session.save(customer);
		
		tx.commit();
		session.close();
	}
	
	@Test
	// 保存或更新()
	public void demo2(){
		// 获得SEssion
		Session session = HibernateUtils.openSession();
		// 开启事务
		Transaction tx = session.beginTransaction();
		
		/*Customer customer = new Customer();
		customer.setName("冠希");
		customer.setAge(34);
		
		session.saveOrUpdate(customer);*/
		
		Customer customer = new Customer();
		customer.setName("冠希");
		customer.setAge(34);
		
		session.saveOrUpdate(customer);
		
		System.out.println(tx.wasCommitted());
		
		// 事务提交
		tx.commit();
		
		System.out.println(tx.wasCommitted());
		// session关闭
		session.close();
	}
	
	@Test
	// 保存数据
	public void demo3(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer customer = new Customer();
		customer.setName("芙蓉");
		customer.setAge(26);
		
		Serializable id = session.save(customer);
		
		// session.get(Customer.class, id);
		
		session.update(customer);
		
		tx.commit();
		session.close();
	}
	
	@Test
	// 测试程序
	public void demo4(){
		Session session = HibernateUtils.openSession();
		
		Customer customer = new Customer();
		customer.setName("关羽");
		customer.setAge(26);
		
		session.save(customer);
		
		int d = 10 / 0;
		
		Customer customer2 = new Customer();
		customer2.setName("张飞");
		customer2.setAge(26);
		
		session.save(customer2);
		
		session.close();
	}
	
	@Test
	// HQL:
	public void demo5(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		// 1.简单查询
		// List<Customer> list = session.createQuery("from Customer").list();
		
		// 2.条件查询:
		// List<Customer> list = session.createQuery("from Customer where name = ?").setParameter(0, "芙蓉").list();
		
		// 3.分页查询:select * from customer limit a,b; a:从哪开始  b:每页显示记录数.
		Query query = session.createQuery("from Customer");
		query.setFirstResult(3);
		query.setMaxResults(3);
		
		List<Customer> list = query.list();
		for (Customer customer : list) {
			System.out.println(customer);
		}
		
		tx.commit();
		session.close();
	}
	
	@Test
	// QBC:
	public void demo6(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		// 1.简单查询
		//List<Customer> list = session.createCriteria(Customer.class).list();
		
		// 2.条件查询:
		/*Criteria criteria = session.createCriteria(Customer.class);
		criteria.add(Restrictions.eq("name","芙蓉"));
		List<Customer> list = criteria.list();*/
		
		// 3.分页查询:
		Criteria criteria = session.createCriteria(Customer.class);
		criteria.setFirstResult(3);
		criteria.setMaxResults(3);
		List<Customer> list = criteria.list();
		for (Customer customer : list) {
			System.out.println(customer);
		}
		
		tx.commit();
		session.close();
	}
	
	@Test
	// 演示错误:(注意:)
	public void demo7(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer customer = (Customer) session.get(Customer.class, 10);
		
		Customer customer2 = new Customer();
		customer.setId(10);
		customer.setName("张飞");
		session.update(customer2);
		
		tx.commit();
		session.close();
	}
	
	@SuppressWarnings("unused")
	@Test
	// 演示持久化类为final情况
	public void demo8(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer customer = (Customer) session.load(Customer.class, 10);
		
		tx.commit();
		session.close();
	}
}
HibernateTest3package cn.itcast.hibernate3.demo1;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.itcast.utils.HibernateUtils;
/**
 * 主键生成策略
 *
 */
public class HibernateTest3 {
	
	@Test
	// 演示increment的问题:
	public void demo1(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer customer = new Customer();
		customer.setName("芙蓉");
		customer.setAge(26);
		
		session.save(customer);
		
		tx.commit();
		session.close();
	}
	
	@Test
	// 演示increment的问题:
	public void demo2(){
		Session session = HibernateUtils.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer customer = new Customer();
		// customer.setId(100);
		customer.setName("凤姐");
		customer.setAge(26);
		
		session.save(customer);
		
		tx.commit();
		session.close();
	}
}
Log4JTestpackage cn.itcast.log4j;
import org.apache.log4j.Logger;
import org.junit.Test;
/**
 * 日志记录的类:
 */
public class Log4JTest {
	
	private Logger logger = Logger.getLogger(Log4JTest.class);
	
	@Test
	public void demo1(){
		logger.fatal("致命错误");
		logger.error("普通错误");
		logger.warn("警告信息");
		logger.info("普通信息");
		logger.debug("调试信息");
		logger.trace("堆栈信息");
	}
}
JAVAWEB开发之Hibernate详解(一)——Hibernate的框架概述、开发流程、CURD操作和核心配置与API以及Hibernate日志的使用
标签:简化 document 识别 开始 ber 业务逻辑 javaweb was for
原文地址:http://blog.csdn.net/u013087513/article/details/65630291