① introducing c3p0 bag, spring-orm package, spring-tx package
② spring main configuration file applicationContext.xml introduced beans, tx, aop constraints
② spring configuration profiles; sessionFactory not configure the binding thread otherwise reported abnormal
<!-- 配置dataSource --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=true"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <!-- 配置sessionFactory --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.connection.isolation">4</prop> </props> </ Property > <-! Configuration ORM metadata automatically scans all the configuration files in the package -> < Property name = "mappingDirectoryLocations" value = "CLASSPATH: COM / sikiedu / Domain" > </ Property > < / bean >
③ injection layer was disposed Dao sessionFactory
<!-- 配置Dao --> <bean name="userDao" class="com.sikiedu.dao.UserDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
④ Dao layer HibernateDaoSupport class inheritance using getHibernateTemplate () for obtaining Session
package com.sikiedu.dao; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.query.NativeQuery; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.sikiedu.domain.User; public class UserDao extends HibernateDaoSupport { public User findUser(User user) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); // Session session = HibernateUtils.getCurrentSession(); String sql = "SELECT * FROM user WHERE username = ? AND password = ?"; NativeQuery<User> sqlQuery = session.createSQLQuery(sql); sqlQuery.setParameter(1, user.getUsername()); sqlQuery.setParameter(2, user.getPassword()); sqlQuery.addEntity(User.class); User temp = sqlQuery.uniqueResult(); return temp; } }
SpringAOP transaction configuration - HibernateTransactionManager
● applicationContext.xml Configuration
Configuration weaving: weaving the above notification to the target object
<! - configuration Hibernate core transaction manager - depends on the connection pool -> < the bean name = "the transactionManager" class = "org.springframework.orm.hibernate5.HibernateTransactionManager" > < Property name = "the sessionFactory" REF = " the sessionFactory " > </ Property > </ the bean > <-! configure the notification -> < TX: the advice ID =" the advice " Transaction Manager- =" the transactionManager " > < TX: Attributes > < TX:method name="*"/><! - * matches all methods -> </ TX: Attributes > </ TX: the advice > <! - Configuration weaving: weaving a notification to the target object -> < AOP: config > <! - Configuring pointcut expression ! -> <- .... com.sikiedu.service * * * (..) represents com.sikiedu * * all methods of all classes in the package; (..) parameter representation dispensable -> < AOP: the pointcut expression The = "Execution (com.sikiedu.service * * * (..)..)" ID = "the pointcut" />
<- configuration section (notification tangent point +! ) ->
< AOP: Advisor the advice-REF = "the advice" the pointcut-REF = "the pointcut" /> </aop:config>
● Configuring web.xml, to expand the scope of the Session
<filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
● service layer uses - aop enhanced automatic use transaction
package com.sikiedu.service; import com.sikiedu.dao.UserDao; import com.sikiedu.domain.User; public class UserService { private UserDao userDao; public User findUser(User user) { User temp = null; temp = userDao.findUser(user); return temp; } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }