springmvc-spring-hibernate整合

原创 2016年05月19日 14:05:43

第一步:创建maven项目.springmvc-spring-hibernate

第二步:引入坐标文件

	
	 <!-- 指定maven仓库 -->  
    <repositories><!-- 发现依赖和扩展的远程资源库 -->  
		<!-- ace maven仓库 -->
		<repository>
			<id>acesdk-repository</id>
			<name>Ace Sdk Repository</name>
			<url>http://acesdk.github.io/repo</url>
		</repository>
     <!--    阿里巴巴发布版本仓库   -->
        <repository>  
            <id>alibaba-opensource</id>  
            <name>alibaba-opensource</name>  
            <url>http://code.alibabatech.com/mvn/releases/</url>  
            <layout>default</layout>  
        </repository>  
       <!--  阿里巴巴快照版本仓库   -->
        <repository>  
            <id>alibaba-opensource-snapshot</id>  
            <name>alibaba-opensource-snapshot</name>  
            <url>http://code.alibabatech.com/mvn/snapshots/</url>  
            <layout>default</layout>  
        </repository>  
    </repositories>  
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!-- spring版本号 -->
		<spring.version>3.2.4.RELEASE</spring.version>
		<hibernate.version>4.2.5.Final</hibernate.version>
		<!-- log4j日志文件管理包版本 -->
		<!-- <slf4j.version>1.7.10</slf4j.version> -->
		<log4j.version>1.2.17</log4j.version>
	</properties>
	<!-- maven打成war包放到tomcat目录下,即可运行 -->
	<build>
		<finalName>PHN</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<!-- junit测试包 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
 
		<!-- servlet api jar -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>3.0-alpha-1</version>
			<scope>provided</scope>
		</dependency>
		<!-- jstl -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<!-- 文件上传 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.2.2</version>
		</dependency>
 
		<!-- fastJson json数组有关的 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.1.26</version>
		</dependency>
 
		<!-- 使用 Jackson 把 Java 对象转换成 JSON 字串 -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.11</version>
		</dependency>
		<!-- mysql驱动包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.29</version>
		</dependency>
		<!-- 阿里巴巴数据源包 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.2</version>
		</dependency>
		<!-- 日志文件管理包 -->
		<!-- log start -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<!-- <dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.version}</version>
		</dependency> -->
 
 
		<!-- aspectjweaver.jar这是Spring AOP所要用到的包 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.7.1</version>
		</dependency>
 
		<!-- hibernate4 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>4.2.5.Final</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		 <dependency>
		    <groupId>org.hibernate.javax.persistence</groupId>
		    <artifactId>hibernate-jpa-2.0-api</artifactId>
		    <version>1.0.1.Final</version>
		</dependency>
		<!-- spring mvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
 
		<!-- spring3 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
 
 
	</dependencies>
第三步:web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
	<display-name></display-name>
	<!-- spring hibernate -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>
	</context-param>
	<!-- openSessionInView配置 -->
	<filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<!--spring mvc 配置 -->
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup> <!-- 是启动顺序,让这个Servlet随Servletp容器一起启动。 -->
	</servlet>
 
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern> <!-- 会拦截/的请求。 -->
	</servlet-mapping>
 
 
	<!-- encodeing -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<!-- encoding filter for jsp page -->
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
 
 
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
 
	<welcome-file-list>
		<welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
	</welcome-file-list>
</web-app>
第四步:配置spring.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans               
           http://www.springframework.org/schema/beans/spring-beans-4.2.xsd      
           http://www.springframework.org/schema/context      
           http://www.springframework.org/schema/context/spring-context-4.2.xsd     
           http://www.springframework.org/schema/mvc      
           http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
           http://www.springframework.org/schema/util/
           http://www.springframework.org/schema/util/spring-util-4.2.xsd">
           <!-- 导入properties配置文件相关信息 -->
           <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
           	<property name="locations">
           		<list>
           			<value>classpath:dataSource.properties</value>
           		</list>
           	</property>
           </bean>
           <!-- 扫描dao,service -->
           <context:component-scan base-package="zx.dao,zx.service"/>
</beans>


