博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【maven + hibernate(注解) +spring +springMVC】 使用maven搭建项目
阅读量:6721 次
发布时间:2019-06-25

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

研究,百度,查资料+好友帮助,使用MyEcplise2015工具,通过maven搭建hibernate+springMVC+spring的项目,数据库采用MySql5.5 不过使用的版本会在项目搭建过程中介绍。

大概就是下面的几个步骤:

1. 设计数据库,得到数据库

2.使用maven搭建项目,导入包的依赖

3.引入spring的配置信息,放在资源文件中

4.引入Log4j日志文件配置

5.配置hibernate

6.配置ehcache.xml

7.使用hibernate  tools生成实体

8.创建Dao层、service层

9.配置spring-mvc.xml

10.配置web.xml容器

11.创建Controller层

12.创建view层

13.打包,部署,运行项目

 

1.设计数据库,并创建数据库

1.1 使用powerDesigner,设计数据表,生成cdm

设计三个数据表分别为角色表,权限表以及用户表,三张表之间分别是多对多的关系。三个实体的主键均使用UUID,因此设计主键长度为varchar(36)

 

1.2 继续生成pdm

由于三张表之间是多对多,生成两张中间表,分别是角色权限表和用户角色表

 

 这个怎么用就不介绍了,总而言之,最后生成SQL语句,然后在MySQL运行,创建一个新的数据库,生成如下的数据库:

2.创建maven项目,导入项目所需依赖架包

2.1创建maven项目

 

新创建成的maven项目结构如下,我们将这个项目结构完善

首先将这个项目转化为web项目,项目右键-->properties,在设置过程中,注意Web版本与jave版本的相互支持

项目转成web项目之后,会生成下面的这个目录

我们要做的就是把这个WebRoot目录下的东西放到src下的webapp下,让它符合maven项目的结构标准.然后,删除WebRoot目录即可。

2.2 调一下项目中的小错误

首先双击pom.xml文件,设置web版本

 

双击Inxde.jsp文件,发现没有httpservlet架包,那我们就进入build path,给项目添加所支持架包

 

既然改到这里了,我们的JRE版本太低了,把它替换成高的版本,顺便把J2EE的架包也搞进来,以备不时之需

搞进来J2EE的lib

搞进来JRE的lib

此时,小错误全部消除完成,现在的项目结构如下所示:

 

2.3 现在配置pom.xml。导入项目依赖架包

1 
2
4.0.0
3
com.it
4
mySecond
5
0.0.1-SNAPSHOT
6
war
7
8
9
10
maven-war-plugin
11
12
3.0
13
14
15
16
17 18
myAgen
19
http://maven.apache.org
20 21
22
UTF-8
23
4.1.4.RELEASE
24
4.3.8.Final
25
2.5.0
26 27
28 29
30 31
32
33
junit
34
junit
35
4.12
36
test
37
38 39
40
41
org.springframework
42
spring-core
43
${spring.version}
44
45 46
47
org.springframework
48
spring-beans
49
${spring.version}
50
51 52
53
org.springframework
54
spring-context
55
${spring.version}
56
57 58
59
org.springframework
60
spring-tx
61
${spring.version}
62
63 64
65
org.springframework
66
spring-web
67
${spring.version}
68
69 70
71
org.springframework
72
spring-test
73
${spring.version}
74
test
75
76 77
78
79
org.springframework
80
spring-webmvc
81
${spring.version}
82
83
84
85
cglib
86
cglib
87
2.2.2
88
89
90
91
org.springframework
92
spring-orm
93
${spring.version}
94
95 96
97
98
org.hibernate
99
hibernate-core
100
${hibernate.version}
101
102 103
104
org.hibernate
105
hibernate-ehcache
106
${hibernate.version}
107
108 109
110
111
net.sf.ehcache
112
ehcache
113
2.9.0
114
115 116
117
118
log4j
119
log4j
120
1.2.17
121
122 123
124
125
mysql
126
mysql-connector-java
127
5.1.34
128
129 130
131
132
com.mchange
133
c3p0
134
0.9.5-pre10
135
136 137
138
139
com.alibaba
140
fastjson
141
1.2.3
142
143 144
145
com.fasterxml.jackson.core
146
jackson-annotations
147
${jackson.version}
148
149 150
151
com.fasterxml.jackson.core
152
jackson-core
153
${jackson.version}
154
155 156
157
com.fasterxml.jackson.core
158
jackson-databind
159
${jackson.version}
160
161 162
163
164
org.aspectj
165
aspectjweaver
166
1.8.4
167
168 169
170
171
javax.servlet
172
servlet-api
173
3.0-alpha-1
174
provided
175
176 177
178
javax.servlet
179
jstl
180
1.2
181
182 183
184
View Code

