github:https://github.com/leelun/ssjpa.git
1 配置项目需要依赖包(pom.xml)
4.0.0
com.newland
ssjpa
war
0.0.1-SNAPSHOT
ssjpa Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
3.1.0
provided
org.springframework
spring-webmvc
4.3.7.RELEASE
org.springframework
spring-jdbc
4.3.7.RELEASE
org.springframework
spring-test
4.3.7.RELEASE
org.springframework
spring-aspects
4.3.7.RELEASE
org.springframework
spring-orm
4.3.7.RELEASE
c3p0
c3p0
0.9.1
org.hibernate
hibernate-core
4.3.0.Final
org.hibernate
hibernate-entitymanager
4.3.0.Final
org.hibernate
hibernate-c3p0
4.3.0.Final
mysql
mysql-connector-java
5.1.41
com.fasterxml.jackson.core
jackson-databind
2.8.8
ssjpa
2 web.xml配置
Archetype Created Web Application
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-servlet.xml
1
spring
/
3 springmvc配置文件spring-servlet.xml配置
4 spring配置文件applicationContext.xml配置
true
true
update
5 entity
package com.newland.jpa.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name="JPA_PERSONS")
@Entity
public class Person {
private Integer id;
private String lastName;
private String email;
private int age;
@GeneratedValue
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="LAST_NAME")
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
6 dao数据库数据获取
package com.newland.jpa.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import com.newland.jpa.entity.Person;
@Repository
public class PersonDao {
//通过 @PersistenceContext 注解来标记成员变量!
@PersistenceContext
private EntityManager entityManager;
@SuppressWarnings("unchecked")
public List getPersons() {
return (List) entityManager.createQuery("SELECT p FROM Person p").getResultList();
}
}
7 service业务处理
package com.newland.jpa.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.newland.jpa.dao.PersonDao;
import com.newland.jpa.entity.Person;
@Service
public class PersonService {
@Autowired
private PersonDao personDao;
public List getPersons(){
return personDao.getPersons();
}
}
8 controller请求处理
package com.newland.jpa.controller;
import java.util.List;
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.ResponseBody;
import com.newland.jpa.entity.Person;
import com.newland.jpa.service.PersonService;
@Controller
public class PersonController {
@Autowired
private PersonService personService;
@ResponseBody
@RequestMapping("/persons")
public List persons() {
return personService.getPersons();
}
}
github:https://github.com/leelun/ssjpa.git