第五步:配置spring-hibernate.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
 
	<!-- JNDI(通过Tomcat)方式配置数据源 -->
	<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->
	
	
	<!-- 配置数据源 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="${jdbc_url}" />
		<property name="username" value="${jdbc_username}" />
		<property name="password" value="${jdbc_password}" />
 
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="0" />
		<!-- 连接池最大使用连接数量 -->
		<property name="maxActive" value="20" />
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="20" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="0" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="60000" />
 
		<!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
 
		<property name="validationQuery" value="${validationQuery}" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="testWhileIdle" value="true" />
 
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="25200000" />
 
		<!-- 打开removeAbandoned功能 -->
		<property name="removeAbandoned" value="true" />
		<!-- 1800秒,也就是30分钟 -->
		<property name="removeAbandonedTimeout" value="1800" />
		<!-- 关闭abanded连接时输出错误日志 -->
		<property name="logAbandoned" value="true" />
 
		<!-- 监控数据库 -->
		<!-- <property name="filters" value="stat" /> -->
		<property name="filters" value="mergeStat" />
	</bean>
 
	<!-- 配置hibernate session工厂 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
			</props>
		</property>
 
		<!-- 自动扫描注解方式配置的hibernate类文件 -->
		<property name="packagesToScan">
			<list>
				<value>com.phn.model</value>
			</list>
		</property>
	</bean>
 
	<!-- 配置事务管理器 -->
	<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
 
	<!-- 注解方式配置事物 -->
	<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
 
	<!-- 拦截器方式配置事物 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" />
			<tx:method name="save*" />
			<tx:method name="update*" />
			<tx:method name="modify*" />
			<tx:method name="edit*" />
			<tx:method name="delete*" />
			<tx:method name="remove*" />
			<tx:method name="repair" />
			<tx:method name="deleteAndRepair" />
 
			<tx:method name="get*" propagation="SUPPORTS" />
			<tx:method name="find*" propagation="SUPPORTS" />
			<tx:method name="load*" propagation="SUPPORTS" />
			<tx:method name="search*" propagation="SUPPORTS" />
			<tx:method name="datagrid*" propagation="SUPPORTS" />
            <!-- 这里是针对我写的判断用户是否存在数据库的方法的事务,如果不配置的话就会出现no session异常 --> 
			<tx:method name="is*" />
 
			<tx:method name="*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="transactionPointcut" expression="execution(* com.phn.service..*Impl.*(..))" />
		<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
	</aop:config>
</beans>


第六步:配置spring-mvc.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context-3.1.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
	<!-- 注解扫描包 -->
	<context:component-scan base-package="zx.controller" />
 
	<!-- <context:component-scan /> -->
	<!-- 扫描指定的包中的类上的注解,常用的注解有: -->
	<!-- @Controller 声明Action组件 -->
	<!-- @Service 声明Service组件 @Service("xxxService") -->
	<!-- @Repository 声明Dao组件 -->
	<!-- @Component 泛指组件, 当不好归类时. -->
	<!-- @RequestMapping("/menu") 请求映射 -->
	<!-- @Resource 用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName") -->
	<!-- @Autowired 用于注入,(spring提供的) 默认按类型装配 -->
	<!-- @Transactional( rollbackFor={Exception.class}) 事务管理 -->
	<!-- @ResponseBody将内容或对象作为 HTTP 响应正文返回,并调用适合HttpMessageConverter的Adapter转换对象,写入输出流 -->
	<!-- @Scope("prototype") 设定bean的作用域 -->
 
 
	<!-- 开启MVC注解,表示SpringMVC支持注解功能 做了很多事情,不包括添加自己定义的拦截器 -->
	<mvc:annotation-driven />
 
	<!-- 拦截器 -->
	<!-- <mvc:interceptors> <bean class="com.phn.inteceptor" /> </mvc:interceptors> -->
 
	<!-- 方案一 (二选一)表示不对静态资源如CSS、JS、HTML等进行拦截 -->
	<!-- 当在web.xml 中 DispatcherServlet使用 <url-pattern>/</url-pattern> 映射时,能映射静态资源,使用默认的servlet来响应请求 -->
	<!-- <mvc:default-servlet-handler /> -->
 
	<!-- 方案二 (二选一) 静态资源映射 -->
	<mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
	<mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
	<mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
	<mvc:resources mapping="/html/**" location="/WEB-INF/html/" />
 
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/plain;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>
 
 
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/plain;charset=UTF-8</value>
						</list>
					</property>
				</bean>
				<bean
					class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>application/json;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>
</beans> 


第七步:配置properties配置文件

hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
jdbc_url=jdbc:mysql://localhost:3306/zx?useUnicode=true&characterEncoding=utf-8
jdbc_username=root
jdbc_password=root
 
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false

第八步:实体类创建

@Table(name = "user")
@Entity
public class UserEntity implements Serializable {
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String userName;
	private String passWord;
	private Boolean sex;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID", nullable = false, length = 9)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "NAME", nullable = false, length = 20)
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Column(name = "PASSWORD", nullable = false, length = 20)
	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	@Column(name = "SEX", nullable = false, length = 1)
	public Boolean getSex() {
		return sex;
	}

	public void setSex(Boolean sex) {
		this.sex = sex;
	}

	public String toString() {
		return "UserEntity [id=" + id + ", userName=" + userName
				+ ", passWord=" + passWord + ", sex=" + sex + "]";
	}

}



这里注解的具体意思以及用法可以看下一遍我转载的文章



第九步:dao接口

public interface UserDao {
	public Boolean isExist(UserEntity userEntity);
}


第十步:dao实现类

