博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用maven搭建SSH项目(spring+springmvc+Hibernate)
阅读量:4211 次
发布时间:2019-05-26

本文共 12448 字,大约阅读时间需要 41 分钟。

本文介绍使用eclipse+maven搭建Spring+SpringMvc+Hibernate项目,以登陆为例: 

1、创建maven项目 
2、把maven项目变为动态网站 
3、搭建spring+springmvc+Hibernate项目


1 创建maven项目

  • 请点击查看:

2 把maven项目变为动态网站

  • 请点击查看:

  • 项目图如下: 

    这里写图片描述

3 开始搭建spring+springmvc+Hibernate项目

3.1 配置maven依赖,在pom.xml写入如下:

4.0.0
com.welljoint
testSSH2
0.0.1-SNAPSHOT
war
UTF-8
4.2.6.RELEASE
org.springframework
spring-core
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-orm
${spring.version}
org.hibernate
hibernate-core
3.6.9.Final
org.hibernate
hibernate-entitymanager
3.6.9.Final
mysql
mysql-connector-java
5.1.6
javax.servlet.jsp.jstl
jstl-api
1.2
javax.servlet
javax.servlet-api
3.1.0
commons-dbcp
commons-dbcp
1.4
commons-pool
commons-pool
1.6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

3.2 请检查maven依赖是否导入到项目中了

  • 请点击查看:

3.3 在src/main/java中新建一个com.welljoint.entity的包;在这个包里新建一个User.java的bean

package com.welljoint.entity;/**  * @version   * @time 2018-2-28 下午6:02:03  * @describe:用户的bean */public class User {
private Integer id; private String userName; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", password=" + password + "]"; }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

3.4 在WEB-INF目录下创建一个view文件夹;在该文件夹里创建一个login.jsp,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
登陆
用户名:
密码:
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 再在view文件夹下创建一个login_success.jsp的页面,代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
登陆成功 登陆成功
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3.5 在src/main/java中新建一个com.welljoint.rest的包;在这个包里新建一个LoginController.java作为控制器

