SSH 坏境搭建(2)_添加Hibernate和Spring

        hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
		<!DOCTYPE hibernate-configuration PUBLIC
				"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
				"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
		<hibernate-configuration>
			<session-factory>
				<!-- 数据库连接信息 -->
				<!-- <property name="connection.url">jdbc:mysql://localhost:3306/ItcastOA</property>
				<property name="connection.username">root</property>
				<property name="connection.password">123</property>
				<property name="connection.driver_class">com.mysql.jdbc.Driver</property> -->
				<!-- Hibernate配置信息 -->
				<!-- dialect方言-->
				<property name="dialect">
					org.hibernate.dialect.MySQLDialect
				</property>
				<property name="javax.persistence.validation.mode">none</property> 
				
				<!-- 其他配置 -->
				<property name="hibernate.show_sql">true</property>
				<property name="hbm2ddl.auto">update</property>


				<!-- 在配置文件中关联映射文件 -->
				 <mapping resource="cn/itcast/oa/domain/User.hbm.xml" /> 
			</session-factory>
		</hibernate-configuration>

        User.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.itcast.oa.domain">

	<class name="User" table="t_user">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="loginName" />
		<property name="password" />
		<property name="name" />
		<property name="gender" />
		<property name="phoneNumber" />
		<property name="email" />
		<property name="description" />
		
		<!-- department属性,本类与Department的多对一 -->
		<many-to-one name="department" class="Department" column="departmentId"></many-to-one>
		
		<!-- roles属性,本类与Role的多对多-->
		<set name="roles" table="t_user_role">
			<key column="userId"></key>
			<many-to-many class="Role" column="roleId"></many-to-many>
		</set>
	</class>

</hibernate-mapping>


spring

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:tx="http://www.springframework.org/schema/tx"
			xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
						http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

			<!-- 自动扫描与装配bean -->
			<context:component-scan base-package="cn.ssh.test"></context:component-scan>	
		</beans>

在web.xml中添加

<!-- 配置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>


测试:1、在ActionTest中添加配置

@Controller
@Scope("prototype")
public class TestAction extends ActionSupport {
	
	@Override
	public String execute() throws Exception {
		
		return "SUCCESS";
	}

}

2、编写TestSpring.java

public class TestSpring {
	private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

	@Test
	public void testBean() throws Exception {
		TestAction testAction = (TestAction) ac.getBean("testAction");
		System.out.println(testAction);
	}

}

3、运行测试类,结果为

wKiom1OEc3OTlsmwAAFDxJHbQ4Q859.jpg

你可能感兴趣的:(spring,ssh,环境搭建)