完整的spring-boot实现
工具:jdk1.7 -- eclipse -- Maven
目的:实现通过部门ID查询部门及所有员工信息
目录结构:
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
Insert title here
DeptNo.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
Insert title here
DeptAndEmp.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Insert title here
编号
姓名
雇佣日期
薪水
所属部门
部门地址
${h.eid}
${h.ename}
${h.hiredate}
${h.salery}
${allList.dname}
${allList.addres}
配置环境文件:
web.xml
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/springmvc-servlet.xml
1
springmvc
/
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceEncoding
true
CharacterEncodingFilter
/
Spring OpenEntityManagerInViewFilter
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
entityManagerFactoryBeanName
entityManagerFactory
Spring OpenEntityManagerInViewFilter
/*
springmvc-servlet.xml
applicationContext.xml
true
update
true
true
org.hibernate.cfg.ImprovedNamingStrategy
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/db_spring_mvc_jpa?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
pom.xml
4.0.0
com.third.spring-mvc-jpa
spring-mvc-jpa
0.0.1-SNAPSHOT
jar
spring-mvc-jpa
http://maven.apache.org
UTF-8
junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
3.1.0
org.springframework
spring-webmvc
4.1.3.RELEASE
org.springframework
spring-context
4.1.3.RELEASE
org.springframework
spring-beans
4.1.3.RELEASE
org.springframework
spring-web
4.1.3.RELEASE
org.springframework
spring-jdbc
4.1.3.RELEASE
org.springframework
spring-aop
4.1.3.RELEASE
org.springframework
spring-tx
4.1.3.RELEASE
org.springframework
spring-core
4.1.3.RELEASE
org.springframework
spring-orm
4.1.3.RELEASE
org.hibernate
hibernate-core
4.3.11.Final
org.hibernate
hibernate-entitymanager
4.3.11.Final
org.hibernate
hibernate-ehcache
4.3.11.Final
mysql
mysql-connector-java
5.1.35
commons-dbcp
commons-dbcp
1.2.2
javax.servlet
jstl
1.2
org.slf4j
slf4j-api
1.7.12
com.jolbox
bonecp
0.8.0.RELEASE
com.jolbox
bonecp-spring
0.8.0.RELEASE
com.jolbox
bonecp-provider
0.8.0-alpha1
taglibs
standard
1.1.2
org.aspectj
aspectjweaver
1.8.7
cglib
cglib-nodep
3.2.0
commons-collections
commons-collections
3.2.1
ant
ant
1.6.5
org.apache.avro
avro
1.7.7
commons-beanutils
commons-beanutils
1.9.2
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
org.apache.commons
commons-lang3
3.4
dom4j
dom4j
1.6.1
net.sf.ezmorph
ezmorph
1.0.6
org.freemarker
freemarker
2.3.23
com.google.guava
guava
18.0
org.javassist
javassist
3.20.0-GA
org.jboss.logging
jboss-logging
3.3.0.Final
com.fasterxml.jackson.core
jackson-databind
2.5.0
com.fasterxml.jackson.core
jackson-core
2.5.0
com.fasterxml.jackson.core
jackson-annotations
2.5.0
org.springframework.data
spring-data-jpa
1.7.1.RELEASE
com.google.code.gson
gson
2.8.2
org.apache.maven.plugins
maven-compiler-plugin
1.7
控制层
DeptController.java
package com.java.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
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.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.java.model.Dept;
import com.java.service.DeptService;
/*
*
* Dept查询是通过自定义sql语句查询
*
*/
@RestController
public class DeptController {
@Autowired
private DeptService deptservice;
//查询所有部门信息
@RequestMapping("getAllDept")
public ModelAndView getAllDept() {
ModelAndView model = new ModelAndView();
model.addObject("listDept",deptservice.getAllDept() );
model.setViewName("AllDept");
return model;
}
//根据部门号查询部门及所有员工信息
@RequestMapping("getAllMessage")
public ModelAndView getAllMessage(@RequestParam(value="did") Long id) {
System.out.println(id);
ModelAndView model = new ModelAndView();
Dept dept = deptservice.getAllMessage(id);
model.addObject("allList",dept );
model.setViewName("DeptAndEmp");
return model;
}
//添加部门信息
@RequestMapping(value="addDept",method=RequestMethod.POST,headers="application/json")
public ModelAndView addDept(@RequestBody Dept dept) {
ModelAndView model = new ModelAndView();
deptservice.addDept(dept);
model.setViewName("success");
return model;
}
}
EmpController.java
package com.java.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
import com.java.service.EmpService;
/*
* EMP 部门查询是通过jpa解析方法名查询
*
*/
@Controller
public class EmpController {
@Autowired
private EmpService empservice;
//查询所有员工信息t
@RequestMapping("getAllEmp")
public ModelAndView getAllEmp(){
ModelAndView model = new ModelAndView();
model.addObject("empList", empservice.getAllEmp());
model.setViewName("AllEmp");
return model;
}
//模糊查询员工信息
@RequestMapping(value = "getLikeEmp",method=RequestMethod.GET)
public ModelAndView getLikeEmp(@RequestParam(value = "name") String name) {
ModelAndView model = new ModelAndView();
System.out.println("name:"+name);
model.addObject("empList", empservice.getLikeEmp("%"+name+"%"));
model.setViewName("AllEmp");
return model;
}
//通过名字查询员工信息
@RequestMapping(value="getEmpByName")
public ModelAndView getEmpByName() {
ModelAndView model = new ModelAndView();
model.addObject("empList", empservice.getEmpByName("lisi"));
model.setViewName("AllEmp");
return model;
}
}
数据层
EmpDao.java
package com.java.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.java.model.Emp;
@Repository
public interface EmpDao extends JpaRepository{
//模糊查询员工信息
public List findByEnameLike(String name);
@Query("select e from Emp e where e.ename =?1")
List findByEname(String name);
}
DeptDao.java
package com.java.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.java.model.Dept;
@Repository
public interface DeptDao extends JpaRepository{
}
模型层
Dept
package com.java.model;
/*
*
* 单向的一对多
*
*/
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
@Entity
@Table(name = "dept")
public class Dept {
@Id
@Column(name = "did")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long did;
private String dname;
private String addres;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,targetEntity=Emp.class)
@JoinColumn(name = "did",nullable = false)
private Set sets;
public Set getSets() {
return sets;
}
public void setSets(Set sets) {
this.sets = sets;
}
public Long getDid() {
return did;
}
public void setDid(Long did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getAddres() {
return addres;
}
public void setAddres(String addres) {
this.addres = addres;
}
}
Emp
package com.java.model;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "emp")
public class Emp {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long eid;
private String ename;
private Date hiredate;
private double salery;
public Long getEid() {
return eid;
}
public void setEid(Long eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public double getSalery() {
return salery;
}
public void setSalery(double salery) {
this.salery = salery;
}
}
Service层
DeptService
package com.java.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.java.dao.DeptDao;
import com.java.model.Dept;
@Service
public class DeptService {
@Autowired
private DeptDao deptdao;
//查询所有部门信息
public List getAllDept(){
return deptdao.findAll();
}
//根据部门号查询所有信息
public Dept getAllMessage(Long id){
return deptdao.findOne(id);
//return deptdao.getOne(id);
}
//添加部门信息
public void addDept(Dept dept) {
deptdao.saveAndFlush(dept);
}
}
EmpService
package com.java.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import com.java.dao.EmpDao;
import com.java.model.Emp;
@Service
public class EmpService {
@Autowired
private EmpDao empdao;
//查询所有员工信息
public List getAllEmp(){
return empdao.findAll();
}
//模糊查询员工信息
public List getLikeEmp(String name){
return empdao.findByEnameLike(name);
}
//通过名字查询信息
public List getEmpByName(String name){
return empdao.findByEname(name);
}
}
最后是,,,结果: