maven+springmvc+spring4+hibernate4整合

最近在将maven、springmvc、spring4、hibernate4几个技术整合做个项目,刚开始的配置就一直出现问题,所以今天做个总结,这是整个项目针对注册功能的相关配置及实现。

1. web.xml配置:


xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
security



org.springframework.web.context.ContextLoaderListener




contextConfigLocation

classpath*:applicationContext.xml




encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8


forceEncoding
true





encodingFilter
/*





springMVC
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath*:spring-mvc.xml

1


springMVC
/




register.jsp


2. maven的包依赖配置:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.dyoon
wearshop
war
0.0.1-SNAPSHOT
wearshop Maven Webapp
http://maven.apache.org

3.2.1
4.3.2.RELEASE
1.7.21





antlr
antlr
2.7.7


commons-beanutils
commons-beanutils
1.9.2


commons-codec
commons-codec
1.10


commons-collections
commons-collections
3.2.1


commons-dbcp
commons-dbcp
1.4


commons-fileupload
commons-fileupload
1.3.1


commons-io
commons-io
2.4


commons-logging
commons-logging
1.2


commons-pool
commons-pool
1.6


dom4j
dom4j
1.6.1


com.fasterxml.jackson.core
jackson-annotations
2.8.1


com.fasterxml.jackson.core
jackson-core
2.8.1


com.fasterxml.jackson.core
jackson-databind
2.8.1


javax.servlet
jstl
1.2


log4j
log4j
1.2.17


org.slf4j
slf4j-api
${slf4j.version}





org.springframework
spring-aop
${spring.version}


org.springframework
spring-beans
${spring.version}


org.springframework
spring-context
${spring.version}


org.springframework
spring-core
${spring.version}


org.springframework
spring-expression
${spring.version}


org.springframework
spring-jdbc
${spring.version}


org.springframework
spring-orm
${spring.version}


org.springframework
spring-test
${spring.version}


org.springframework
spring-tx
${spring.version}


org.springframework
spring-web
${spring.version}


org.springframework
spring-webmvc
${spring.version}


org.springframework
spring-context-support
4.3.2.RELEASE


org.springframework
spring-aspects
4.3.2.RELEASE





aopalliance
aopalliance
1.0




org.aspectj
aspectjrt
1.7.4


org.aspectj
aspectjweaver
1.7.4





mysql
mysql-connector-java
5.1.38





org.hibernate
hibernate-core
4.3.5.Final



org.hibernate
hibernate-entitymanager
4.3.5.Final


org.hibernate
hibernate-ehcache
4.3.5.Final


org.hibernate
hibernate-commons-annotations
3.3.0.ga


org.hibernate.javax.persistence
hibernate-jpa-2.1-api
1.0.0.Final





org.javassist
javassist
3.18.1-GA




org.jboss.logging
jboss-logging
3.1.3.GA


org.jboss.spec.javax.annotation
jboss-annotations-api_1.2_spec
1.0.0.Final


org.jboss
jandex
1.1.0.Final




javax.servlet
javax.servlet-api
3.0.1




junit
junit
4.9
test







org.apache.maven.plugins
maven-compiler-plugin

1.8
1.8



org.mortbay.jetty
jetty-maven-plugin
7.1.6.v20100715







maven2-repository.dev.java.net
Java.net Repository for Maven
http://download.java.net/maven/2/
default



3. spring的applicationContext.xml配置:

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" 
xmlns:aop="http://www.springframework.org/schema/aop"
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.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">









 

value="jdbc:mysql://localhost:3306/db_wearshop?useUnicode=true">


 


class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">



org.hibernate.dialect.MySQLDialect
true
true
update



           
                com.dyoon.pojo*
           

       





class="org.springframework.orm.hibernate4.HibernateTransactionManager">
















id="mypointcut" />





4. springMVC的配置:

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.3.xsd  
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">



 




            value="org.springframework.web.servlet.view.JstlView" />  




5. Controller层实现:
package com.dyoon.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import com.dyoon.pojo.Member;
import com.dyoon.service.IMemberService;


@Controller
public class MemberController {

@Autowired
private IMemberService memberService;

@RequestMapping(value="/register")
public ModelAndView register(HttpServletRequest request,HttpServletResponse response){

ModelAndView mv = new ModelAndView();
Member member;
Member newMember = new Member();
try {
member = memberService.findByMembername(request.getParameter("memberName"));
} catch (Exception e) {
member = null;
}


if(member != null){
mv.addObject("msg","用户名已存在!");
mv.setViewName("register");
}else{
newMember.setMemberName(request.getParameter("memberName"));
newMember.setPassword(request.getParameter("password"));
newMember.setSex(request.getParameter("sex"));
newMember.setEmail(request.getParameter("phone"));
newMember.setCity(request.getParameter("email"));
newMember.setAddress(request.getParameter("address"));
memberService.save(newMember);

mv.addObject("msg","注册成功!");
mv.setViewName("account");
}
return mv;
}
}
6. service层实现:
package com.dyoon.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.dyoon.dao.IMemberDao;
import com.dyoon.pojo.Member;
import com.dyoon.service.IMemberService;


@Service("memberService")
public class MemberServiceImpl implements IMemberService {


@Autowired
private IMemberDao memberDao;

@Override
public void save(Member member) {
memberDao.save(member);
}


@Override
public Member findByMembername(String membername) {
return memberDao.findByMemberName(membername);
}

}
7. DAO层实现:
package com.dyoon.dao.impl;


import java.awt.Menu;
import java.util.List;


import javax.annotation.Resource;


import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;


import com.dyoon.dao.IMemberDao;
import com.dyoon.pojo.Member;


@Repository(value="memberDao")
public class MemberDaoImpl extends HibernateDaoSupport implements IMemberDao {


@Resource(name = "sessionFactory") 
   public void setSessionFactoryOverride(SessionFactory sessionFactory)  
   {  
       super.setSessionFactory(sessionFactory);  

   } 
 
@Override
public List findAll() {
List members = (List) this.getHibernateTemplate().find("from tb_member");
return members;
}


@Override
public void save(Member t) {
getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
this.getHibernateTemplate().save(t);
}


@Override
public void delete(Member t) {
this.getHibernateTemplate().delete(t);
}


@Override
public void update(Member t) {
this.getHibernateTemplate().update(t);


}


@Override
public void findById(Integer id) {
this.getHibernateTemplate().find("from tb_member where memberID = ?", id);


}


@Override
public Member findByMemberName(String membername) {
return (Member) this.getHibernateTemplate().find("from tb_member where memberName = ?", membername).get(0);

}


}
8. 实体类与Hibernate映射的注解实现:
package com.dyoon.pojo;


import java.io.Serializable;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity(name = "tb_member")
public class Member implements Serializable {


/**

*/
private static final long serialVersionUID = -141893975369584497L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="memberID")
private Integer memberID;