3.引入spring的配置信息,放在资源文件中

路径下创建config.properties文件,这里面存放的就是一堆的变量,有关hibernate配置的变量

 

1 #application configs 2  3 #jdbc c3p0 config 4 jdbc.driver = com.mysql.jdbc.Driver 5 jdbc.url = jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf-8 6 jdbc.username = root 7 jdbc.password = root 8  9 #hibernate config10 hibernate.dialect = org.hibernate.dialect.MySQLDialect11 hibernate.show_sql = true12 hibernate.format_sql = false13 hibernate.hbm2ddl.auto = update14 hibernate.cache.use_second_level_cache = true15 hibernate.cache.use_query_cache = true16 hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory17 hibernate.cache.provider_configuration_file_resource_path =ehcache.xml
View Code

 

 

 

 

4.引入Log4j日志文件配置

下创建log4j.properties,

 

1 ### set log levels ### 2 log4j.rootLogger = INFO , C , D , E  3  4 ### console ### 5 log4j.appender.C = org.apache.log4j.ConsoleAppender 6 log4j.appender.C.Target = System.out 7 log4j.appender.C.layout = org.apache.log4j.PatternLayout 8 log4j.appender.C.layout.ConversionPattern = [springmvc_hibernate_demo][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n 9 10 ### log file ###11 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender12 log4j.appender.D.File = ../logs/springmvc_hibernate_demo.log13 log4j.appender.D.Append = true14 log4j.appender.D.Threshold = INFO 15 log4j.appender.D.layout = org.apache.log4j.PatternLayout16 log4j.appender.D.layout.ConversionPattern = [springmvc_hibernate_demo][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n17 18 ### exception ###19 log4j.appender.E = org.apache.log4j.DailyRollingFileAppender20 log4j.appender.E.File = ../logs/springmvc_hibernate_demo_error.log 21 log4j.appender.E.Append = true22 log4j.appender.E.Threshold = ERROR 23 log4j.appender.E.layout = org.apache.log4j.PatternLayout24 log4j.appender.E.layout.ConversionPattern =[sspringmvc_hibernate_demo][%p] [%-d{yyyy-MM-dd HH\:mm\:ss}] %C.%M(%L) | %m%n
View Code

 

 

 

 

5.配置hibernate

下创建spring-hibernate.xml,其中配置了数据源dataSource,配置sessionFactory,配置事务管理器。【如果需要连接多干服务器上的多个数据库,那就可以再多配一份同样的dataSource,sessionFacrory,事务管理器,在注入的时候,在Controller中,@Resource(注明是哪个sessionFactory即可)这里可能有误,之后再说】

 

1 
2
11 12
13
14 15 16
17
19
20
21
22
23 24
25
26
27
28
29
30
31
32
33
34
35
36
37 38
39
41
42
43
44
45
46
${hibernate.dialect}
47
${hibernate.show_sql}
48
${hibernate.format_sql}
49
${hibernate.hbm2ddl.auto}
50 51
52
${hibernate.cache.use_second_level_cache}
53
${hibernate.cache.use_query_cache}
54
${hibernate.cache.region.factory_class}
55
${hibernate.cache.provider_configuration_file_resource_path}56
57
58
59
60
61
62 63
64
66
67
68 69 70 71 72 73 74
View Code

 

 

 

 

