日志
- slf4j 核心jar : slf4j-api-1.6.1.jar 。slf4j是日志框架,将其他优秀的日志第三方进行整合。
- 步骤:
- 导入jar包
- 导入配置文件log4j.properties
- log4j级别:fatal 致命错误 error 错误 warn 警告 info 信息 debug 调试信息 trace 堆栈信息 (由高到底顺序)
- 输出源:
例如:log4j.appender.file=org.apache.log4j.FileAppender
格式:log4j.appender.输出源的名称=输出源的实现类
名称:自定义
实现类:log4j提供 - 布局 – 确定输出格式
- 例如:log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- 格式:log4j.appender.数据源.layout=org.apache.log4j.PatternLayout
- 布局属性:log4j.appender. 数据源.layout.ConversionPattern=值
### 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{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=e\: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
一对一关系
-
一对一的两种方式:
-
创建步骤(使用外键)
-
创建实体类
-
配置文件
Address.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 package="com.lwb.domain">
<class name="com.lwb.domain.Address" table="t_address" >
<id name="id" column="id">
<!-- generator:主键生成策略 -->
<generator class="native"></generator>
</id>
<property name="name" column="name" type="string"></property>
<!-- unique:配置属性唯一 -->
<many-to-one name="company" class="Address" column="cid" unique="true"></many-to-one>
</class>
</hibernate-mapping>
Company.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 package="com.lwb.domain">
<class name="com.lwb.domain.Company" table="t_company" >
<id name="id" column="id">
<!-- generator:主键生成策略 -->
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<!--
配置一对一
one-to-one:默认使用主键同步策略完成一对一的表关系体现。
property-ref:指定company在一对一关联时,指向哪个属性
-->
<one-to-one name="address" class="Address" property-ref="company"></one-to-one>
</class>
</hibernate-mapping>
测试:
@Test
public void test1(){
Session session = HibernateUtils.openSession();
session.beginTransaction();
Company c = new Company();
c.setName("灰豚公司");
Address a = new Address();
a.setName("科技园");
a.setCompany(c);//维护关系
session.save(c);
session.save(a);
session.getTransaction().commit();
session.close();
}
- 创建步骤(主键同步)
- 配置文件:
Address.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 package="com.itheima.domain" >
<class name="Address" table="t_Address" >
<id name="id" column="id" >
<!-- foreign: 该主键既是主键又是外键 -->
<generator class="foreign">
<!-- 作为外键时引用哪个属性 -->
<param name="property">company</param>
</generator>
</id>
<property name="name" column="name" type="string" ></property>
<!-- 配置一对一关系 -->
<one-to-one name="company" class="Company" constrained="true" ></one-to-one>
</class>
</hibernate-mapping>
Company.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 package="com.itheima.domain" >
<class name="Company" table="t_Company" >
<id name="id" column="id" >
<generator class="native"></generator>
</id>
<property name="name" column="name" type="string" ></property>
<!-- 配置一对一
one-to-one : 默认使用主键同步策略完成一对一的表关系体现.
cascade:
fetch:
lazy:
-->
<one-to-one name="address" class="Address" > </one-to-one>
</class>
</hibernate-mapping>