private String memberName;

private String password;

private String sex;

private String phone;

private String email;

private String city;

private String address;

public Member() {
}

public Member(String memberName, String password, String sex, String phone, String email, String city,
String address) {
super();
this.memberName = memberName;
this.password = password;
this.sex = sex;
this.phone = phone;
this.email = email;
this.city = city;
this.address = address;
}


public Member(Integer memberID, String memberName, String password, String sex, String phone, String email,
String city, String address) {
super();
this.memberID = memberID;
this.memberName = memberName;
this.password = password;
this.sex = sex;
this.phone = phone;
this.email = email;
this.city = city;
this.address = address;
}


public Integer getMemberID() {
return memberID;
}


public void setMemberID(Integer memberID) {
this.memberID = memberID;
}


public String getMemberName() {
return memberName;
}


public void setMemberName(String memberName) {
this.memberName = memberName;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


public String getSex() {
return sex;
}


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


public String getPhone() {
return phone;
}


public void setPhone(String phone) {
this.phone = phone;
}


public String getEmail() {
return email;
}


public void setEmail(String email) {
this.email = email;
}


public String getCity() {
return city;
}


public void setCity(String city) {
this.city = city;
}


public String getAddress() {
return address;
}


public void setAddress(String address) {
this.address = address;
}


}

9. jsp页面的form表单:

 




用户名
 


密码
 


重复密码



性别
 


电话
 


邮箱
 


城市
 


地址
 


   

至此对maven+springmvc+spring4+Hibernate4整合技术做个记录。


你可能感兴趣的:(ssh技术)