package com.welljoint.rest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.welljoint.entity.User;/**  * @version   * @time 2018-2-28 下午5:57:07  * @describe:控制器 */@Controller  //注解为控制器@RequestMapping(value="/login")    //截获带有/login的请求public class LoginController {
@RequestMapping(method=RequestMethod.GET) public String get(){ //用来返回一个页面 return "login"; //返回指向login.jsp页面 } @RequestMapping(method=RequestMethod.POST) public String post(User user){ //用来处理用户的登陆请求 return "login_success"; }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

3.6 在src/main/resources中新建一个spring-mvc.xml的文件,代码如下:

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

3.7 配置web.xml,代码如下:

testSSH2
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
springmvc
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
CharacterEncodingFilter
/*
*.jsp
true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 如果你是按照上述一步步做的话,已经可以运行看效果了,效果图如下: 
    这里写图片描述
  • 任意输入点击登陆后,如下图: 
    这里写图片描述

3.8 写service层:在src/main/java中新建一个com.welljoint.service的包;在这个包里新建一个LoginService.java作为服务层的接口

package com.welljoint.service;public interface LoginService {
public int login(String userName,String password);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 再在com.welljoint.service包中新建一个LoginServiceImpl.java作为服务层接口的实现
package com.welljoint.service;import org.springframework.stereotype.Service;@Servicepublic class LoginServiceImpl implements LoginService{
public int login(String userName,String password){ return 1; }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 此时就可以把service层注入到controller层来发挥作用了,把LoginController.java修改为如下:
package com.welljoint.rest;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 com.welljoint.entity.User;import com.welljoint.service.LoginService;/**  * @version   * @time 2018-2-28 下午5:57:07  * @describe:控制器 */@Controller  //注解为控制器@RequestMapping(value="/login")    //截获带有/login的请求public class LoginController {
@Autowired LoginService loginService; //注入service层 @RequestMapping(method=RequestMethod.GET) public String get(){ //用来返回一个页面 return "login"; //返回指向login.jsp页面 } @RequestMapping(method=RequestMethod.POST) public String post(User user){ //用来处理用户的登陆请求 if (loginService.login(user.getUserName(), user.getPassword())==1) { return "login_success"; //登陆成功,跳转到login_success.jsp页面 } return "login"; }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 把项目运行起来,如果效果和之前的一样,输入任意值,点击登陆可以跳转,则证明service层写正确了,如下: 
    这里写图片描述

3.9写dao层:在src/main/java中新建一个com.welljoint.dao的包;在这个包里新建一个UserDao.java作为数据层的接口

package com.welljoint.dao;import com.welljoint.entity.User;public interface UserDao {
public User find(String userName,String password);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 再在com.welljoint.dao包中新建一个UserDaoImpl.java作为数据层接口的实现
package com.welljoint.dao;import org.springframework.stereotype.Repository;import com.welljoint.entity.User;@Repositorypublic class UserDaompl implements UserDao{
public User find(String userName,String password){ return new User();//假数据,后期要通过Hibernate去数据库取 }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 此时就可以把dao层注入到service层来发挥作用了,把LoginServiceImpl.java修改为如下:
package com.welljoint.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.welljoint.dao.UserDao;@Servicepublic class LoginServiceImpl implements LoginService{
@Autowired private UserDao userDao; public int login(String userName,String password){ return userDao.find(userName, password)==null?0:1; }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 把项目运行起来,如果效果和之前的一样,输入任意值,点击登陆可以跳转,则证明dao层写正确了,如下: 
    这里写图片描述

3.10 把Hibernate加入到项目中,并且去数据库查询数据

  • 对于这一节,是难点,<敲黑板>,3.9节前做的是前奏,都没有涉及到数据库操作,关于3.10节,大家可以用Hibernate也可以用Mybatis,这一章我们讲Hibernate,我的另一篇文章讲Mybatis,也欢迎大家看。

  • (1)使用mysql,在本地创建一个名为ssh的数据库(不用建表,表会自动创建的) 

    这里写图片描述

  • (2)在com.welljoint.entity包中创建User这个bean的映射文件User.hbm.xml
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • (3)在src/main/resources中创建spring.xml文件,代码如下:
org.hibernate.dialect.MySQLDialect
true
update
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • (4)把spring.xml配置文件,加入到web.xml中,web.xml代码修改为如下(请关注本文件中新添加的两部分代码):
testSSH2
contextConfigLocation
classpath:spring.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
springmvc
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
CharacterEncodingFilter
/*
*.jsp
true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • (5)把Hibernate功能添加到dao层中,从而进行数据库操作,UserDaoImpl.java代码修改为如下:
package com.welljoint.dao;import java.util.List;import javax.annotation.Resource;import org.hibernate.SessionFactory;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.springframework.stereotype.Repository;import com.welljoint.entity.User;@Repositorypublic class UserDaompl extends HibernateDaoSupport implements UserDao{
//HibernateDaoSupport来操作数据库更加方便 //用来注入sessionFactory(不注入会报错) @Resource(name = "sessionFactory") public void setSessionFactoryOverride(SessionFactory sessionFactory) { super.setSessionFactory(sessionFactory); } @Override public User find(String userName,String password){ //注意:以下是HQL语句 List
users = getHibernateTemplate().find("from User where userName=? and password=?",userName,password); return users.size()>0?(User)users.get(0):null;//假数据,后期要通过Hibernate去数据库取 }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • (6)最终项目截图如下: 
    这里写图片描述
  • (7)恭喜您,整个SSH项目已经搭建完毕了,快来运行一下吧。启动项目后,首先检查eclipse的控制台有没有错误信息,有的话请百度。再看到浏览器会显示登陆框,如下: 
    这里写图片描述
  • (8)此时ssh数据库已经自动创建了一个user表了,请看: 
    这里写图片描述
  • (9)我们在上述的user表中,手动填入“11”,“22”,(后期大家自己写注册的代码,就不用手动填写了)如下图: 
    这里写图片描述
  • (10)回到浏览器,我们可以使用刚刚填入到数据库的值进行登陆了 
    这里写图片描述 
    这里写图片描述 
    • 我们填入错误的值(填入错误的值,仍会跳转到login.jsp页面) 
      这里写图片描述 
      这里写图片描述

4项目下载地址:

本文原地址:

你可能感兴趣的文章
Talib学习笔记(二)- 价格指数学习
查看>>
CAS机制是什么?
查看>>
Semaphore源码解析
查看>>
ConcurrentLinkedDeque源码解析
查看>>
ReentrantLock源码解析
查看>>
StampedLock源码解析
查看>>
ReentrantReadWriteLock源码解析
查看>>
springboot源码解析(四)
查看>>
CompletionService实践
查看>>
YApi在Window上离线安装笔记
查看>>
Mysql学习笔记(十三)查看mysql日志
查看>>
JVM垃圾回收相关知识笔记
查看>>
Curator学习笔记(一)- 读写锁
查看>>
第一次炒股小记
查看>>
《redis in action》ZSet相关命令
查看>>
《redis in action》redis发布订阅
查看>>
《redis in action》sort排序命令
查看>>
《redis in action》redis事务
查看>>
《redis in action》key的自动过期
查看>>
《redis in action》redis持久化简介
查看>>