6.配置ehcache.xml

下创建ehcache.xml,配置的二级缓存

1 
2
3
4
5
13 14
15 16
17 18
26 27 28 29
View Code

 

 

7.使用hibernate  tools生成实体

在MyEcplise右上角,点击【如果没有,就点击MyEcplise Java Enterprise 左边的小图标】MyEcplise  Hibernate,进入

 

在这里,New一个新的连接。连接到指定的数据库上

编辑新的连接,下面的这些,必须完全一致,除了账号密码和驱动的路径,但是选择了驱动之后,Driver ClassName必须得是com.mysql.jdbc.Driver【如果是MySQL数据库的话】.

编辑完成之后可以Test Driver一下看看是否可以连接成功。

连接成功之后如下,我们就开始选择所有数据表,点击hibernate反转,生成entity

但是此刻发现我们要选择的folder中不能指定到我们的项目中,我们的项目没有被识别出来。

因此需要点击MyEcplise Jave Enterprise,回去设置我们的项目,让项目支持hibernate,或者说让项目变成hibernate项目

右键项目 --->properties--->Project Facets

 

 完了之后会有两个自动生成的文件,看看是否需要留着,或者删除

 然后再回到hibernate tools下面

这次就可以看到本项目了

因为要使用注解的形式,所以,按照下面的选择,

主键使用UUID,注意hibernate的版本的问题,需要注意选择的主键生成策略

 包下生成的实体如下:

 下面贴出来生成实体的代码:

Authority.java

1 package com.it.entity; 2  3 import java.util.HashSet; 4 import java.util.Set; 5 import javax.persistence.CascadeType; 6 import javax.persistence.Column; 7 import javax.persistence.Entity; 8 import javax.persistence.FetchType; 9 import javax.persistence.GeneratedValue;10 import javax.persistence.Id;11 import javax.persistence.ManyToMany;12 import javax.persistence.Table;13 import org.hibernate.annotations.GenericGenerator;14 15 /**16  * Authority entity. @author MyEclipse Persistence Tools17  */18 @Entity19 @Table(name = "authority", catalog = "testdb")20 public class Authority implements java.io.Serializable {21 22     // Fields23 24     private String authorityId;25     private String authorityName;26     private Set
roles = new HashSet
(0);27 28 // Constructors29 30 /** default constructor */31 public Authority() {32 }33 34 /** full constructor */35 public Authority(String authorityName, Set
roles) {36 this.authorityName = authorityName;37 this.roles = roles;38 }39 40 // Property accessors41 @GenericGenerator(name = "generator", strategy = "uuid.hex")42 @Id43 @GeneratedValue(generator = "generator")44 @Column(name = "authorityId", unique = true, nullable = false, length = 36)45 public String getAuthorityId() {46 return this.authorityId;47 }48 49 public void setAuthorityId(String authorityId) {50 this.authorityId = authorityId;51 }52 53 @Column(name = "authorityName", length = 50)54 public String getAuthorityName() {55 return this.authorityName;56 }57 58 public void setAuthorityName(String authorityName) {59 this.authorityName = authorityName;60 }61 62 @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "authorities")63 public Set
getRoles() {64 return this.roles;65 }66 67 public void setRoles(Set
roles) {68 this.roles = roles;69 }70 71 }
View Code

Myuser.java

1 package com.it.entity;  2   3 import java.sql.Timestamp;  4 import java.util.HashSet;  5 import java.util.Set;  6 import javax.persistence.CascadeType;  7 import javax.persistence.Column;  8 import javax.persistence.Entity;  9 import javax.persistence.FetchType; 10 import javax.persistence.GeneratedValue; 11 import javax.persistence.Id; 12 import javax.persistence.ManyToMany; 13 import javax.persistence.Table; 14 import org.hibernate.annotations.GenericGenerator; 15  16 /** 17  * Myuser entity. @author MyEclipse Persistence Tools 18  */ 19 @Entity 20 @Table(name = "myuser", catalog = "testdb") 21 public class Myuser implements java.io.Serializable { 22  23     // Fields 24  25     private String myUserId; 26     private String myUserName; 27     private String myUserPhone; 28     private Timestamp myUserTime; 29     private Set
roles = new HashSet
(0); 30 31 // Constructors 32 33 /** default constructor */ 34 public Myuser() { 35 } 36 37 /** minimal constructor */ 38 public Myuser(Timestamp myUserTime) { 39 this.myUserTime = myUserTime; 40 } 41 42 /** full constructor */ 43 public Myuser(String myUserName, String myUserPhone, Timestamp myUserTime, 44 Set
roles) { 45 this.myUserName = myUserName; 46 this.myUserPhone = myUserPhone; 47 this.myUserTime = myUserTime; 48 this.roles = roles; 49 } 50 51 // Property accessors 52 @GenericGenerator(name = "generator", strategy = "uuid.hex") 53 @Id 54 @GeneratedValue(generator = "generator") 55 @Column(name = "myUserId", unique = true, nullable = false, length = 36) 56 public String getMyUserId() { 57 return this.myUserId; 58 } 59 60 public void setMyUserId(String myUserId) { 61 this.myUserId = myUserId; 62 } 63 64 @Column(name = "myUserName", length = 20) 65 public String getMyUserName() { 66 return this.myUserName; 67 } 68 69 public void setMyUserName(String myUserName) { 70 this.myUserName = myUserName; 71 } 72 73 @Column(name = "myUserPhone", length = 11) 74 public String getMyUserPhone() { 75 return this.myUserPhone; 76 } 77 78 public void setMyUserPhone(String myUserPhone) { 79 this.myUserPhone = myUserPhone; 80 } 81 82 @Column(name = "myUserTime", nullable = false, length = 19) 83 public Timestamp getMyUserTime() { 84 return this.myUserTime; 85 } 86 87 public void setMyUserTime(Timestamp myUserTime) { 88 this.myUserTime = myUserTime; 89 } 90 91 @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "myusers") 92 public Set
getRoles() { 93 return this.roles; 94 } 95 96 public void setRoles(Set
roles) { 97 this.roles = roles; 98 } 99 100 }
View Code

Role.java

1 package com.it.entity; 2  3 import java.util.HashSet; 4 import java.util.Set; 5 import javax.persistence.CascadeType; 6 import javax.persistence.Column; 7 import javax.persistence.Entity; 8 import javax.persistence.FetchType; 9 import javax.persistence.GeneratedValue;10 import javax.persistence.Id;11 import javax.persistence.JoinColumn;12 import javax.persistence.JoinTable;13 import javax.persistence.ManyToMany;14 import javax.persistence.Table;15 import org.hibernate.annotations.GenericGenerator;16 17 /**18  * Role entity. @author MyEclipse Persistence Tools19  */20 @Entity21 @Table(name = "role", catalog = "testdb")22 public class Role implements java.io.Serializable {23 24     // Fields25 26     private String roleId;27     private String roleName;28     private Set
myusers = new HashSet
(0);29 private Set
authorities = new HashSet
(0);30 31 // Constructors32 33 /** default constructor */34 public Role() {35 }36 37 /** full constructor */38 public Role(String roleName, Set
myusers, Set
authorities) {39 this.roleName = roleName;40 this.myusers = myusers;41 this.authorities = authorities;42 }43 44 // Property accessors45 @GenericGenerator(name = "generator", strategy = "uuid.hex")46 @Id47 @GeneratedValue(generator = "generator")48 @Column(name = "roleId", unique = true, nullable = false, length = 36)49 public String getRoleId() {50 return this.roleId;51 }52 53 public void setRoleId(String roleId) {54 this.roleId = roleId;55 }56 57 @Column(name = "roleName", length = 20)58 public String getRoleName() {59 return this.roleName;60 }61 62 public void setRoleName(String roleName) {63 this.roleName = roleName;64 }65 66 @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)67 @JoinTable(name = "userrole", catalog = "testdb", joinColumns = { @JoinColumn(name = "roleId", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "myUserId", nullable = false, updatable = false) })68 public Set
getMyusers() {69 return this.myusers;70 }71 72 public void setMyusers(Set
myusers) {73 this.myusers = myusers;74 }75 76 @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)77 @JoinTable(name = "roleauthor", catalog = "testdb", joinColumns = { @JoinColumn(name = "roleId", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "authorityId", nullable = false, updatable = false) })78 public Set
getAuthorities() {79 return this.authorities;80 }81 82 public void setAuthorities(Set
authorities) {83 this.authorities = authorities;84 }85 86 }
View Code

