标签:com 方言 log ref 问题 实现 resource interface director
1.导入maven节点 <dependencies>
//测试用的
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency>
//aop相关
<dependency> <groupId> org.aspectj</groupId > <artifactId> aspectjweaver</artifactId > <version> 1.8.7</version > </dependency>
//mysql驱动
<dependency> <groupId>org.wisdom-framework</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34_1</version> </dependency>
//c3p0数据源(选用)
<dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
//spring事务
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.3.RELEASE</version> </dependency>
//sqring核心
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.0.RELEASE</version> </dependency>
//hibernate核心
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.6.Final</version> </dependency>
//spring和hibernate整合相关
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version> 4.2.2.RELEASE</version> </dependency>
//测试
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies>
//用于解决idea读取不到非resource目录下配置文件问题
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources> </build>
2.建立分层
3.建立实体类Stuck
package cn.lhl.entity; /** * Created by Administrator on 2017/4/2/002. */
//股票类
public class Stuck {
//id private int sid;
//股票名 private String sname;
//持股数 private int scount; //相关geter,setter public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public int getScount() { return scount; } public void setScount(int scount) { this.scount = scount; } }
4.创建Stuck的映射文件Stuck.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.lhl.entity"> <class name="Stuck"> <id name="sid"> <generator class="native"></generator> </id> <property name="scount"/> <property name="sname"/> </class> </hibernate-mapping>
5.创建dao接口IStuckDao和实现类StuckDaoImpl
package cn.lhl.dao; import cn.lhl.entity.Stuck; /** * Created by Administrator on 2017/4/4/004. */ public interface IStuckDao { int addStuck(Stuck stuck); }
package cn.lhl.dao.impl; import cn.lhl.dao.IStuckDao; import cn.lhl.entity.Stuck; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Transactional; import java.io.Serializable; /** * Created by Administrator on 2017/4/4/004. */ public class StuckDaoImpl implements IStuckDao {
//创建session工厂 private SessionFactory sessionFactory;
//事务注解(执行该方法时打开事务) @Transactional
//添加股票的方法 public int addStuck(Stuck stuck) { Session session = sessionFactory.getCurrentSession(); Serializable save = session.save(stuck); return (Integer)save; } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
6.创建service层接口IStuckService和实现类StuckServiceImpl
package cn.lhl.service; import cn.lhl.entity.Stuck; /** * Created by Administrator on 2017/4/4/004. */ public interface IStuckService { int addStuck(Stuck stuck); }
package cn.lhl.service.impl; import cn.lhl.dao.IStuckDao; import cn.lhl.entity.Stuck; import cn.lhl.service.IStuckService; /** * Created by Administrator on 2017/4/4/004. */ public class StuckServiceImpl implements IStuckService { private IStuckDao iStuckDao; public int addStuck(Stuck stuck) { return iStuckDao.addStuck(stuck); } public IStuckDao getiStuckDao() { return iStuckDao; } public void setiStuckDao(IStuckDao iStuckDao) { this.iStuckDao = iStuckDao; } }
7.创建spring配置文件applicationContext.xml<?xml version="1.0" encoding="UTF-8"?<beans xmlns="http://www.springframework.org/schema/beans"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql:///gupiao" p:username="root" p:password="qq1684547081">
</bean>
<!--配置sessionFactory-->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--引入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--设置hibernate相关配置-->
<property name="hibernateProperties">
<props>
<!--sql方言-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<!--是否显示sql-->
<prop key="hibernate.show_sql">true</prop>
<!--设置于线程绑定的session-->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
<!--设置自动更新表结构-->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--扫描hibernate小配置-->
<property name="mappingDirectoryLocations" value="classpath:cn/lhl/entity"></property>
</bean>
<!--dao代理bean-->
<bean name="stuckDaoImpl" class="cn.lhl.dao.impl.StuckDaoImpl">
<!--植入sessionFactory-->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--service代理bean-->
<bean name="stuckService" class="cn.lhl.service.impl.StuckServiceImpl">
<!--植入dao-->
<property name="iStuckDao" ref="stuckDaoImpl"></property>
</bean>
<!--设置事务管理器-->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!--引入sessionFactory-->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--配置注解事务扫描器-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
8.创建测试类
package cn.lhl.test;
import cn.lhl.entity.Stuck;
import cn.lhl.service.impl.StuckServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2017/4/4/004.
*/
public class SSHTest {
@Test
public void add(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
StuckServiceImpl stuckService=(StuckServiceImpl) context.getBean("stuckService");
Stuck stuck=new Stuck();
stuck.setSname("龙骨");
stuck.setSname("1");
stuckService.addStuck(stuck);
}
}
整合完成
标签:com 方言 log ref 问题 实现 resource interface director
原文地址:http://www.cnblogs.com/www-datou-com/p/6665290.html