Skip to content

Commit 8a04ac5

Browse files
committed
init
0 parents  commit 8a04ac5

File tree

68 files changed

+1409
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1409
-0
lines changed

demo.iml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="web" name="Web">
5+
<configuration>
6+
<descriptors>
7+
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml" />
8+
</descriptors>
9+
<webroots>
10+
<root url="file://$MODULE_DIR$/src/main/webapp" relative="/" />
11+
</webroots>
12+
</configuration>
13+
</facet>
14+
<facet type="Spring" name="Spring">
15+
<configuration>
16+
<fileset id="fileset" name="Spring Application Context" removed="false">
17+
<file>file://$MODULE_DIR$/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml</file>
18+
</fileset>
19+
</configuration>
20+
</facet>
21+
<facet type="jpa" name="JPA">
22+
<configuration>
23+
<setting name="validation-enabled" value="true" />
24+
<setting name="provider-name" value="" />
25+
<datasource-mapping>
26+
<factory-entry name="defaultPersistenceUnit" />
27+
<factory-entry name="noname-a.xml@0" value="c0cc93d0-8be0-4cfa-a804-adaee631058c" />
28+
</datasource-mapping>
29+
<deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/main/resources/META-INF/persistence.xml" />
30+
</configuration>
31+
</facet>
32+
</component>
33+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
34+
<output url="file://$MODULE_DIR$/target/classes" />
35+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
36+
<content url="file://$MODULE_DIR$">
37+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
38+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
39+
<excludeFolder url="file://$MODULE_DIR$/target" />
40+
</content>
41+
<orderEntry type="inheritedJdk" />
42+
<orderEntry type="sourceFolder" forTests="false" />
43+
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:3.8.1" level="project" />
44+
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.2.2.RELEASE" level="project" />
45+
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
46+
<orderEntry type="library" name="Maven: org.springframework:spring-web:4.2.2.RELEASE" level="project" />
47+
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.2.2.RELEASE" level="project" />
48+
<orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" />
49+
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.2.2.RELEASE" level="project" />
50+
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.2.2.RELEASE" level="project" />
51+
<orderEntry type="library" name="Maven: javax.servlet:servlet-api:2.5" level="project" />
52+
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet.jsp:jsp-api:2.1" level="project" />
53+
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.2.2.RELEASE" level="project" />
54+
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.2.2.RELEASE" level="project" />
55+
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:4.2.2.RELEASE" level="project" />
56+
<orderEntry type="library" name="Maven: jstl:jstl:1.2" level="project" />
57+
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-jpa:1.2.0.RELEASE" level="project" />
58+
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons-core:1.4.0.RELEASE" level="project" />
59+
<orderEntry type="library" name="Maven: org.springframework:spring-orm:3.1.2.RELEASE" level="project" />
60+
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:3.1.2.RELEASE" level="project" />
61+
<orderEntry type="library" name="Maven: org.springframework:spring-tx:3.1.2.RELEASE" level="project" />
62+
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.6.1" level="project" />
63+
<orderEntry type="library" scope="RUNTIME" name="Maven: org.slf4j:jcl-over-slf4j:1.6.1" level="project" />
64+
<orderEntry type="library" name="Maven: org.aspectj:aspectjrt:1.6.12" level="project" />
65+
<orderEntry type="library" name="Maven: org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final" level="project" />
66+
<orderEntry type="library" name="Maven: org.hibernate:hibernate-entitymanager:3.6.10.Final" level="project" />
67+
<orderEntry type="library" name="Maven: org.hibernate:hibernate-core:3.6.10.Final" level="project" />
68+
<orderEntry type="library" name="Maven: antlr:antlr:2.7.6" level="project" />
69+
<orderEntry type="library" name="Maven: commons-collections:commons-collections:3.1" level="project" />
70+
<orderEntry type="library" name="Maven: dom4j:dom4j:1.6.1" level="project" />
71+
<orderEntry type="library" name="Maven: org.hibernate:hibernate-commons-annotations:3.2.0.Final" level="project" />
72+
<orderEntry type="library" name="Maven: javax.transaction:jta:1.1" level="project" />
73+
<orderEntry type="library" name="Maven: cglib:cglib:2.2" level="project" />
74+
<orderEntry type="library" name="Maven: asm:asm:3.1" level="project" />
75+
<orderEntry type="library" name="Maven: javassist:javassist:3.12.0.GA" level="project" />
76+
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.34" level="project" />
77+
<orderEntry type="library" name="Maven: org.json:json:20080701" level="project" />
78+
</component>
79+
</module>

