hibernate5
、spring5.1
、struts2.5
,不同版本配置可能会有所不同1.hibernate所需jar包
2.spring所需jar包
3.struts2所需jar包
可选
)database.properties具体配置文件如下
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=scott
jdbc.password=123456
将需要的配置文件都放置于一个文件夹中(resources)便于管理
编写POJO类
package pojo.user;
/**
* 用户类
*/
public class User {
private Integer id;
public String name;
private String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
}
编写DAO层
package dao.user;
import pojo.user.User;
import java.util.List;
/**
* 用户持久层
*/
public interface UserDao {
/**
* 根据id查询用户
* @param id
* @return
*/
public User getUserById(Integer id);
}
编写DaoImpl类
package dao.user;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import pojo.user.User;
import java.util.List;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
/**
* 根据id查询用户
*
* @param id
* @return
*/
@Override
public User getUserById(Integer id) {
return this.getHibernateTemplate().load(User.class,id);
}
}
编写service层
package service.user;
import pojo.user.User;
/**
* 用户业务逻辑层
*/
public interface UserService {
/**
*根据id查询用户
*/
public User login(String name, String password);
}
编写ServiceImpl类
package service.user;
import dao.user.UserDao;
import pojo.user.User;
public class UserServiceImpl implements UserService {
private UserDao ud;
@Override
public User getUserById(Integer id) {
return ud.getUserById(id);
}
}
编写applicationContext.xml
文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置dataSource-->
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" scope="singleton">
<property name="username" value="${jdbc.username}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
</bean>
<!--配置sessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--配置hibernate属性-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 单个xml配置:mappingResources , 整包配置:mappingDirectoryLocations -->
<property name="mappingDirectoryLocations">
<list>
<value>/pojo</value>
</list>
</property>
</bean>
<!--配置dao-->
<bean id="userDao" class="dao.user.UserDaoImpl">
<!--方式一:直接注入sessionFactory-->
<!--方式二:创建hibernateTemplate注入sessionFactory,在这里引用hibernateTemplate-->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--配置service-->
<bean id="userService" class="service.user.UserServiceImpl">
<property name="ud" ref="userDao"/>
</bean>
<!-- 定义事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--事务通知-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 定义事务属性,声明事务规则 -->
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 定义切入点 -->
<aop:pointcut id="serviceMethod" expression="execution(* service.*.*(..))" />
<!-- 将事务通知与切入点组合 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
</beans>
hibernateProperties
配置中的thread
hibernate
属性时key
值属性需要加上前缀hibernate
(切记切记!)编写测试类运行,检查hibernate
、spring
整合是否配置成功
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.user.User;
import service.user.UserService;
public class test {
@Test
public void test1(){
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserService userService= (UserService) context.getBean("userService");
User user=userService.getUserById(1001);
System.out.println(user.toString());
}
}
常见错误:
如果出现ORA-01017: invalid username/password; logon denied
错误
解决方案如下:
方法一:
database.properties
文件中的key
之前全加上jdbc.
,这样在读取属性时就不会与系统属性冲突jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=scott
jdbc.password=123456
applicationContext.xml
中的dataSource
注入改为: <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
方法二:
context:property-placeholder
的system-properties-mode
改为NONE
或者FALLBACK
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置spring-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--开启事务监听器-->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
/*
创建action
package action.user;
import com.opensymphony.xwork2.ActionSupport;
import service.user.UserService;
public class loginAction extends ActionSupport {
private UserService us ;
private String name;
private String password;
public UserService getUs() {
return us;
}
public void setUs(UserService us) {
this.us = us;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login() {
return us.login(name,password)==null?ERROR:SUCCESS;
}
}
在applicationContext.xml中加入action的配置
<!--配置action-->
<bean id="loginAction" class="action.user.loginAction" scope="prototype">
<property name="us" ref="userService"/>
</bean>
配置Struts2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="struts-default">
<!--此处的class为application中配置action的id名称-->
<action name="loginAction" class="loginAction" method="login">
<result name="success" >success.jsp</result>
<result name="error">login.jsp</result>
<result name="input">login.jsp</result>
</action>
</package>
</struts>
如果该文章有出错地方还望各位指出,希望能多多提出建议,我将会努力改进,谢谢!