@Repository
public class UserDaoImpl implements UserDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}
 
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	public Boolean isExist(UserEntity userEntity) {
		Session session = getSession();
		String sql="from UserEntity where userName=? and passWord=?";
		Query query = session.createQuery(sql);
		query.setString(0, userEntity.getUserName());
		query.setString(1, userEntity.getPassWord());
		List<UserEntity> list = query.list();
		if(list.size()>0){
			return true;
		}
		return false;
	}

}


第十一步:测试类

@ContextConfiguration(locations = { "classpath:spring.xml",
		"classpath:spring-hibernate.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
// defaultRollback=true不会改变数据库,false会改变数据库
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class UserDaoTest {
	@Resource
	private UserDao userDao;

	@Test
	public void testUserDao() {
		UserEntity userEntity=new UserEntity();
		userEntity.setUserName("张肖");
		userEntity.setPassWord("1234");
		Boolean exist = userDao.isExist(userEntity);
		System.out.println(exist);
	}
}


第十二步 编写 Service接口以及它的实现类,并进行测试.

public interface UserService {
	Boolean isExist(UserEntity userEntity);
}

@Service
@Transactional
public class UserServiceImpl implements UserService {

	@Resource
	private UserDao userDao;

	public Boolean isExist(UserEntity userEntity) {
		return userDao.isExist(userEntity);
	}

}
@ContextConfiguration(locations = { "classpath:spring.xml",
		"classpath:spring-hibernate.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
// defaultRollback=true不会改变数据库,false会改变数据库
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class UserServiceTest {
	@Resource
	private UserService userService;

	@Test
	public void testUserDao() {
		UserEntity userEntity=new UserEntity();
		userEntity.setUserName("张肖");
		userEntity.setPassWord("1234");
		Boolean exist = userService.isExist(userEntity);
		System.out.println(exist);
	}
}
请大家自行就行测试


第十三步:表现层 controller编写

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	
	@ResponseBody
	@RequestMapping(value="/login", method = RequestMethod.POST)
	public Map<String,Object> login( UserEntity user){
		Map<String,Object> map = new HashMap<String,Object>();
		System.out.println(user.toString());
		boolean loginResult = userService.isExist(user);
		map.put("loginResult", loginResult);
		return map;
	}
}

第十四步:页面以及测试

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/login.js"></script>
</head>
<body>
	用户名:<input type="text" name="userName" /><br>
	密码:<input type="password" name="passWord" /><br>
	<input  type="button" value="登录" id="butlog"/><br>
</body>
</html>
$(function() {
$("#butlog").click(function(){
	console.log("login");
	var username=$("input[name='userName']").val();
	var password=$("input[name='passWord']").val();
	var user={
			userName:username,
			passWord:password
	};
	$.ajax({
		   type: "POST",
		   async: false,
		   url: "/user/login",
		   data: user,
		   dataType:"json",
		   success: function(data){
			   if(data.loginResult){
				   alert("成功");
			   }else{
				   alert("账号密码错误");
			   }
		   },
			error:function(){
				alert("服务器繁忙请重试");
			}
		});
})
})

这样我们的简易springmvc,hibernate和spring的整合算是搭建完成

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

spring+spring mvc+hibernate+maven+oracle10g框架搭建及hibernate Tool使用汇总(附github源码)

今天搭建了一套ssh的maven管理的项目框架,大多是网上已经有的资源的整合,中间也遇到了一些小问题,现总结下,分享给有需要的朋友,大家多多进步; 首先贴上源码:https://github.com...

springmvc4+hibernate4整合框架的搭建,超详细哦

spring

spring springmvc hibernate(ssh)项目整合开发---总体架构搭建

ssm项目的整合-先进行整体架构的搭建

SpringMVC+Hibernate+Spring整合实例(一)

转自 http://blog.csdn.net/bjyfb/article/details/8998267 SpringMVC又一个漂亮的web框架,他与Struts2并驾齐驱,Struts出世...

springMVC系列之(四) spring+springMVC+hibernate 三大框架整合

Hibernate五大对象: Configuration sessionFactory Session Transa
  • lishehe
  • lishehe
  • 2014年08月03日 11:37
  • 61626

框架篇:Spring+SpringMVC+hibernate整合开发

前言:   最近没什么事做,搭个框架写成博客记录下来,拉通一下之前所学知识.   话不多说,我们直接步入正题。   准备工作:      1/安装并配置java运行环境      2/数据库...

SpringMVC+Spring+Hibernate搭建实例

SpringMVC+Spring+Hibernate框架的实例

Spring mvc+Spring+hibernate整合

有段时间没有更新博客了,跟最近比较忙有关系。无聊搭建个Java web框架,反正好久没有自己搭建框架了,算是练练手了,今天我就来搭建一个框架,技术选型为Spring mvc+Spring+hibern...

spring4+springmvc+hibernate3 整合demo

  • 2017年10月26日 09:44
  • 20.99MB
  • 下载

SpringMVC+spring+hibernate整合ssh

  • 2017年09月06日 21:54
  • 19.88MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:springmvc-spring-hibernate整合
举报原因:
原因补充:

(最多只允许输入30个字)