pom.xml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.cenyol</groupId>
5+
<artifactId>example</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>example Maven Webapp</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<properties>
12+
<spring.version>4.2.2.RELEASE</spring.version>
13+
<spring-data.version>1.2.0.RELEASE</spring-data.version>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>3.8.1</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework</groupId>
25+
<artifactId>spring-core</artifactId>
26+
<version>${spring.version}</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework</groupId>
31+
<artifactId>spring-web</artifactId>
32+
<version>${spring.version}</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>javax.servlet</groupId>
37+
<artifactId>servlet-api</artifactId>
38+
<version>2.5</version>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>javax.servlet.jsp</groupId>
43+
<artifactId>jsp-api</artifactId>
44+
<version>2.1</version>
45+
<scope>provided</scope>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.springframework</groupId>
50+
<artifactId>spring-webmvc</artifactId>
51+
<version>${spring.version}</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.springframework</groupId>
56+
<artifactId>spring-test</artifactId>
57+
<version>${spring.version}</version>
58+
<scope>test</scope>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>jstl</groupId>
63+
<artifactId>jstl</artifactId>
64+
<version>1.2</version>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.springframework.data</groupId>
69+
<artifactId>spring-data-jpa</artifactId>
70+
<version>${spring-data.version}</version>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>org.hibernate.javax.persistence</groupId>
75+
<artifactId>hibernate-jpa-2.0-api</artifactId>
76+
<version>1.0.0.Final</version>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>org.hibernate</groupId>
81+
<artifactId>hibernate-entitymanager</artifactId>
82+
<version>3.6.10.Final</version>
83+
</dependency>
84+
85+
<dependency>
86+
<groupId>mysql</groupId>
87+
<artifactId>mysql-connector-java</artifactId>
88+
<version>5.1.34</version>
89+
</dependency>
90+
91+
<dependency>
92+
<groupId>org.json</groupId>
93+
<artifactId>json</artifactId>
94+
<version>20080701</version>
95+
</dependency>
96+
</dependencies>
97+
<build>
98+
<finalName>example</finalName>
99+
</build>
100+
</project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.cenyol.example.controller;
2+
3+
import com.cenyol.example.model.UserEntity;
4+
import com.cenyol.example.repository.UserRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.ModelMap;
8+
import org.springframework.web.bind.annotation.ModelAttribute;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
13+
import java.lang.reflect.Method;
14+
import java.util.List;
15+
16+
/**
17+
* Created by sjj on 2015/10/24 0024.
18+
*/
19+
@Controller
20+
public class MainController {
21+
22+
// 自动装配
23+
@Autowired
24+
private UserRepository userRepository;
25+
26+
// 首页
27+
@RequestMapping(value = "/", method = RequestMethod.GET)
28+
public String index() {
29+
return "index";
30+
}
31+
32+
// 用户管理
33+
@RequestMapping(value = "/users", method = RequestMethod.GET)
34+
public String users(ModelMap modelMap){
35+
// 找到user表里面的所有记录
36+
List<UserEntity> userEntityList = userRepository.findAll();
37+
38+
// 将所有的记录传递给返回的jsp页面
39+
modelMap.addAttribute("userList", userEntityList);
40+
41+
// 返回pages目录下的userManage.jsp
42+
return "userManage";
43+
}
44+
45+
// 添加用户表单页面
46+
@RequestMapping(value = "/addUser", method = RequestMethod.GET)
47+
public String addUser(){
48+
return "addUser";
49+
}
50+
51+
// 添加用户处理
52+
@RequestMapping(value = "/addUserPost", method = RequestMethod.POST)
53+
public String addUserPost(@ModelAttribute("user") UserEntity userEntity){
54+
// 向数据库添加一个用户
55+
//userRepository.save(userEntity);
56+
57+
// 向数据库添加一个用户,并将内存中缓存区的数据刷新,立即写入数据库,之后才可以进行访问读取
58+
userRepository.saveAndFlush(userEntity);
59+
60+
// 返回重定向页面
61+
return "redirect:/users";
62+
}
63+
64+
// 查看用户详细信息
65+
// @PathVariable可以收集url中的变量,需匹配的变量用{}括起来
66+
// 例如:访问 localhost:8080/showUser/1 ,将匹配 userId = 1
67+
@RequestMapping(value = "/showUser/{userId}", method = RequestMethod.GET)
68+
public String showUser( @PathVariable("userId") Integer userId, ModelMap modelMap ){
69+
UserEntity userEntity = userRepository.findOne(userId);
70+
modelMap.addAttribute("user", userEntity);
71+
return "userDetail";
72+
}
73+
74+
// 更新用户信息页面
75+
@RequestMapping(value = "/updateUser/{userId}", method = RequestMethod.GET)
76+
public String updateUser(@PathVariable("userId") Integer userId, ModelMap modelMap){
77+
UserEntity userEntity = userRepository.findOne(userId);
78+
modelMap.addAttribute("user", userEntity);
79+
return "updateUser";
80+
}
81+
// 处理用户修改请求
82+
@RequestMapping(value = "/updateUserPost", method = RequestMethod.POST)
83+
public String updateUserPost(@ModelAttribute("user") UserEntity userEntity){
84+
userRepository.updateUser(
85+
userEntity.getFirstName(),
86+
userEntity.getLastName(),
87+
userEntity.getPassword(),
88+
userEntity.getId()
89+
);
90+
return "redirect:/users";
91+
}
92+
93+
// 删除用户
94+
@RequestMapping(value = "/deleteUser/{userId}", method = RequestMethod.GET)
95+
public String deleteUser(@PathVariable("userId") Integer userId){
96+
// 删除id为userId的用户
97+
userRepository.delete(userId);
98+
// 立即刷新数据库
99+
userRepository.flush();
100+
return "redirect:/users";
101+
}
102+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.cenyol.example.model;
2+
3+
import javax.persistence.*;
4+
5+
/**
6+
* Created by sjj on 2015/10/24 0024.
7+
*/
8+
@Entity
9+
@Table(name = "blog", schema = "", catalog = "test")
10+
public class BlogEntity {
11+
private int id;
12+
private String title;
13+
private String content;
14+
private UserEntity userByUserid;
15+
16+
@Id
17+
@Column(name = "id", nullable = false, insertable = true, updatable = true)
18+
public int getId() {
19+
return id;
20+
}
21+
22+
public void setId(int id) {
23+
this.id = id;
24+
}
25+
26+
@Basic
27+
@Column(name = "title", nullable = true, insertable = true, updatable = true, length = 100)
28+
public String getTitle() {
29+
return title;
30+
}
31+
32+
public void setTitle(String title) {
33+
this.title = title;
34+
}
35+
36+
@Basic
37+
@Column(name = "content", nullable = true, insertable = true, updatable = true, length = 255)
38+
public String getContent() {
39+
return content;
40+
}
41+
42+
public void setContent(String content) {
43+
this.content = content;
44+
}
45+
46+
@Override
47+
public boolean equals(Object o) {
48+
if (this == o) return true;
49+
if (o == null || getClass() != o.getClass()) return false;
50+
51+
BlogEntity that = (BlogEntity) o;
52+
53+
if (id != that.id) return false;
54+
if (title != null ? !title.equals(that.title) : that.title != null) return false;
55+
if (content != null ? !content.equals(that.content) : that.content != null) return false;
56+
57+
return true;
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
int result = id;
63+
result = 31 * result + (title != null ? title.hashCode() : 0);
64+
result = 31 * result + (content != null ? content.hashCode() : 0);
65+
return result;
66+
}
67+
68+
@ManyToOne
69+
@JoinColumn(name = "userid", referencedColumnName = "id")
70+
public UserEntity getUserByUserid() {
71+
return userByUserid;
72+
}
73+
74+
public void setUserByUserid(UserEntity userByUserid) {
75+
this.userByUserid = userByUserid;
76+
}
77+
}

0 commit comments

Comments
 (0)