标签:cto use classpath let override 引入 集合 this 版本
一点点总结Spring整合Hibernate Day1
IDE: IntelliJ IDEA 2017
数据库:MySql
另外加入的的jar包:
1.用于Spring AOP需要的jar包:aspectjrt.jar, aspectjweaver.jar (此项可以省略!)
2.用于spring-config.xml配置数据源需要的jar包:mysql-connector-java-5.0.8-bin.jar,commons-pool-1.6.jar, commons-dbcp-1.4.jar
3.用于Hibernate调试信息需要的jar包:log4j-1.2.17.jar,slf4j-api-1.7.25.jar,slf4j-log4j12-1.7.25.jar(另外需要文件log4j.properties)
新建项目勾选Spring和Hibernate jar包由IDEA自动下载(笔者用:Spring-4.3.12.RELEASE和Hibernate 5.2.11-5.2.11)
各部分源码:
LogInterceptor.java类(用于AOP织入的类)
1 package com.pp.aop; 2 3 import org.aspectj.lang.annotation.Aspect; 4 import org.aspectj.lang.annotation.Before; 5 import org.springframework.stereotype.Component; 6 7 @Aspect 8 @Component 9 public class LogInterceptor { 10 @Before("execution(public void com.pp.model.Chinese.useAxe())") 11 public void before(){ 12 System.out.println("LogInterceptor, Test AOP!"); 13 } 14 }
Axe和Person接口
1 package com.pp.impl; 2 3 public interface Axe { 4 public String chop(); 5 }
1 package com.pp.impl; 2 3 public interface Person { 4 public void useAxe(); 5 }
Chinese.java类
1 package com.pp.model; 2 3 import com.pp.impl.Axe; 4 import com.pp.impl.Person; 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import javax.annotation.Resource; 8 9 public class Chinese implements Person { 10 private Axe axe; 11 private SessionFactory sessionFactory; 12 13 public SessionFactory getSessionFactory() { 14 return sessionFactory; 15 } 16 17 18 @Resource 19 public void setSessionFactory(SessionFactory sessionFactory) { 20 this.sessionFactory = sessionFactory; 21 } 22 23 24 @Resource //(name = "stoneAxe") 25 public void setAxe( Axe axe){ 26 this.axe = axe; 27 } 28 @Override 29 public void useAxe() { 30 System.out.println("sessionFactory.Class:"+sessionFactory.getClass()); 31 Session session = sessionFactory.openSession(); 32 session.beginTransaction(); //开始Transaction 33 session.save(new peopleInfo()); //数据存入数据库 34 session.getTransaction().commit(); //得到Transaction并提交 35 session.close(); //关闭连接 36 37 System.out.println("我打算去看点柴火!"); 38 System.out.println(axe.chop()); 39 } 40 }
peopleInfo.java类 (与数据库有相对应的表peopleInfo)
1 package com.pp.model; 2 3 import javax.persistence.*; 4 5 @Entity 6 @Table(name = "peopleInfo") 7 public class peopleInfo { 8 private int id; 9 private String name; 10 11 /** 12 * @Id 映射主键属性,这里采用uuid的主键生成策略 13 * @GeneratedValue —— 注解声明了主键的生成策略。该注解有如下属性 14 * strategy 指定生成的策略,默认是GenerationType. AUTO 15 * GenerationType.AUTO 主键由程序控制 16 * GenerationType.TABLE 使用一个特定的数据库表格来保存主键 17 * GenerationType.IDENTITY 主键由数据库自动生成,主要是自动增长类型 18 * GenerationType.SEQUENCE 根据底层数据库的序列来生成主键,条件是数据库支持序列 19 * generator 指定生成主键使用的生成器 20 */ 21 @Id 22 @GeneratedValue 23 @Column(name = "id") 24 public int getId() { 25 return id; 26 } 27 28 public void setId(int id) { 29 this.id = id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 }
StoneAxe.java类
1 package com.pp.model; 2 3 import com.pp.impl.Axe; 4 import org.springframework.stereotype.Component; 5 6 @Component 7 public class StoneAxe implements Axe { 8 @Override 9 public String chop() { 10 return "石斧砍柴好慢!"; 11 } 12 }
测试类:BeanTest.java类
1 package com.pp.test; 2 3 import com.pp.impl.Person; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class BeanTest { 8 public static void main(String args[]){ 9 ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml"); 10 Person p = ctx.getBean("chinese", Person.class); 11 p.useAxe(); 12 } 13 }
log4j.properties(自行百度copy即可)
# Root logger option log4j.rootLogger=INFO, console log4j.logger.com.ch.demo=INFO, console # Direct log messages to console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{HH:mm}| %p | %F %L | %m%n # direct messages to file hibernate.log # log4j.logger.org.hibernate=DEBUG, hibernate log4j.appender.hibernate=org.apache.log4j.RollingFileAppender log4j.appender.hibernate.File=hibernate.log log4j.appender.hibernate.layout=org.apache.log4j.PatternLayout log4j.appender.hibernate.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n # Hibernate主要的类别如下: # 1)org.hibernate.SQL # 日志输出所有Hibernate执行的SQL DML语句 log4j.logger.org.hibernate.SQL=debug # 2)org.hibernate.type # 日志输出所有的JDBC参数 # 3)org.hibernate.transaction # 日志输出所有活动相关的事务 # 4)org.hibernate.jdbc # 日志输出所有的JDBC资源采集 # 5)org.hibernate.tool.hbm2ddl # 日志输出所有Hibernate执行的SQL DDL语句 log4j.logger.org.hibernate.tool.hbm2ddl = Debug # 6)org.hibernate # 日志输出所有的Hibernate信息 # 如果指定日志输出类别为org.hibernate.SQL,那么将会输出SQL语句。但是,还有一种更简单的查看SQL语句的方法,只需简单地设置show_sql参数为true。
spring-config.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop.xsd"> 12 <context:annotation-config /> 13 <context:component-scan base-package="com.pp" /> 14 <aop:aspectj-autoproxy /> 15 16 <!--数据源的配置--> 17 <!--外加包:commons-pool, commons-dbcp, mysql-connector-java--> 18 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 19 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 20 <property name="url" value="jdbc:mysql://ip地址:端口号/ppsql" ></property> 21 <property name="username" value="root"></property> 22 <property name="password" value="root"></property> 23 </bean> 24 25 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 26 <!-- 注入datasource,给sessionfactoryBean内setdatasource提供数据源 --> 27 <property name="dataSource" ref="dataSource" /> 28 <property name="annotatedClasses"> <!--Hibernate关系映射的类集合--> 29 <list> 30 <value>com.pp.model.peopleInfo</value> <!-- 关系映射的类 --> 31 </list> 32 </property> 33 <property name="hibernateProperties"> <!--Hibernate配置文件中的设置参数--> 34 <props> 35 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <!--需要根据数据库版本来选择对于的Dialect--> 36 <prop key="hibernate.show_sql">true</prop> 37 <prop key="hibernate.format_sql">true</prop> 38 <prop key="hibernate.hbm2ddl.auto">update</prop> 39 </props> 40 </property> 41 </bean> 42 43 <bean id="chinese" class="com.pp.model.Chinese" scope="singleton" /> 44 45 </beans>
配置过程中遇到的问题点:
1.Error executing DDL via JDBC Statement 解:org.hibernate.dialect.*Dialect (*:要选择相对应的数据库版本)
2.通过JDBC初始化dataSource连接MySql需要引入包commons-pool-1.6(建议用pool而不是pool2),commons-dbcp-1.4,mysql-connector-java三个包
感觉自己还是没有总结的很好,以后继续努力加油!!!!!!!
标签:cto use classpath let override 引入 集合 this 版本
原文地址:http://www.cnblogs.com/seven1314pp/p/7728731.html