上面有几个需要注意的

 

     上面大多数都是Hibernate tools自动生成的,但是有几个需要手动添加:

              1、如果这个实体使用二级缓存的话,需要添加@cache注解;

              2、如果该实体里面还有集合元素(set, map,list),如果实体要采用二级缓存,那么这些集合元素必须也添加@cache注解

              3、@JsonIgnoreProperties注解是为了防止SpringMVC在json返回时产生循环输出使用,如果不配置的话就会出现json死循环(还有多对多,一对多的属性时)。

             4、@JsonIgnore这个是在转换成json是忽略该属性,而@JsonIgnoreProperties(value={"acctUsers", "acctAuthorities"})则是忽略acctRoles里面的acctUsers和acctAuthorites属性。

 

8.创建Dao层、service层

8.1 创建Dao层,面向接口的思想创建

8.1.1  先要创建一个基础Dao

1 package com.it.dao; 2  3 import java.io.Serializable; 4 import java.util.List; 5  6 public interface GenericDao
{ 7 8 T load(PK id); 9 10 T get(PK id);11 12 List
findAll();13 14 void persist(T entity);15 16 PK save(T entity);17 18 void saveOrUpdate(T entity);19 20 void delete(PK id);21 22 void flush();23 }
View Code

8.1.2 再定义具体的接口,继承基础的Dao

1 package com.it.dao; 2  3 import com.it.entity.Myuser; 4  5 /** 6  * MyUser的Dao 7  * @author Administrator 8  * 9  */10 public interface MyUserDao extends GenericDao
{11 12 }
View Code

8.1.3 接着定义具体的DaoImpl的实现类

