标签:des blog http os io 使用 java ar for
前面的一篇文章http://blog.csdn.net/wangdianyong/article/details/38842693曾经也写过三者之间的整合,但是当时没有用maven
导致了项目中含有大量包,看着十分不美观!
下面就使用maven重新整合一下!新手还望不吝赐教!
maevn项目的建立过程省略
看项目结构
各个配置文件代码我贴一下,不一样的地方请自行修改
database.properties
hibernate.dialect=org.hibernate.dialect.MySQLDialect driverClassName=com.mysql.jdbc.Driver validationQuery=SELECT 1 url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8 username=root password=root hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=true
<?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
">
<!-- 使用C3P0数据源,MySQL数据库 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- MySQL5 -->
<property name="driverClass" value="${driverClassName}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="maxPoolSize" value="40"></property>
<property name="minPoolSize" value="1"></property>
<property name="initialPoolSize" value="1"></property>
<property name="maxIdleTime" value="20"></property>
</bean>
<!-- 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>
<!-- 注解方式配置 -->
<property name="packagesToScan">
<list>
<value>com.iss.model</value>
</list>
</property>
</bean>
<!-- 配置事务 -->
<bean name="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
</beans>spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <context:component-scan base-package="com.iss.controller" /> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <!-- <bean id="mappingJacksonHttpMessageConverter" --> <!-- class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> --> <!-- <property name="supportedMediaTypes"> --> <!-- <list> --> <!-- <value>text/html;charset=UTF-8</value> --> <!-- </list> --> <!-- </property> --> <!-- </bean> --> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" />json转换器 </list> </property> </bean> --> <mvc:annotation-driven /> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> </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:context="http://www.springframework.org/schema/context" 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.0.xsd "> <!--启用注解 --> <context:annotation-config /> <context:component-scan base-package="com.iss.action,com.iss.dao.impl,com.iss.service"></context:component-scan> <!-- 引入属性文件Hibernate属性配置 --> <context:property-placeholder location="classpath:database.properties" /> <!-- 自动扫描dao和service包(自动注入) --> <!-- <context:component-scan base-package="com.iss*" /> --> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring.xml,/WEB-INF/classes/spring-hibernate.xml,</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Hibernate的session丢失解决方法 --> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts2配置 --> <!-- <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> <!-- springMVC 配置 --> <servlet> <description>spring mvc servlet</description> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
com.iss.controller.UserController
package com.iss.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.iss.model.User;
import com.iss.service.UserService;
@Controller("userController")
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/reg")
public String saveUser(User user) {
System.out.println(userService);
userService.save(user);
return "success";
}
}
package com.iss.dao;
import com.iss.model.User;
public interface UserDao<T> {
public User save(T o);
}
package com.iss.dao.impl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.iss.dao.UserDao;
import com.iss.model.User;
@Repository("userDao")
public class UserDaoImpl implements UserDao<User> {
@Autowired
private SessionFactory sessionFactory;
//
// public void setSessionFactory(SessionFactory sessionFactory) {
// this.sessionFactory = sessionFactory;
// }
//
// public SessionFactory getSessionFactory() {
// return sessionFactory;
// }
@Override
public User save(User o) {
Session session = sessionFactory.getCurrentSession();
session.save(o);
return o;
}
}
package com.iss.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user")
public class User implements Serializable {
private int id;
private String name;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.iss.service;
import com.iss.model.User;
public interface UserService {
public User save(User user);
}
package com.iss.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.iss.dao.UserDao;
import com.iss.model.User;
import com.iss.service.UserService;
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDao<User> userDao;
@Override
public User save(User user) {
// TODO Auto-generated method stub
return userDao.save(user);
}
}<body> <form action="userAction" method="post"> <table width="207" border="0" align="center"> <tr> <td colspan="2" align="center" nowrap="nowrap">用户注册</td> </tr> <tr> <td width="68" nowrap="nowrap">用户名</td> <td width="127" nowrap="nowrap"><label> <input name="name" type="text" id="username" size="20" /> </label></td> </tr> <tr> <td colspan="2" align="center" nowrap="nowrap"><label> <input type="submit" value="注册" /> <input type="reset" value="重填" /> </label></td> </tr> </table> </form> </body>
<body>${user.name }
</body>完成之后mvn install 部署到tomcat服务器即可!
Spring-SpringMVC-Hibernate maven整合
标签:des blog http os io 使用 java ar for
原文地址:http://blog.csdn.net/wangdianyong/article/details/39100173