SSH 基础模块总结
1 Spring+springMVC+hibernate+Maven框架搭建
1、创建maven工程
2、导入依赖
<? xml version ="1.0" encoding ="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi :schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.cyc</ groupId > < artifactId >SSHDemo</ artifactId > < version >1.0-SNAPSHOT</ version > < packaging >war</ packaging > <!-- 本人将配置包的版本信息都统一放入此处,方便以后管理--> <properties > < maven.compiler.source >1.7</ maven.compiler.source > < maven.compiler.target >1.7</ maven.compiler.target > < springVersion >4.3.1.RELEASE</ springVersion > < hibernateVersion >RELEASE</ hibernateVersion > < jstlVersion >1.2</ jstlVersion > < taglibVersion >1.1.2</ taglibVersion > < servletVersion >3.0-alpha-1</ servletVersion > < jsonVersion >1.9.13</ jsonVersion > < jacksonVersion >2.5.0</ jacksonVersion > < mysqlVersion >5.1.8</ mysqlVersion > < c3p0Version >0.9.1.2</ c3p0Version > < log4jVersion >1.2.17</ log4jVersion > < fileuploadVersion >1.3.1</ fileuploadVersion > < lombokVersion >1.16.10</ lombokVersion > </ properties > < dependencies > <!-- 单元测试 --> <dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >3.8.1</ version > < scope >test</ scope > </ dependency > <!-- spring--> <dependency > < groupId >org.springframework</ groupId > < artifactId >spring-beans</ artifactId > < version >${springVersion}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-core</ artifactId > < version >${springVersion}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >${springVersion}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-orm</ artifactId > < version >${springVersion}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-aop</ artifactId > < version >${springVersion}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-tx</ artifactId > < version >${springVersion}</ version > </ dependency > <!-- spring web + spring MVC--> <dependency > < groupId >org.springframework</ groupId > < artifactId >spring-web</ artifactId > < version >${springVersion}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > < version >${springVersion}</ version > </ dependency > <!-- hibernate 配置--> <dependency > < groupId >org.hibernate</ groupId > < artifactId >hibernate-core</ artifactId > < version >${hibernateVersion}</ version > </ dependency > <!-- hibernate 缓存, 视情况添加--> <dependency > < groupId >org.hibernate</ groupId > < artifactId >hibernate-ehcache</ artifactId > < version >${hibernateVersion}</ version > </ dependency > <!-- jsp 页面使用的jstl 支持--> <dependency > < groupId >javax.transaction</ groupId > < artifactId >jta</ artifactId > < version >1.1</ version > </ dependency > < dependency > < groupId >aspectj</ groupId > < artifactId >aspectjweaver</ artifactId > < version >1.5.4</ version > </ dependency > < dependency > < groupId >jstl</ groupId > < artifactId >jstl</ artifactId > < version >${jstlVersion}</ version > </ dependency > < dependency > < groupId >taglibs</ groupId > < artifactId >standard</ artifactId > < version >${taglibVersion}</ version > </ dependency > < dependency > < groupId >javax.servlet</ groupId > < artifactId >servlet-api</ artifactId > < version >${servletVersion}</ version > < scope >provided</ scope > </ dependency > <!-- DataBase 数据库连接 mysql 包--> <dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >${mysqlVersion}</ version > </ dependency > <!-- 数据库连接池--> <dependency > < groupId >c3p0</ groupId > < artifactId >c3p0</ artifactId > < version >${c3p0Version}</ version > </ dependency > <!-- json 数据 使springMVC 可以返回json值 ,视情况添加--> <dependency > < groupId >org.codehaus.jackson</ groupId > < artifactId >jackson-mapper-asl</ artifactId > < version >${jsonVersion}</ version > </ dependency > <!-- Jackson 可以轻松的将Java 对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象--> <dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-core</ artifactId > < version >${jacksonVersion}</ version > </ dependency > < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-annotations</ artifactId > < version >${jacksonVersion}</ version > </ dependency > < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-databind</ artifactId > < version >${jacksonVersion}</ version > </ dependency > < dependency > < groupId >com.alibaba</ groupId > < artifactId >fastjson</ artifactId > < version >1.2.9</ version > </ dependency > <!-- log4j 配置, 视情况添加--> <dependency > < groupId >log4j</ groupId > < artifactId >log4j</ artifactId > < version >${log4jVersion}</ version > </ dependency > <!-- 文件 上传--> <dependency > < groupId >commons-fileupload</ groupId > < artifactId >commons-fileupload</ artifactId > < version >${fileuploadVersion}</ version > </ dependency > <!-- lombok 插件导包--> <dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > < version >${lombokVersion}</ version > < scope >provided</ scope > </ dependency > </ dependencies > </ project >
3、spring和hibernate整合
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: aop ="http://www.springframework.org/schema/aop" xmlns: tx ="http://www.springframework.org/schema/tx" 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/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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" > <!-- 配置数据源 --> <context :property-placeholder location ="classpath:application.properties" /> < bean id ="dataSource" class ="com.mchange.v2.c3p0.ComboPooledDataSource" > < property name ="user" value ="${jdbc.username}" ></property > < property name ="password" value ="${jdbc.password}" ></property > < property name ="driverClass" value ="${jdbc.driverClassName}" ></property > < property name ="jdbcUrl" value ="${jdbc.url}" ></property > < property name ="initialPoolSize" value ="${jdbc.initPoolSize}" ></property > < property name ="maxPoolSize" value ="${jdbc.maxPoolSize}" ></property > </ bean > <!-- 配置sessionFactory--> <bean id ="sessionFactory" class ="org.springframework.orm.hibernate5.LocalSessionFactoryBean" > < property name ="dataSource" ref ="dataSource" /> <!-- hibernate 配置文件的路径 --> <property name ="configLocation" value ="classpath:spring/hibernate.cfg.xml" ></property > < property name ="annotatedClasses" > < list > < value >com.sunsharing.model.User</ value > </ list > </ property > </ bean > <!-- 配置事务管理器 --> <bean id ="txManager" class ="org.springframework.orm.hibernate5.HibernateTransactionManager" > < property name ="sessionFactory" ref ="sessionFactory" /> </ bean > <!-- 配置自动包扫描 --> <context :component-scan base-package ="com.sunsharing.service,com.sunsharing.dao" /> <!-- 开启注解事务 只对当前配置文件有效 --> <tx :annotation-driven transaction-manager ="txManager" /> </ beans >
hibernate.cfg.xml备注:此处也可直接在spring文件里面配置
<? 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 > <!-- 配置hibernate 基本信息 --> <!-- 1.数据源配置在IOC容器中,此处不需要额外配置 --> <!-- 2.关联的.hbm.xml文件也在IOC容器配置SessionFactory时配置 --> <!-- 3.此处配置hibernate的基本信息:数据库方言、SQL显示及格式化,及生成数据表的策略,二级缓存等 --> <property name ="hibernate.dialect" >org.hibernate.dialect.MySQL5Dialect</ property > < property name ="hibernate.show_sql" >true</ property > < property name ="hibernate.format_sql" >true</ property > < property name ="hibernate.hbm2ddl.auto" >update</ property > < mapping class ="com.sunsharing.model.User" /> </ session-factory > </ hibernate-configuration >
application.properties
jdbc.driverClassName =com.mysql.jdbc.Driver jdbc.url =jdbc:mysql://localhost:3306/db_test?characterEncoding=UTF8 jdbc.username =root jdbc.password =123 jdbc.initPoolSize =20 jdbc.maxPoolSize =100 jdbc.minPoolSize =10 jdbc.maxIdleTime =600 jdbc.acquireIncrement =5 jdbc.maxStatements =5 jdbc.idleConnectionTestPeriod =60
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_3_1.xsd" version ="3.1" > < welcome-file-list > < welcome-file >/pages/index.jsp</ welcome-file > </ welcome-file-list > <!-- 加载所有的配置文件 --> <context-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath*:/spring/applicationContext.xml</ param-value > </ context-param > <!-- 配置Spring 监听 --> <listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > < 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/spring-dispatcher.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 > < 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 > < filter-mapping > < filter-name >encodingFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > <!-- 配置Session --> <filter > < filter-name >OpenSessionInViewFilter</ filter-name > < filter-class > org.springframework.orm.hibernate5.support.OpenSessionInViewFilter </ filter-class > </ filter > < filter-mapping > < filter-name >OpenSessionInViewFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > </ web-app >
4、加入springMVC配置(springMVC和spring不需要整合)
<? 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" > <!-- 注解扫描包 --> <context :component-scan base-package ="com.sunsharing.controller" /> <!-- 开启注解 --> <mvc :annotation-driven /> <!-- 静态资源(js/image) 的访问 --> <!-- <mvc:resources location="/js/" mapping="/js/**"/> --> <mvc :resources mapping ="/js/**" location ="/js/" /> < mvc :resources mapping ="/artTemplate/**" location ="/artTemplate/" /> < mvc :resources mapping ="/css/**" location ="/css/" /> < mvc :resources mapping ="/img/**" location ="/img/" /> < mvc :resources mapping ="/ulynlist/**" location ="/ulynlist/" /> <!-- 定义视图解析器 --> <bean id ="viewResolver" class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name ="prefix" value ="/WEB-INF/jsp/" ></property > < property name ="suffix" value =".jsp" ></property > </ bean > </ beans >
5、dao方法
package com.sunsharing.dao.Impl; import com.sunsharing.dao.UserDao; import com.sunsharing.model.User; import org.hibernate.query.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Repository ; import javax.annotation.Resource ; import java.util.List; /** * Created by Administrator on 2017/7/24. */ @Repository public class UserDaoImpl implements UserDao { @Resource private SessionFactory sessionFactory ; public User getUser(String id) { User user = getSession().get(User.class , id); return user; } public List<User> getUserByUserName(String username) { String hql = "from User u where u.userName=?" ; Query query = getSession().createQuery(hql); query.setParameter(0 , username); return (List<User>) query.uniqueResult(); } public List<User> getAllUser() { String hql = "from User" ; Query query = getSession().createQuery(hql); return query.list(); } public void addUser(User user) { getSession().save(user); } public boolean delUser(User user) { getSession().delete(user); return true ; } public boolean updateUser(User user) { getSession().update(user); return (true ); } public List<User> findUser(String username) { String hql = "from User u where u.userName like ?" ; Query query = getSession().createQuery(hql); query.setParameter(0 , username + "%" ); return query.list(); } private Session getSession() { return sessionFactory .getCurrentSession(); } }