1 package com.it.dao.Impl; 2  3 import java.util.List; 4  5  6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.stereotype.Repository;10 11 import com.it.dao.MyUserDao;12 import com.it.entity.Myuser;13 14 15 @Repository("myUserDao")16 public class MyUserDaoImpl implements MyUserDao {17     18     @Autowired19     private SessionFactory sessionFactory;20     21     22     private Session getCurrentSession() {23         return this.sessionFactory.getCurrentSession();24     }25 26     public Myuser load(String id) {27         // TODO Auto-generated method stub28         return (Myuser) this.getCurrentSession().load(Myuser.class, id);29     }30 31     public Myuser get(String id) {32         // TODO Auto-generated method stub33         return (Myuser) this.getCurrentSession().get(Myuser.class, id);34     }35 36     public List
findAll() {37 @SuppressWarnings("unchecked")38 List
allUser = this.getCurrentSession().createQuery("from Myuser").list();39 return allUser;40 }41 42 public void persist(Myuser entity) {43 this.getCurrentSession().persist(entity);44 }45 46 /**47 * 可以选择手动控制事务的提交,也可以交给spring自动去提交48 */49 public String save(Myuser entity) {50 // TODO Auto-generated method stub51 // Session session = sessionFactory.openSession();52 // session.getTransaction().begin();53 // session.;54 // session.getTransaction().commit();55 // session.flush();56 // session.close();57 return (String) this.getCurrentSession().save(entity);58 }59 60 public void saveOrUpdate(Myuser entity) {61 // TODO Auto-generated method stub62 this.getCurrentSession().saveOrUpdate(entity);63 }64 65 public void delete(String id) {66 // TODO Auto-generated method stub67 Myuser myuser=this.load(id);68 this.getCurrentSession().delete(myuser);69 }70 71 public void flush() {72 this.getCurrentSession().flush();73 }74 75 }
View Code

8.2 创建service层,也是面向接口的思想创建

8.2.1 创建一个基础Service

1 package com.it.service; 2  3 import java.io.Serializable; 4 import java.util.List; 5  6 public interface GenericService
{ 7 8 T load(PK id); 9 10 T get(PK id);11 12 List
findAll();13 14 void persist(T entity);15 16 PK save(T entity);17 18 void saveOrUpdate(T entity);19 20 void delete(PK id);21 22 void flush();23 }
View Code

8.2.2 创建service继承基础的Service

1 package com.it.service;2 3 import com.it.entity.Myuser;4 5 public interface MyUserService extends GenericService
{6 7 }
View Code

8.2.3 创建serviceImpl

1 package com.it.service.Impl; 2  3 import java.util.List; 4  5 import javax.annotation.Resource; 6  7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Service; 9 import org.springframework.transaction.annotation.Transactional;10 11 import com.it.dao.MyUserDao;12 import com.it.entity.Myuser;13 import com.it.service.GenericService;14 import com.it.service.MyUserService;15 16 17 @Service("myUserService")18 @Transactional19 public class MyUserServiceImpl implements MyUserService {20     21     @Resource(name = "myUserDao")22     private MyUserDao myUserDao;23     24 25     public Myuser load(String id) {26         // TODO Auto-generated method stub27         return myUserDao.load(id);28     }29 30     public Myuser get(String id) {31         // TODO Auto-generated method stub32         return myUserDao.get(id);33     }34 35     public List
findAll() {36 // TODO Auto-generated method stub37 return myUserDao.findAll();38 }39 40 public void persist(Myuser entity) {41 // TODO Auto-generated method stub42 myUserDao.persist(entity);43 }44 45 public String save(Myuser entity) {46 // TODO Auto-generated method stub47 return myUserDao.save(entity);48 }49 50 public void saveOrUpdate(Myuser entity) {51 // TODO Auto-generated method stub52 myUserDao.saveOrUpdate(entity);53 }54 55 public void delete(String id) {56 // TODO Auto-generated method stub57 myUserDao.delete(id);58 }59 60 public void flush() {61 // TODO Auto-generated method stub62 myUserDao.flush();63 }64 65 }
View Code

 

9.配置spring-mvc.xml

spring-mvc.xml中配置了自动扫描Controller层注解+视图解析器

 

1 
2
14 15
16
17 18
19
20
21 22
23
24
25
26
28
29
30
text/html;charset=UTF-8
31
application/json;charset=UTF-8
32
33
34 35 36
37
38 39
40
42
44
45
46 47 48
84 85
View Code

 

10.配置web.xml容器

 

1 
2
6
7
11
springmvc_demo
12
13
14
15
contextConfigLocation
16
classpath:spring-hibernate.xml
17
18
19
20
encodingFilter
21
org.springframework.web.filter.CharacterEncodingFilter
22
23
encoding
24
utf-8
25
26
27
forceEncoding
28
true
29
30
31
32
encodingFilter
33
/*
34
35 36
37
38
openSessionInViewFilter
39
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
40
41
singleSession
42
true
43
44
45 46
47
48
org.springframework.web.context.ContextLoaderListener
49
50 51 52
53
54
org.springframework.web.util.IntrospectorCleanupListener
55
56 57 58
59
60
61
spring mvc servlet
62
rest
63
org.springframework.web.servlet.DispatcherServlet
64
65
contextConfigLocation
66
67
classpath:spring-mvc.xml
68
69
1
70
71
72
73
rest
74
*.htmls
75
76 77
78
openSessionInViewFilter
79
/*
80
81 82
83
84
30
85
86
87
88
/index.jsp
89
90
View Code

 

11.创建Controller层

 

1 package com.it.controller; 2  3 import java.sql.Timestamp; 4 import java.util.List; 5 import java.util.UUID; 6  7 import javax.annotation.Resource; 8  9 import org.apache.log4j.Logger;10 import org.springframework.stereotype.Controller;11 import org.springframework.ui.ModelMap;12 import org.springframework.web.bind.annotation.PathVariable;13 import org.springframework.web.bind.annotation.RequestMapping;14 import org.springframework.web.bind.annotation.ResponseBody;15 16 import com.it.entity.Myuser;17 import com.it.service.MyUserService;18 19 20 21 22 @Controller23 @RequestMapping("/myuser")24 public class MyUserController {25     26     private static final Logger LOGGER = Logger.getLogger(MyUserController.class);27     28     29     @Resource( name = "myUserService")30     private MyUserService  myUserService;31     32     33     @RequestMapping("/saveOne")34     public String saveOne(){35         Myuser myuser = new Myuser();36         //myuser.setMyUserId(UUID.randomUUID().toString());37         myuser.setMyUserName("andy");38         myuser.setMyUserPhone("13823237878");39         myuser.setMyUserTime(Timestamp.valueOf("1995-09-15 12:12:12"));40         myUserService.save(myuser);41         return "/user/showInfo";42     }43     44     @RequestMapping("/showInfo/{userId}")45     public String showUserInfo(ModelMap model, @PathVariable String userId){46         LOGGER.info("查询用户:"+userId);47         Myuser myuser = myUserService.load(userId);48         model.addAttribute("userInfo", myuser);49         return "/user/showInfo";50     }51     52     53     @RequestMapping("/showInfos")54     public @ResponseBody List
showAllUser(){55 LOGGER.info("查询所有用户");56 List
list = myUserService.findAll();57 return list;58 }59 }
View Code

 

 

12.创建view视图层

在src/main/webapp/WEB-INF/views下 创建user/showInfo.jsp

1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 4  5 <% 6     String path = request.getContextPath(); 7     String basePath = request.getScheme() + "://" 8             + request.getServerName() + ":" + request.getServerPort() 9             + path + "/";10 %>11 12 13 14 15 
16 17 用户信息展示18 19 20 用户信息 昵称: ${userInfo.myUserName} 用户id:${userInfo.myUserId} 用户电话:${userInfo.myUserPhone } 注册时间:21
22 角色:[23
24 ${role.roleName } 权限[25
26 ${authority.authorityName } 27
]28
29 ]30 31
ajax显示全部用户信息:32
33 34 52
View Code

 13. 一切准备就绪,把项目部署到tomcat中,启动即可

 

可能你会说,那要这个view页面做什么??因为参考别人的嘛 并且,这个页面比较值得钻研

转载地址:http://ngcmo.baihongyu.com/

你可能感兴趣的文章
如何从Spotify Music中删除DRM?
查看>>
VR开发者为Labo VR辩护 预计这可能是任天堂进军VR的开始
查看>>
全面解析大数据框架Hadoop主要模块
查看>>
手写调用门
查看>>
海恩法则与墨菲定律
查看>>
linux RHEL 解决中文网页乱码和界面英文
查看>>
linux中oracle的日常维护命令
查看>>
Linux 修改IP地址和网关
查看>>
linux查看硬件信息
查看>>
apache http的源码编译
查看>>
find命令的参数
查看>>
H3C交换机配置镜像端口
查看>>
ESXI6.0(6.7)实践——惠普A6 7310主板,APU,Realtek网卡的安装之路
查看>>
我的友情链接
查看>>
26期学员参观森华易腾移动IDC机房有感
查看>>
三、一个简单的BDB JE例子
查看>>
在Windows Server2008R2安装Oracle Database 11g Release 2
查看>>
借助mysql和DNS view实现智能DNS(centos6.3 x64环境)
查看>>
维纳-辛钦 (Wiener–Khinchin) 定理
查看>>
修改mysql的数据库密码
查看>>