源码地址:http://download.csdn.net/download/a295277302/9829063
1.项目结构截图
对pom.xml文件添加jar
之后配置web.xml 文件
添加SpringMvc配置文件
在WEB-INF下添加
springDispatcherServlet-servlet.xml
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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-4.0.xsd">
添加src下的资源目录下Spring Bean文件
applicationContext.xml
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
添加数据库连接信息 >>>>> db.properties
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring
添加缓存ehcache >>>> ehcache.xml
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/> -->
添加测试类 >>> SSSPTest 测试与数据库的连通情况
package com.htf.sssp.test;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SSSPTest {
private ApplicationContext ctx = null;
{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void testDataSource() throws SQLException {
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
}
如果成功会显示:
添加实体类 >>>> Employee
package com.htf.sssp.entity;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.springframework.format.annotation.DateTimeFormat;
@Table(name="SSSP_EMPLOYEES")
@Entity
public class Employee {
private Integer id;
private String lastName;
private String email;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth;
private Date createTime;
private Department department;
@GeneratedValue
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Temporal(TemporalType.DATE)
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Temporal(TemporalType.TIMESTAMP)
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@JoinColumn(name="DEPARTMENT_ID")
@ManyToOne(fetch=FetchType.LAZY)
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
添加实体类 >>>> Department
package com.htf.sssp.entity;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Cacheable
@Table(name="SSSP_DEPARTMENTS")
@Entity
public class Department {
private Integer id;
private String departmentName;
@GeneratedValue
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
添加Handler >>>>> EmployeeHandler
package com.htf.sssp.haderln;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
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.bind.annotation.ResponseBody;
import com.htf.sssp.entity.Employee;
import com.htf.sssp.service.DepartmentService;
import com.htf.sssp.service.EmployeeService;
@Controller
public class EmployeeHandler {
@Autowired
private EmployeeService employeeService;
@Autowired
private DepartmentService departmentService;
@RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
employeeService.delete(id);
return "redirect:/emps";
}
@ModelAttribute
public void getEmployee(@RequestParam(value="id",required=false) Integer id,
Map
if(id != null){
Employee employee = employeeService.get(id);
employee.setDepartment(null);
map.put("employee", employee);
}
}
@RequestMapping(value="/emp/{id}",method=RequestMethod.PUT)
public String update(Employee employee){
employeeService.save(employee);
return "redirect:/emps";
}
@RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
public String input(@PathVariable("id") Integer id, Map
Employee employee = employeeService.get(id);
map.put("employee", employee);
map.put("departments", departmentService.getAll());
return "emp/input";
}
@RequestMapping(value="/emp",method=RequestMethod.POST)
public String save(Employee employee){
employeeService.save(employee);
return "redirect:/emps";
}
@ResponseBody
@RequestMapping(value="/ajaxValidateLastName",method=RequestMethod.POST)
public String validateLastName(@RequestParam(value="lastName",required=true) String lastName){
Employee employee = employeeService.getByLastName(lastName);
if(employee == null){
return "0";
}else{
return "1";
}
}
@RequestMapping(value="/emp",method=RequestMethod.GET)
public String input(Map
map.put("departments", departmentService.getAll());
map.put("employee", new Employee());
return "emp/input";
}
@RequestMapping("/emps")
public String list(@RequestParam(value="pageNo", required=false, defaultValue="1") String pageNoStr,
Map
int pageNo = 1;
try {
//¶Ô pageNo µÄУÑé
pageNo = Integer.parseInt(pageNoStr);
if(pageNo < 1){
pageNo = 1;
}
} catch (Exception e) {}
Page
map.put("page", page);
return "emp/list";
}
}
添加repository >>>>> DepartmentRepository , EmployeeRepository
package com.htf.sssp.repository;
import java.util.List;
import javax.persistence.QueryHint;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import com.htf.sssp.entity.Department;
public interface DepartmentRepository extends JpaRepository
@QueryHints({@QueryHint(name=org.hibernate.ejb.QueryHints.HINT_CACHEABLE,value="true")})
@Query("FROM Department d")
List
}
package com.htf.sssp.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.htf.sssp.entity.Employee;
public interface EmployeeRepository extends JpaRepository
Employee getByLastName(String lastName);
}
添加Service >>>> DepartmentService , EmployeeService
package com.htf.sssp.service;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.htf.sssp.entity.Employee;
import com.htf.sssp.repository.EmployeeRepository;
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Transactional
public void delete(Integer id){
employeeRepository.delete(id);
}
@Transactional(readOnly=true)
public Employee get(Integer id){
return employeeRepository.findOne(id);
}
@Transactional
public void save(Employee employee){
if(employee.getId() == null){
employee.setCreateTime(new Date());
}
employeeRepository.saveAndFlush(employee);
}
@Transactional(readOnly=true)
public Employee getByLastName(String lastName){
return employeeRepository.getByLastName(lastName);
}
@Transactional(readOnly=true)
public Page
PageRequest pageable = new PageRequest(pageNo - 1, pageSize);
return employeeRepository.findAll(pageable);
}
}
package com.htf.sssp.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.htf.sssp.entity.Department;
import com.htf.sssp.repository.DepartmentRepository;
@Service
public class DepartmentService {
@Autowired
private DepartmentRepository departmentRepository;
@Transactional(readOnly=true)
public List
return departmentRepository.getAll();
}
}
剩下的就是添加Handerln 返回的jsp页面
WEB-INF 下的 view 之后添加jquery.js
可参考源码,此处不再赘述。
将项目打包到TOMCAT
启动tomcat 看到这个说明成功
打开google 输入 http://localhost:8080/sssp/
添加页面
列表页面
具体样式自己去改。
源码路径:http://download.csdn.net/download/a295277302/9829063